Character Array and Character Pointer in C

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:

In behavior the statements the type of variable 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:

  1. arr is an array of 12 characters. When compiler sees the statement:

     char arr[] = "Hello World";
    

    It allocates 12 consecutive bytes of memory and associates the address of the first allocated byte with arr.

    character-array-in-memory

    On the other hand when the compiler sees the statement.

     char ptr* = "Yello World";
    

    It allocates 12 consecutive bytes for string literal "Hello World" and 4 extra bytes for pointer variable ptr. And assigns the address of the string literal to ptr. So, in this case, a total of 14 bytes are allocated.

    character-pointer-and-string-literal

  2. We already learned that name of the array is a constant pointer. So if arr points 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.

     arr = "Yellow World"; // Wrong
    

    On the contrary, ptr is a pointer variable of type char, so it can take any other address. As a result string, assignments are valid for pointers.

     ptr = "Yellow World"; // ok
    

    string-assignment-is-valid-for-pointer-to-char

    After the above assignment, ptr points 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 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. Recall that modifying a string literal causes undefined behavior, so the following 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. Using an uninitialized pointer may also lead to undefined undefined behavior.

     char *ptr;
    

    Here ptr is uninitialized an contains garbage value. So the following operations are 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 operations mentioned above are valid. Another way we can use ptr is by allocation memory dynamically using malloc() or calloc() functions.

     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.

#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:

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