gpt4 book ai didi

c - 打印结构体的字符串

转载 作者:行者123 更新时间:2023-11-30 19:31:34 25 4
gpt4 key购买 nike

当我尝试打印字符串“descpt”时出现问题。有人可以帮助我吗?这是我的代码:

struct Article {
int a;
int b;
const char *descpt[20];
};

void Print(Article * data) {
printf("The article number is %d\n", data->a);
printf("The quantity is %d\n", data->b);
printf("The article number is %s\n", data->descpt[20]);
}

int main() {
Article data;
data.a = 10;
data.b = 20;
data.descpt[20] = "Articles";
Print(&data);
return 0;
}

最佳答案

#include<stdio.h>
#include<string.h>
struct Article {
int a;
int b;
char descpt[20];
};

void Print(Article * data) {
printf("The article number is %d\n", data->a);
printf("The quantity is %d\n", data->b);
printf("The article number is %s\n", data->descpt);
}
int main()
{
Article data;
data.a = 10;
data.b = 20;
strncpy_s(data.descpt, "Articles", strlen("Articles"));
Print(&data);
return 0;

}

关于c - 打印结构体的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48436972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com