OverIQ.com

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:

  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* = "Hello 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 16 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.

     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';
    
  3. 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");
    
  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.

    1
    2
    3
    4
    5
    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.

    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() or calloc() 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