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.


    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.


  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


    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 a new string to arr by using gets(), scanf(), strcpy() or by assigning characters one by one.

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

Let's conclude this chapter by creating dynamic 1-d array of characters.


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