The difference between char array and char pointer in C language

        For the difference between character array and character pointer in C language, let us first look at the following example:

void test()
{
    //arr is array of characters
    char arr[12] = "Aticleworld";
    
    //ptr is pointer to char
    char *ptr = "Aticleworld";
}

        Now, let's compare arr (array of characters) and ptr (pointer to characters).

Difference 1:

        A string literal is a sequence of zero or more multibyte characters enclosed in double quotes. When you write the statement char arr[12] = "Articleworld" the characters in the string literal are copied to arr.

        When you write the statement char *ptr = "Articleworld" you are having the array of string literals undergo an array-to-pointer conversion to obtain a pointer to its first element. The pointer ptr points to the first element ('A') of the string literal array.

 Difference 2:

        arr is a collection of characters stored in contiguous memory locations, while ptr holds the addresses of the characters.

        arr contains 12 elements, each located at a contiguous memory location. On the other hand, ptr holds the address of the first character of the string literal.

Difference 3:

        When we use sizeof operator on char array arr it gives the total number of characters whereas char pointer ptr just gives the size of the pointer. as follows:

#include <stdio.h>
int main()
{
    //arr is array of characters
    char arr[] = "Aticleworld";
    //ptr is pointer to char
    char *ptr = "Aticleworld";
    printf("Size of arr %ld\n", sizeof(arr));
    // sizeof a pointer is printed which is same for all type
    // of pointers (char *, void *, etc)
    printf("Size of ptr %ld", sizeof(ptr));
    return 0;
}

output:


Size of arr 24
Size of ptr 4

Difference 4:

        Another important difference between pointers and pointers is that we can increment pointers, but we cannot create increments of arrays. example:

arr++ =>非法语句。
ptr++ ==>正常语句。

Difference 5:

        We can reassign values ​​to arrays, but string literals are not modifiable. If the program attempts to modify a static array formed of string literals, the behavior is undefined. example:

//arr is array of characters
char arr[] = "Aticleworld";
gets(arr); 
fgets(arr,sizeof(arr),stdin); //有效表达式
scanf("%s", arr); //有效表达式
strcpy(arr, "aticle"); //有效表达式
arr[0] = 'a'; //有效表达式
arr[10] = 'M'; //有效表达式
arr[11] = 'M'; //有效表达式
char *ptr = "Aticleworld";

ptr[0] = 'P'; //无效表达式

*ptr = 'W'; //无效表达式

Difference 6:

        Uninitialized pointers can also cause undefined behavior. See the examples below.

char *ptr;
ptr[0] = 'A'; //未定义的行为
gets(ptr); //未定义的行为
scanf(“%s”, ptr); //未定义的行为
strcpy(ptr, “source”); //未定义的行为
strcat(ptr, “second string”); //未定义的行为

Difference 7:

        Char arrays are static in nature which means you cannot resize the array whereas with pointers you can change the size of the allocated memory at any point of time.

Difference 8:

        Arrays are completely under program control. It will properly allocate the memory it needs and automatically free the memory when it goes out of scope. However, the case is different for char pointers if you allocate dynamic memory, you have to deallocate it manually or you will introduce a memory leak. example:

void foo1()
{
    //arr是字符数组
    char arr[12] = "Aticleworld";
}


//Issue memory leak
void foo2()
{
    char *ptr = (char*)malloc(12);
   
    //忘记释放内存
}

        The above are several differences and related examples between character arrays and character pointers in C language.

Guess you like

Origin blog.csdn.net/huanxiajioabu/article/details/131354897