C 返回结构数组时出现问题
我是个新手,我对类型结构的数组有问题。我真的不知道在哪里指定数组是指针,在哪里不指定它。所以,无论我在声明和调用涉及这个结构数组的函数时对指针做什么组合,它都不会编译。你知道为什么吗? 以下是相关代码:C 返回结构数组时出现问题,c,C,我是个新手,我对类型结构的数组有问题。我真的不知道在哪里指定数组是指针,在哪里不指定它。所以,无论我在声明和调用涉及这个结构数组的函数时对指针做什么组合,它都不会编译。你知道为什么吗? 以下是相关代码: typedef struct{ char letter[7]; } player; player init_player(){ player tab[2]; char letter_player1[4]={' ','a','b','c'}; char letter_playe
typedef struct{
char letter[7];
} player;
player init_player(){
player tab[2];
char letter_player1[4]={' ','a','b','c'};
char letter_player2[4]={' ','x','y','z'};
strcpy(tab[1].letter,letter_player1);
strcpy(tab[2].letter,letter_player2);
return *tab;
}
void displaying(player tab){
int position=1;
printf("%c\n",*tab[1].letter[position]);
}
int main(){
player tab=init_player(tab);
displaying(tab);
}
提前谢谢你 您的代码如何出错:
/* required #include (or function prototypes) are not written */
typedef struct{
char letter[7];
} player;
player init_player(){ /* the return type is not for returning arrays */
player tab[2];
/* no terminating null characters are placed, so cannot be used with strcpy() */
char letter_player1[4]={' ','a','b','c'};
char letter_player2[4]={' ','x','y','z'};
strcpy(tab[1].letter,letter_player1);
strcpy(tab[2].letter,letter_player2); /* only tab[0] and tab[1] are available, tab[2] is out-of-bounds */
return *tab; /* not the array but only the first element of the array is returned */
}
void displaying(player tab){ /* the argument type is not for getting arrays */
int position=1;
/* even if tab were an array(pointer), dereferencing is done via [] operator, so you don't need * here */
printf("%c\n",*tab[1].letter[position]);
}
int main(){
player tab=init_player(tab); /* the variable type is not for dealing with arrays */
displaying(tab);
}
如何修复:
/* add #include */
#include <stdio.h> /* for printf() */
#include <stdlib.h> /* for malloc(), free() and exit() */
#include <string.h> /* for strcpy() */
typedef struct{
char letter[7];
} player;
/* change return type to return a pointer */
player* init_player(){
/* you cannot use non-static local array, so instead dynamically allocate an array here */
player* tab = malloc(sizeof(*tab) * 2);
/* add terminating null characters (increase array size, and they will be automatically added) */
char letter_player1[5]={' ','a','b','c'};
char letter_player2[5]={' ','x','y','z'};
if (tab == NULL) exit(1); /* for in case the allocation failed */
/* use tab[0] and tab[1] instead of tab[1] and tab[2] */
strcpy(tab[0].letter,letter_player1);
strcpy(tab[1].letter,letter_player2);
return tab; /* return the (pointer to the first element of) array */
}
/* change argument type to get a pointer */
void displaying(player* tab){
int position=1;
/* remove extra * (indirection operator) */
printf("%c\n",tab[1].letter[position]);
}
int main(){
/* change variable type to store a pointer */
player* tab=init_player(tab);
displaying(tab);
free(tab); /* the array is dynamically allocated, so clean-up it */
}
您不需要将参数选项卡传递给init_player,
但是它是无害的,因为init_player可以接受任何参数,并且不使用这些参数。strcpy需要一个以nul结尾的字符串:char letter_player1[]={','a','b','c','\0'};选项卡[2]不存在,c中的数组基于0。最后,返回一个本地数组,其生存期以函数UB结束。@DavidRanieri注释的最后一部分是错误的。返回的不是本地数组,而是本地数组的第一个元素,因此没有UB用于返回将变为无效的指针。@MikeCAT在function:main中,语句:player*tab=init_playertab;这是不对的。因为函数:init_player没有参数。@MikeCAT哇谢谢你这么全面的回答,我没想到会有这么多错误!有办法绕过马洛克吗?如果不是,我想是时候学习了哈哈