简体   繁体   English

为什么结构指针变量在它指向的结构变量中打印整个内容,即使我想打印特定元素?

[英]Why is struct pointer variable printing the whole thing inside the struct variable it's pointing to even though I want to print a specific element?

I created a struct datatype 'ans' that contains three string datatype member variables a[2],b[2],c[2].我创建了一个结构数据类型“ans”,其中包含三个字符串数据类型成员变量 a[2]、b[2]、c[2]。 Inside main, I created a struct variable 'p' to accept the three string inputs and then pass it to a function - void f1(ans *x) via call by reference to print the strings.在 main 内部,我创建了一个结构变量 'p' 来接受三个字符串输入,然后通过引用调用将其传递给 function - void f1(ans *x) 以打印字符串。 Now in the function, instead of printing the three separate strings (*x).a,(*x).b,(*x).c, it is printing the whole string joined together.现在在 function 中,不是打印三个单独的字符串 (*x).a,(*x).b,(*x).c,而是打印连接在一起的整个字符串。 I am attaching the code and output for reference:我附上代码和 output 以供参考:

#include <stdio.h>

typedef struct 
{
    char a[2];
    char b[2];
    char c[2];
} ans;

void f1(ans *x) {
   printf("The strings are :\n");
   printf("%s\n",(*x).a);
   printf("%s\n",(*x).b);
   printf("%s\n",(*x).c);
}

int main() {
    ans p;
    printf("Enter for a:\n");
    scanf("%s", p.a);
    printf("Enter for b:\n");
    scanf("%s", p.b);
    printf("Enter for c:\n");
    scanf("%s", p.c);

    f1(&p);

    return 0;
}

Sample output:样品 output:

Enter for a:
ab
Enter for b:
cd
Enter for c:
ef
The strings are :
abcdef
cdef
ef

Can anyone explain why is this showing as output instead of the following:谁能解释为什么这显示为 output 而不是以下内容:

The strings are:
ab
cd
ef

I can't figure out what's happening:(我不知道发生了什么:(

In scanf("%s", pa);scanf("%s", pa); , scanf reads characters and writes them to the memory pointed to by pa . , scanf读取字符并将它们写入pa指向的 memory 。 It also writes a terminating null character after them.它还在它们后面写入一个终止 null 字符。 Since the a member of the structure is declared as char a[2];由于结构的a成员被声明为char a[2]; , when scanf writes more than two characters, including the terminating null, the behavior is not defined by the C standard. ,当scanf写入两个以上字符时,包括终止 null,C 标准未定义该行为。

In printf("%s\n",(*x).a);printf("%s\n",(*x).a); , for %s , printf takes a pointer to a char and prints the characters it finds there until a terminating null character marks the end of the string. ,对于%sprintf获取一个指向char的指针并打印在那里找到的字符,直到终止 null 字符标记字符串的结尾。 When there is no terminating null character the array that is (*x).a , printf overruns the array, and the behavior is not defined by the C standard.当没有终止 null 字符时,数组即(*x).aprintf超出数组,并且行为未由 C 标准定义。

To fix the problem, ensure there is enough space in the arrays for all the characters to be written into them, including the terminating null character, or ensure that no more characters are written in the arrays than will fit.要解决此问题,请确保 arrays 中有足够的空间用于所有要写入其中的字符,包括终止 null 字符,或者确保在 ZA3CBC3F9D0CE2F2C1554E1B671D71 中写入的字符不超过合适的。

if you enter "ab" , scanf will scan string as "ab\0" which is 3 characters, therefore your a[2] won't fit as same as other variabls.如果您输入"ab"scanf会将字符串扫描为"ab\0" ,即 3 个字符,因此您a[2]不会像其他变量一样适合。

To prevent scanf from overruns the array, a simple adaptation of your program, from the several that can be suggested:为了防止 scanf 超出数组,您的程序的简单改编,可以建议的几个:

#define MAXCH   3

typedef struct 
{
    char a[MAXCH];
    char b[MAXCH];
    char c[MAXCH];
} ans;

void f1(ans *x)
{
   printf("The strings are :\n");
   printf("%s\n",(*x).a);
   printf("%s\n",(*x).b);
   printf("%s\n",(*x).c);
}

int main()
{
    ans p;
    char in[128];
    
    printf("Enter for a:\n");
    scanf("%s",in);
    snprintf(p.a,MAXCH,"%s",in);
    printf("Enter for b:\n");
    scanf("%s",in);
    snprintf(p.b,MAXCH,"%s",in);
    printf("Enter for c:\n");
    scanf("%s",in);
    snprintf(p.c,MAXCH,"%s",in);

    f1(&p);

    return 0;
}

Sample output:样品 output:

Enter for a:
abcdefgh
Enter for b:
cdefghij
Enter for c:
efghijkl
The strings are :
ab
cd
ef

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

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