简体   繁体   English

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

[英]Difference between character array and pointer

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. 在代码2中:我使用了char数组用于相同的目的。 But why is the output is different ? 但为什么输出会有所不同?

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?)  
}

In the second example you're assigning a to b , which means that ap ( char* ) is being assigned to bp . 在第二个示例中,您将a分配给b ,这意味着将apchar* )分配给bp Therefore modifying the memory pointed to by bp is also modifying the memory pointed to by ap , since they're both pointing to the same location in memory . 因此,修改bp指向的内存也会修改ap指向的内存,因为它们都指向内存中的相同位置

In the first example, you have two separate arrays . 在第一个示例中,您有两个单独的数组 Assigning a to b copies each char in the array ap to bp - these blocks of memory are part of the struct , they're not pointers to a specific part in memory. 分配ab将数组ap中的每个char复制bp - 这些内存块是struct一部分,它们不是指向内存中特定部分的指针。 Any modification to bp in this case can't affect ap since they're completely unrelated. 在这种情况下对bp任何修改都不会影响ap因为它们完全不相关。

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)时,它会将两个节点都指向的内存写入内存。

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. 在你的第一个代码中,struct包含一个实际的字符数组( char[10] ),因此当你复制struct( b = a )时,数组也会被复制。 Then you overwrite one of them but not the other. 然后你覆盖其中一个而不是另一个。

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. 在第二个代码中,struct包含一个指向字符的指针,因此在复制struct时,指针会被复制,但数据却不会复制。 So both point to the same data. 所以两者都指向相同的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM