C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Pointers and Strings

C Programming

In this tutorial we will learn to store strings using pointers in C programming language.

We know that a string is a sequence of characters which we save in an array. And in C programming language the \0 null character marks the end of a string.

Creating a string

In the following example we are creating a string str using char character array of size 6.

char str[6] = "Hello";

The above string can be represented in memory as follows.

Each character in the string str takes 1 byte of memory space.

Creating a pointer for the string

The variable name of the string str holds the address of the first element of the array i.e., it points at the starting memory address.

So, we can create a character pointer ptr and store the address of the string str variable in it. This way, ptr will point at the string str.

In the following code we are assigning the address of the string str to the pointer ptr.

char *ptr = str;

We can represent the character pointer variable ptr as follows.

The pointer variable ptr is allocated memory address 8000 and it holds the address of the string variable str i.e., 1000.

Accessing string via pointer

To access and print the elements of the string we can use a loop and check for the \0 null character.

In the following example we are using while loop to print the characters of the string variable str.

#include <stdio.h>

int main(void) {
    
  // string variable
  char str[6] = "Hello";
  
  // pointer variable
  char *ptr = str;
  
  // print the string
  while(*ptr != '\0') {
    printf("%c", *ptr);
    
    // move the ptr pointer to the next memory location
    ptr++;
  }
  
  return 0;
}

Using pointer to store string

We can achieve the same result by creating a character pointer that points at a string value stored at some memory location.

In the following example we are using character pointer variable strPtr to store string value.

#include <stdio.h>

int main(void) {
    
  // pointer variable to store string
  char *strPtr = "Hello";
  
  // temporary pointer variable
  char *t = strPtr;
  
  // print the string
  while(*t != '\0') {
    printf("%c", *t);
    
    // move the t pointer to the next memory location
    t++;
  }
  
  return 0;
}

Note! In the above code we are using another character pointer t to print the characters of the string as because we don't want to lose the starting address of the string "Hello" which is saved in pointer variable strPtr.

In the above image the string "Hello" is saved in the memory location 5000 to 5005.

The pointer variable strPtr is at memory location 8000 and is pointing at the string address 5000.

The temporary variable is also assigned the address of the string so, it too holds the value 5000 and points at the starting memory location of the string "Hello".

Array of strings

We can create a two dimensional array and save multiple strings in it.

For example, in the given code we are storing 4 cities name in a string array city.

char city[4][12] = {
  "Chennai",
  "Kolkata",
  "Mumbai",
  "New Delhi"
};

We can represent the city array as follows.

The problem with this approach is that we are allocating 4x12 = 48 bytes memory to the city array and we are only using 33 bytes.

We can save those unused memory spaces by using pointers as shown below.

char *cityPtr[4] = {
  "Chennai",
  "Kolkata",
  "Mumbai",
  "New Delhi"
};

In the above code we are creating an array of character pointer cityPtr of size 4 to store the name of the four cities.

We can represent the array of pointers as follows.

The above array of pointers can be represented in memory as follows.

The cityPtr pointer variable is allocated the memory address 8000 to 8007. Assuming integer address value takes 2 bytes space. So, each pointer gets 2 bytes.

Name of the cities are saved in locations 1000, 2000, 3000 and 4000.

Accessing values pointed by array of pointers

To access and print the values pointed by the array of pointers we take help of loop as shown in the following example.

#include <stdio.h>

int main(void) {
    
  // array of pointers
  char *cityPtr[4] = {
    "Chennai",
    "Kolkata",
    "Mumbai",
    "New Delhi"
  };
  
  // temporary variable
  int r, c;
  
  // print cities
  for (r = 0; r < 4; r++) {
    c = 0;
    while(*(cityPtr[r] + c) != '\0') {
      printf("%c", *(cityPtr[r] + c));
      c++;
    }
    printf("\n");
  }
  
  return 0;
}

Output:

Chennai
Kolkata
Mumbai
New Delhi

In the above code we are using the r variable to access each row of the pointer. And we are using the c variable to access each character in a selected row.