字符数组和指针之间的区别

时间:2022-10-11 19:59:09

I am doing the same thing in both the codes.

我在两个代码中做同样的事情。

In code 1: I have used a char * and allocate the space using malloc in main.

在代码1中:我使用了char *并在main中使用malloc分配空间。

In code 2: I have used a char array for the same purpose. But why is the output is different ?

在代码2中:我使用了char数组用于相同的目的。但为什么输出会有所不同?

Code 1:

代码1:

struct node2
{
    int data;
    char p[10];
}a,b;

main()
{
    a.data = 1;

    strcpy(a.p,"stack");
    b = a;
    printf("%d %s\n",b.data,b.p);     // output 1 stack
    strcpy(b.p,"overflow"); 
    printf("%d %s\n",b.data,b.p);     // output  1 overflow
    printf("%d %s\n",a.data,a.p);     // output  1 stack
}

Code 2:

代码2:

struct node1
{
    int data;
    char *p;
}a,b;

main()
{
    a.data = 1;
    a.p = malloc(100);
    strcpy(a.p,"stack");
    b = a;
    printf("%d %s\n",b.data,b.p);   //output 1 stack
    strcpy(b.p,"overflow");  
    printf("%d %s\n",b.data,b.p);   // output 1 overflow
    printf("%d  %s\n",a.data,a.p); // output 1 overflow(why not same as previous one?)  
}

3 个解决方案

#1


10  

In the second example you're assigning a to b, which means that a.p (char*) is being assigned to b.p. Therefore modifying the memory pointed to by b.p is also modifying the memory pointed to by a.p, since they're both pointing to the same location in memory.

在第二个例子中,你将a分配给b,这意味着a.p(char *)被分配给b.p.因此,修改b.p指向的内存也会修改a.p指向的内存,因为它们都指向内存中的相同位置。

In the first example, you have two separate arrays. Assigning a to b copies each char in the array a.p to b.p - these blocks of memory are part of the struct, they're not pointers to a specific part in memory. Any modification to b.p in this case can't affect a.p since they're completely unrelated.

在第一个示例中,您有两个单独的数组。分配a到b将数组a.p中的每个char复制到b.p - 这些内存块是struct的一部分,它们不是指向内存中特定部分的指针。在这种情况下对b.p的任何修改都不会影响a.p,因为它们完全不相关。

#2


3  

In your first code, the struct contains an actual character array (char[10]), so when you copy the struct (b = a) the array is copied also. Then you overwrite one of them but not the other.

在你的第一个代码中,struct包含一个实际的字符数组(char [10]),因此当你复制struct(b = a)时,数组也会被复制。然后你覆盖其中一个而不是另一个。

In your second code, the struct contains a pointer to a character, so when you copy the struct the pointer is copied, but the data is not. So both point to the same data.

在第二个代码中,struct包含一个指向字符的指针,因此在复制struct时,指针会被复制,但数据却不会复制。所以两者都指向相同的数据。

#3


3  

Character pointers and character arrays are not the same thing.

字符指针和字符数组不是一回事。

Your node with the array is getting copied, so the contents are copied into the new node. When you put the new value (overflow) into the copied node (b), it only overwrites the copy.

您的阵列节点正在被复制,因此内容将被复制到新节点中。将新值(溢出)放入复制的节点(b)时,它只会覆盖副本。

Your node with the character pointer is getting the pointer's value copied, so both nodes point to the same memory location. When you put the new value (overflow) into the copied node (b), it writes to the memory that both nodes have a pointer to.

带有字符指针的节点正在复制指针的值,因此两个节点都指向相同的内存位置。当您将新值(溢出)放入复制的节点(b)时,它会将两个节点都指向的内存写入内存。

#1


10  

In the second example you're assigning a to b, which means that a.p (char*) is being assigned to b.p. Therefore modifying the memory pointed to by b.p is also modifying the memory pointed to by a.p, since they're both pointing to the same location in memory.

在第二个例子中,你将a分配给b,这意味着a.p(char *)被分配给b.p.因此,修改b.p指向的内存也会修改a.p指向的内存,因为它们都指向内存中的相同位置。

In the first example, you have two separate arrays. Assigning a to b copies each char in the array a.p to b.p - these blocks of memory are part of the struct, they're not pointers to a specific part in memory. Any modification to b.p in this case can't affect a.p since they're completely unrelated.

在第一个示例中,您有两个单独的数组。分配a到b将数组a.p中的每个char复制到b.p - 这些内存块是struct的一部分,它们不是指向内存中特定部分的指针。在这种情况下对b.p的任何修改都不会影响a.p,因为它们完全不相关。

#2


3  

In your first code, the struct contains an actual character array (char[10]), so when you copy the struct (b = a) the array is copied also. Then you overwrite one of them but not the other.

在你的第一个代码中,struct包含一个实际的字符数组(char [10]),因此当你复制struct(b = a)时,数组也会被复制。然后你覆盖其中一个而不是另一个。

In your second code, the struct contains a pointer to a character, so when you copy the struct the pointer is copied, but the data is not. So both point to the same data.

在第二个代码中,struct包含一个指向字符的指针,因此在复制struct时,指针会被复制,但数据却不会复制。所以两者都指向相同的数据。

#3


3  

Character pointers and character arrays are not the same thing.

字符指针和字符数组不是一回事。

Your node with the array is getting copied, so the contents are copied into the new node. When you put the new value (overflow) into the copied node (b), it only overwrites the copy.

您的阵列节点正在被复制,因此内容将被复制到新节点中。将新值(溢出)放入复制的节点(b)时,它只会覆盖副本。

Your node with the character pointer is getting the pointer's value copied, so both nodes point to the same memory location. When you put the new value (overflow) into the copied node (b), it writes to the memory that both nodes have a pointer to.

带有字符指针的节点正在复制指针的值,因此两个节点都指向相同的内存位置。当您将新值(溢出)放入复制的节点(b)时,它会将两个节点都指向的内存写入内存。