0

我现在正在学习 C 语言,在理解指针和结构数组时遇到了一些麻烦。这是我编写的一个简单程序:

#include <stdio.h>

typedef struct {                                        /* Define the structure Pokemon that contains a nickname, type and level*/
    char nickname[11];
    char type[11];
    int level;
} Pokemon;

int main(void) {

    char nickname[11];
    char type[11];
    int level;

    for (int i = 0; i < 3; i++) {                      /* Iterate through the loop three times, each time create a new pokemon */
        printf("Pokemon %i \n", i);
        printf("Nickname: ");
        scanf("%s", &nickname);
        printf("Type: ");
        scanf("%s", &type);
        printf("Level: ");
        scanf("%i", &level);

        Pokemon * poke = {nickname, type, level};                /* Insert the pokemon into the array of Pokemon */

        printf("%s, %s, %i", poke->nickname, poke->type, poke->level);
    }
}

基本上我想为具有三个特征的口袋妖怪创建一个结构。在主函数中,我希望用户输入 3 个 pokemon 的特征,然后创建一个具有这三个特征的 struct pokemon 实例,并将这些特征打印到 stdout。使用此代码,它可以编译,但我收到警告:

pokemon.c:33:9: warning: initialization from incompatible pointer type [enabled by default]
pokemon.c:33:9: warning: (near initialization for ‘poke’) [enabled by default]
pokemon.c:33:9: warning: excess elements in scalar initializer [enabled by default]
pokemon.c:33:9: warning: (near initialization for ‘poke’) [enabled by default]
pokemon.c:33:9: warning: excess elements in scalar initializer [enabled by default]
pokemon.c:33:9: warning: (near initialization for ‘poke’) [enabled by default]

不知道为什么会这样——我想这与我设置的指针有关,但正如我所说,我仍在努力解决这个问题。

我还想将每个口袋妖怪实例放入三个口袋妖怪的数组中。到目前为止,我有这个:

Pokemon p [3];

// This bit inside the for loop and after the 'poke' struct instantiation
p[i] = poke;
printf("%s,%s,%i inserted\n", poke.nickname, poke.type, poke.level );

但这不想编译 - 我想这是另一个指针错误。

4

5 回答 5

1

鉴于对所需功能的描述,我想说根本不需要指针。只需读取数据并将其直接存储到您的数组中:

int main(void) 
{
    Pokemon p[3];
    for (int i = 0; i < 3; i++) 
    {
        printf("Pokemon %i \n", i);
        printf("Nickname: ");
        scanf("%s", &(p[i].nickname));
        printf("Type: ");
        scanf("%s", &(p[i].type));
        printf("Level: ");
        scanf("%i", &(p[i].level));

        printf("%s, %s, %i", p[i].nickname, p[i].type, p[i].level);
    }
    return 0;
}
于 2013-03-26T09:30:02.720 回答
1
Pokemon p[3];
...
Pokemon poke;
strncpy(poke.nickname, nickname, sizeof(poke.nickname));
strncpy(poke.type, type, sizeof(poke.type));
poke.level = level;
p[i] = poke;

编辑:固定临时结构初始化。

于 2013-03-26T09:16:44.850 回答
1

在这一行:

   Pokemon * poke = {nickname, type, level};

你只是在声明一个指针,所以你不能将它初始化为一个结构。而且由于您的结构具有字符数组,因此您不能分配字符串指针。

如果您使用的是 C99,则可以使用复合文字:

   Pokemon * poke = &(Pokemon){.level = level}; 

否则,您需要声明一个结构,然后分配其地址。

对于下一部分,您需要

   p[i] = *poke;

将指向的结构复制到新的结构中。

但是,以这种方式初始化不会将字符串复制到结构中。你可以做什么:

   strcpy(p[i].nickname, nickname);
   strcpy(p[i].type, type);

您还应该删除&这些行中的:

   scanf("%s", &nickname);
   scanf("%s", &type);
于 2013-03-26T09:19:55.100 回答
1

问题出在线路上

Pokemon * poke = {nickname, type, level};                /* Insert the pokemon into the array of Pokemon */

因此,您也可以简单地使用它,而不是像那样初始化:

    Pokemon *poke = malloc(sizeof(Pokemon));
    strcpy(poke->nickname,nickname);
    strcpy(poke->type,type);
    poke->level=level;   
于 2013-03-26T09:33:09.677 回答
1

为什么不直接读入你的数组?

#include <stdio.h>
typedef struct {         /* Define the structure Pokemon that contains a nickname, type and level*/
    char nickname[11];
    char type[11];
    int level;
} Pokemon;

int main(void) 
{
    Pokemon p[3];

    for (int i = 0; i < 3; i++)   /* Iterate three times, each time create pokemon */
    {                      
        printf("Pokemon %i \n", i);
        printf("Nickname: ");
        scanf("%10s", p[i].nickname);
        printf("\nType: ");
        scanf("%10s", p[i].type);
        printf("\nLevel: ");
        scanf("%i", &p[i].level);

        printf("\n%s, %s, %i\n", p[i].nickname, p[i].type, p[i].level);
    }
}
于 2013-03-26T09:42:20.407 回答