简体   繁体   English

带有char数组的结构问题

[英]question on struct with char array

Below is my code snippet 以下是我的代码段

struct encode
{

   char code[MAX];

}a[10];

int main()
{ 
char x[]={'3','0','2','5','9','3','1'};

  for(i=0;i<1;i++)
  {

      printf("%c",x[i]);

//This will printout like 3025931 now I want this to be stored in structure.         
  }

strcpy(a[0].code,x); 
// or 
a[0].code=x;//neither works

display();

}

void display()
{
printf("%c",a[0].code);
}

I want the output to be like:3025931. 我希望输出为:3025931。

Which I am not getting due to incompatible assign type. 由于分配类型不兼容而无法获取该信息。 Please tell me where am i going wrong. 请告诉我我要去哪里错了。

I see two problems here. 我在这里看到两个问题。 The first is that the source of the strcpy is a where it probably should be x . 第一个是,源strcpya地方可能应该是x

The second is that x is not null-terminated. 第二个是x不是以空值结尾的。 Strings in C are null-terminated character arrays. C中的字符串是以null结尾的字符数组。

I would change the two lines: 我将更改这两行:

char x[] = {'3','0','2','5','9','3','1'};
strcpy(a[0].code, a);

to: 至:

char x[] = {'3','0','2','5','9','3','1', '\0'};
strcpy(a[0].code, x);

Here's a complete program that gives you what you want (it actually prints out the number twice, once in your inner loop character by character and once with the printf so that you can see they're the same): 这是一个完整的程序,可为您提供所需的内容(它实际上将数字打印两次,一次在每个字符的内部循环中,一次在printf以便您可以看到它们是相同的):

#include <stdio.h>
#include <string.h>
#define MAX 100
struct encode {
    char code[MAX];
} a[10];

int main() {
    int i, j;
    char x[] = {'3','0','2','5','9','3','1','\0'};

    for(i = 0; i < 1; i++) {
        for(j = 0; j < 7; j++) {
            printf("%c", x[j]);
        }
        printf("\n");

        strcpy(a[0].code, x);
    }
    printf("%s\n",a[0].code);
    return 0;
}

Update based on comment: 根据评论更新:

I am sorry. 对不起。 I am new to C. My apologies for not pasting the code snippet correctly in the beginning: "printf("%c",a[0].code);" 我是C的新手。我很抱歉未能在一开始就正确粘贴代码段:“ printf(”%c“,a [0] .code);” doesn't display "3025931". 不显示“ 3025931”。

No, it won't. 不,不会。 That's because a[0].code is a character array (string in this case) and you should be using "%s" , not "%c" . 这是因为a[0].code是一个字符数组(在这种情况下为字符串),您应该使用"%s"而不是 "%c" Changing the format specifier in the printf should fix that particular issue. printf更改格式说明符应该可以解决该特定问题。

Here, 这里,

strcpy(a[0].code, a);

did you mean 你的意思是

strcpy(a[0].code, x);

...? ...?

Also, x needs to be null terminated, or you need to replace strcpy with strncpy or memcpy and pass in a length. 另外, x需要以null终止,或者您需要用strncpymemcpy替换strcpy并传递一个长度。

This line doesn't make much sense: 这行没有多大意义:

   strcpy(a[0].code, a);

Perhaps you want this: 也许您想要这样:

    memcpy(a[0].code, x, sizeof x);
    a[0].code[sizeof x] = '\0';

(The second line is necessary to nul-terminate code , making it a proper C string). (第二行对于nul终止code ,使其成为正确的C字符串是必需的)。

A lot of things are wrong in your program. 您的程序中有很多错误。 The most offending line is this: 最令人讨厌的是:

strcpy(a[0].code, a); 

but there are other oddities as well, eg 但也有其他奇怪的地方,例如

  • display is never called 永不显示
  • a is only assigned (kind of), but never read (except in display, which is never called) 仅分配了(一种),但从未读取(在显示中,从未调用过)
  • the i loop makes no sense 我循环没有意义

Basically, this program looks like copy-pasted by someone who has no clue. 基本上,该程序看起来像是由一无所知的人复制粘贴的。

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

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