我正在学习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);
}
}
基本上我想为一个具有三个特征的口袋妖怪创建一个结构。在main函数中,我希望用户输入3个口袋妖怪的特征,然后创建具有这三个特征的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 );
但是这不想编译 - 我想这是另一个指针错误。
答案 0 :(得分: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;
编辑:修复临时struct init。
答案 1 :(得分:1)
在这一行:
Pokemon * poke = {nickname, type, level};
您只是声明一个指针,因此您无法将其初始化为结构。由于你的struct有字符数组,你不能分配一个字符串指针。
如果你正在使用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);
答案 2 :(得分: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;
}
答案 3 :(得分: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;
答案 4 :(得分: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);
}
}