gpt4 book ai didi

c - 结构体,并将变量定义为同义词

转载 作者:行者123 更新时间:2023-11-30 19:49:08 27 4
gpt4 key购买 nike

我正在与结构作斗争,并且有一些我不知道如何做的问题。首先,我必须定义一个结构体调用parts,其中包含int变量part_number,i和一个包含25个字符的字符数组。其次,我必须将部分定义为结构部分的同义词。第三次从键盘读取part_number和part_name到变量a的各个成员中。

include <stdio.h>

int main(void)
{
struct part_containg

{
int part_number,i;
char part_name[25] !='\0';
// set up array of 25 characters and include terminating null character
};

struct part_containg part_number,part_number[i];

for(part_number=0; part_number<25;++part_number) // part_number goes up 1 for each part_name
{
printf("Intersert Part Name"\n);
scanf("%c", &part_name[i]); // scans for part_name
}
return 0;
}

最佳答案

您有不少语法错误。你应该查阅一本介绍性的教科书:它们真的很有帮助!我读过六本,我对自己的评价相当低 ~“C 技能连续体”~

无论如何,我们都是从底层开始的。这是带有大量评论的替代版本:

#include <stdio.h>  /* you need a hash sign to include libraries...!     */
#include <string.h> /* i use this library to copy input rather than loop */

int main() { // main does not take an argument

struct part_containing {
int part_number; // what is i for? I removed it
char part_name[25]; // do the null later, this is not the time!
};

struct part_containing name; // only declare the struct, not its variables
// p.s. name is an awful name-choice :P

char input[25]; // make a local var for input
puts("Enter part name:\n"); // prompt user for input
scanf("%s", input); // capture local var
strcpy(name.part_name, input); // copy it into the struct
name.part_name[25] = '\0'; // null just to be sure!

puts("Enter number:\n"); // same for the number now
scanf("%i", &name.part_number); // notice the syntax: &name.part_number...!

printf("%s and %i", name.part_name, name.part_number);
// simple error check
// did we get the expected input? It can be helpful to check in test runs!

return 0;
}

它并不能回答您作业中的所有问题,但足以帮助您入门。

希望这有帮助!如果您正在研究它并有更多问题,请提问!

关于c - 结构体,并将变量定义为同义词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15537342/

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