String as Pointer in C Programming Language
Table of Content:
In the previous tutorial, we learn that the abstract idea of a string is implemented with just an array of characters. For example, here is a string:
char arrayName[] = "Single";
What this array looks like in memory is the following:
S | i | n | g | l | e | \0 |
where the beginning of the array is at some location in computer memory,
for example, location 1000
.
Note: Don't forget that one character is needed
to store the nul character (\0
), which indicates the end of the string.
A character array can have more characters than the abstract string held in it, as below:
char arrayName[10] = "Single";
giving an array that looks like:
S | i | n | g | l | e | \0 |
(where 3 array elements are currently unused).
Since these strings are really just arrays, we can access each character in the array using subscript notation, as in:
printf("Third char is: %c\n", arrayName[2]);
which prints out the third character, n.
A disadvantage of creating strings using the character array syntax is that you must say ahead of time how many characters the array may hold. For example, in the following array definitions, we state the number of characters (either implicitly or explicitly) to be allocated for the array.
char arrayName[] = "Single"; /* 7 characters */ char arrayName[10] = "Single";
Thus, you must specify the maximum number of characters you will ever need to store in an array. This type of array allocation, where the size of the array is determined at compile-time, is called static allocation.
Strings as pointers
Another way of accessing a contiguous chunk of memory, instead of with an array, is with a pointer.
Since we are talking about strings, which are made up of characters, we'll be using pointers to characters, or rather, char *
's.
However, pointers only hold an address, they cannot hold all the characters in a character array. This means that when we use a char *
to keep track of a string, the character array containing the string must already exist (having been either statically- or dynamically-allocated).
Below is how you might use a character pointer to keep track of a string.
char label[] = "atnyla"; char arrayName1[10] = "welcome"; char *Ptr; Ptr = arrayName;
We would have something like the following in memory (e.g., supposing that the array arrayName
started at memory address 2000
, etc.):
arrayName - starting memory address @2000arrayName1 - starting memory address @3000
a t n y l a \0 Ptr - memory address @4000 -------- | 2000 | --------
w e l c o m e \0
Note: Since we assigned the pointer the address of an array of characters, the pointer must be a character pointer--the types must match.
Also, to assign the address of an array to a pointer, we do not use the address-of (&
) operator since the name of an array (like arrayName
) behaves like the address of that array in this context. That's also why you don't use an ampersand when you pass a string variable to scanf()
, e.g,
int id; char name[30]; scanf("%d%s", &id, name);
Now, we can use Ptr
just like the array name arrayName
. So, we could access the third character in the string with:
printf("Third char is: %c\n", Ptr[2]);
It's important to remember that the only reason the pointer Ptr
allows us to access the arrayName
array is because we made Ptr
point to it. Suppose, we do the following:
Ptr = arrayName1;
Now, no longer does the pointer Ptr
refer to arrayName
, but now to arrayName1
as follows:
arrayName1 - starting memory address @3000Ptr memory address @4000 -------- | 3000 | --------
w e l c o m e \0
So, now when we subscript using Ptr
, we are referring to characters in arrayName1
. The following:
printf("Third char is: %c\n", Ptr[2]);
prints out r, the third character in the arrayName1
array.
C program to print a string using pointer.
Program
C program to print a string using pointer.
/*C program to print a string using pointer.*/ #includeint main() { char strng[100]; char *ptr; printf("Enter a string: "); gets(strng); //assign address of str to ptr ptr=strng; printf("Entered string is: "); while(*ptr!='\0') printf("%c",*ptr++); return 0; }
Output
Enter a string: atnyla Entered string is: atnyla