Character Array and Character Pointer in C

Share Your Love

In this chapter we are going to learn about the difference between character array and character pointer  following example: 

char arr[] = "Hello World"; // array version
char ptr* = "Hello World";  // pointer version

Now can you point out the difference between them?

The similarity between them is:

The type of both the variable is a pointer char or (char*) so you can pass them in the function who is formal argument accept array of characters or character pointer.

Here is the difference:

  1. arr is an array of 12 characters while the compiler looks at this statement:
char arr[] = "Hello World";

It allocates 12 consecutive bytes of memory and assigns the first address to the array arr.

array in characters
Array With Characters

On the other side when the compiler looks at this statement it found out:

char ptr* = "Hello World";

It allocates 12 consecutive bytes of a string literal “Hello World” and 4 extra bytes assigned to store the pointer variable ptr. So in this case a total of 16 bytes are allocated.

pointer with character array
Pointer With Characters array

2. We already learned that the name of an array is a constant pointer. If the arr points to the address 2000 until the program ends it will always point to that address 2000, we can’t change its address This means string assignment is not valid for strings defined for arrays.

arr = "Yellow World"; // Wrong

On the other hand, ptr is a pointer variable of type char, so it can take any other address. So string assignment for a valid pointer.

ptr = "Yellow World"; // ok
how pointer differentiate character array
how_pointer_differentiate_character_array

After the above assignment, ptr points to the address of "Yellow World" which is stored differently in the memory.

So there is a question that comes to your mind how do we assign a different string to arr?

We can assign a new string to arr by using gets()scanf()strcpy() or by assigning characters one by one.

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';

3. Modifying string literal causes undefined behaviour, so the below operations are invalid.

char *ptr = "Hello";
ptr[0] = 'Y'; or *ptr = 'Y';
gets(name);
scanf("%s", ptr);
strcpy(ptr, "source");
strcat(ptr, "second string");

4. Uninitialized pointer maybe lead to undefined behaviour.

char *ptr

If ptr holds the garbage value due to uninitialized. then below operation is invalid.

ptr[0] = 'H';
gets(ptr);
scanf("%s", ptr);
strcpy(ptr, "source");
strcat(ptr, "second string");

We can only use ptr only if it points to a valid memory location.

char str[10];
char *p = str;

Now all the above operations are valid. Another way we can use ptr is by allocation memory by using dynamic memory allocation using malloc() and calloc() function.

char *ptr;
ptr = (char*)malloc(10*sizeof(char)); // allocate memory to store 10 characters

Let’s conclude this chapter by ending this using a dynamic 1-d array of characters.

#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;
}

Output:

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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 355

Newsletter Updates

Enter your email address below to subscribe to our newsletter