In this chapter, we will study the difference between character array and character pointer.
Consider the following example:
char arr = "Hello World"; // array version
char ptr* = "Hello World"; // pointer version
Can you point out similarities or differences between them ?
The similarity is:
The type of both the variables is a pointer to
(char*), so you can pass either of them to a function whose formal argument accepts an array of characters or a character pointer.
Here are the differences:
arris an array of
12characters. When compiler sees the statement:
1char arr = "Hello World";
12consecutive bytes of memory and associates the address of the first allocated byte with
On the other hand when the compiler sees the statement.1char ptr* = "Hello World";
12consecutive bytes for string literal
4extra bytes for pointer variable
ptr. And assigns the address of the string literal to
ptr. So, in this case, a total of
14bytes are allocated.
- We already learned that name of the array is a constant pointer. So if
arrpoints to the address
2000, until the program ends it will always point to the address
2000, we can’t change its address. This means string assignment is not valid for strings defined as arrays.1arr = "Yellow World"; // Wrong
On the contrary,
ptris a pointer variable of type
char, so it can take any other address. As a result string, assignments are valid for pointers.1ptr = "Yellow World"; // ok
After the above assignment,
ptrpoints to the address of
"Yellow World"which is stored somewhere in the memory.
Obviously, the question arises so how do we assign a different string to
We can assign a new string to
strcpy()or by assigning characters one by one.12345678910111213gets(arr);scanf("%s", arr);strcpy(arr, "new string");arr = 'R';arr = 'e';arr = 'd';arr = ' ';arr = 'D';arr = 'r';arr = 'a';arr = 'g';arr = 'o';arr = 'n';
- Recall that modifying a string literal causes undefined behavior, so the following operations are invalid.
123456char *ptr = "Hello";ptr = 'Y'; or *ptr = 'Y';gets(name);scanf("%s", ptr);strcpy(ptr, "source");strcat(ptr, "second string");
- Using an uninitialized pointer may also lead to undefined undefined behavior.
ptris uninitialized an contains garbage value. So the following operations are invalid.12345ptr = 'H';gets(ptr);scanf("%s", ptr);strcpy(ptr, "source");strcat(ptr, "second string");
We can only use
ptronly if it points to a valid memory location.12char str;char *p = str;
Now all the operations mentioned above are valid. Another way we can use ptr is by allocation memory dynamically using
calloc()functions.123char *ptr;ptr = (char*)malloc(10*sizeof(char)); // allocate memory to store 10characters
Let’s conclude this chapter by creating dynamic 1-d array of characters.
int n, i;
printf("Enter number of characters to store: ");
ptr = (char*)malloc(n*sizeof(char));
for(i=0; i < n; i++)
printf("Enter ptr[%d]: ", i);
/* notice the space preceding %c is
necessary to read all whitespace in the input buffer
scanf(" %c", ptr+i);
printf("\nPrinting elements of 1-D array: \n\n");
for(i = 0; i < n; i++)
printf("%c ", ptr[i]);
// signal to operating system program ran fine
Enter number of characters to store: 6
Enter ptr: a
Enter ptr: b
Enter ptr: c
Enter ptr: d
Enter ptr: y
Enter ptr: z
Printing elements of 1-D array:
a b c d y z