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

时间:2013-03-21 00:27:39

标签: c struct structure synonym

我正在为结构而苦苦挣扎,并且有一些我无法弄清楚如何做的问题。 首先我必须定义一个结构调用部分包含int变量part_number,i和一个包含25个字符的字符数组。 第二,我必须将part定义为struct部分的同义词。 第三个读取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;
}

1 个答案:

答案 0 :(得分:0)

你有很多语法错误。你应该参考一本介绍性教科书:它们真的很有帮助!我已经阅读了六打,我会对〜“C Skill Continuum”〜

的评价很低

无论如何,我们都从底部开始。这是一个备有很多评论的替代版本:

#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;
}

它没有回答你作业中的所有问题,但它应该足以让你开始。

希望这有帮助!如果您正在处理并有更多问题,请询问!