Character Array and Character Pointer in C
Last updated on July 27, 2020
In this chapter, we will study the difference between character array and character pointer. Consider the following example:
1 2 | 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 or (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 of12characters. When compiler sees the statement:char arr[] = "Hello World";
It allocates
12consecutive bytes of memory and associates the address of the first allocated byte witharr.
On the other hand when the compiler sees the statement.
char ptr* = "Hello World";
It allocates
12consecutive bytes for string literal"Hello World"and4extra bytes for pointer variableptr. And assigns the address of the string literal toptr. So, in this case, a total of16bytes are allocated.
We already learned that name of the array is a constant pointer. So if
arrpoints to the address2000, until the program ends it will always point to the address2000, we can't change its address. This means string assignment is not valid for strings defined as arrays.arr = "Yellow World"; // Wrong
On the contrary,
ptris a pointer variable of typechar, so it can take any other address. As a result string, assignments are valid for pointers.ptr = "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
arr?We can assign a new string to
arrby usinggets(),scanf(),strcpy()or by assigning characters one by one.1 2 3 4 5 6 7 8 9 10 11 12 13
gets(arr); scanf("%s", arr); strcpy(arr, "new string"); arr[0] = 'R'; arr[1] = 'e'; arr[2] = 'd'; arr[3] = ' '; arr[4] = 'D'; arr[5] = 'r'; arr[6] = 'a'; arr[7] = 'g'; arr[8] = 'o'; arr[9] = 'n';
Recall that modifying a string literal causes undefined behavior, so the following operations are invalid.
1 2 3 4 5 6
char *ptr = "Hello"; ptr[0] = '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.
char *ptr;
Here
ptris uninitialized an contains garbage value. So the following operations are invalid.1 2 3 4 5
ptr[0] = '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.1 2
char str[10]; char *p = str;
Now all the operations mentioned above are valid. Another way we can use ptr is by allocation memory dynamically using
malloc()orcalloc()functions.1 2
char *ptr; ptr = (char*)malloc(10*sizeof(char)); // allocate memory to store 10 characters
Let's conclude this chapter by creating dynamic 1-d array of characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include<stdio.h>
#include<stdlib.h>
int main()
{
int n, i;
char *ptr;
printf("Enter number of characters to store: ");
scanf("%d", &n);
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
return 0;
}
|
Expected Output:
1 2 3 4 5 6 7 8 9 10 11 | Enter number of characters to store: 6
Enter ptr[0]: a
Enter ptr[1]: b
Enter ptr[2]: c
Enter ptr[3]: d
Enter ptr[4]: y
Enter ptr[5]: z
Printing elements of 1-D array:
a b c d y z
|
Load Comments