Pointers and strings

The way a group of integers are stored in an array of integers, similarly a group of characters are stored in array of characters. We know that string is nothing but an array of characters, terminated with a null character, ‘\0’. For example,

Char str[] = {‘B’, ‘h’, ‘u’, ‘b’, ‘a’, ‘n’, ‘s’, ‘w’, ‘a’, ‘r’, ‘\0’};

Each character in this array occupies one byte of memory and the last character is always ‘\0’. Physically, it looks like two characters, but actually it an escape sequence. This character is called as null characters.

As far as string is concerned, the null terminated character is important, because this is the only way to check where the string ends. A set of characters not terminated by a null character (‘\0’) is not really a string, merely a collection of characters.

C also provides a shortcut to initialize a string. For example the above string initialization can be rewritten

char str [] = “Bhubaneswar “,

Note that in this declaration an exclusive null character is not required. C automatically inserts a null character at the end of string.

We can work with strings in the following ways:

  • • Pointers to strings
  • • Pointer and string constants
  • • Two – dimensional string arrays
  • • Array of pointers to string

Pointers to string

Pointer variables can be used to access individual elements of string. Let us consider the following statements

char str [] = “Bhubaneswar “;
char *cptr;
cptr = str;

In the last statement cptr, a pointer to character, which will hold the address will hold the address of the first character of string.


Pointers and string constants

String constant is a quoted string whose value is known at compilation time. String constants often stored on read – only heap storage. There are some advantages to using pointers for string constants instead of character any variables.

  • • values may be safe in read – only memory
  • • less storage needed for table of pointers to constants
  • • can use direct assignment instead of string functions

Two – dimensional characters arrays

We have already mentioned in prev chapter i.e two – dimensional array of integers. The two – dimensional array of characters is similar to two – dimensional array of integers.


Array of pointers to strings

As we know, a pointer variable always contains an address. Therefore, if we construct an array pointers, it would contain a number of addresses. Let us take the array of pointers to month names, which we have discussed in earlier sub – section. Each array element will contain the address of the corresponding month name string somewhere in memory.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext