用结构数组调用C函数
因此,我试图在C中进行一些实践,尝试创建一个结构的动态数组,但在尝试将结构传递到不同操作的不同函数中时遇到了一些困难 到目前为止,我的代码是:用结构数组调用C函数,c,arrays,struct,C,Arrays,Struct,因此,我试图在C中进行一些实践,尝试创建一个结构的动态数组,但在尝试将结构传递到不同操作的不同函数中时遇到了一些困难 到目前为止,我的代码是: #include <stdio.h> #include <string.h> #include <stdlib.h> struct node { char *str; int len; }; //& gives address of value, * gives value at address int ma
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char *str;
int len;
};
//& gives address of value, * gives value at address
int main(void) {
struct node **strarray = NULL;
int count = 0, i = 0;
printf("hello\n");
strarray = (struct node **)realloc(strarray, (count + 1) * sizeof(struct node *));
/* allocate memory for one `struct node` */
strarray[count] = (struct node *)malloc(sizeof(struct node));
strarray = init(strarray);
return 0;
}
struct node ** init(struct node ** strarray){ //this is the line that's causing problems
int i = 0, count = 0;
char line[1024];
if(fgets(line, 1024, stdin) != NULL) {
/* add ONE element to the array */
strarray = (struct node **)realloc(strarray, (count + 1) * sizeof(struct node *));
/* allocate memory for one `struct node` */
strarray[count] = (struct node *)malloc(sizeof(struct node));
/* copy the data into the new element (structure) */
strarray[count]->str = strdup(line);
strarray[count]->len = strlen(line);
count++;
return **strarray;
}
}
void printarray(){
for(i = 0; i < count; i++) {
printf("--\n");
printf("[%d]->str: %s", i, strarray[i]->str);
printf("[%d]->len: %d\n", i, strarray[i]->len);
}
}
#包括
#包括
#包括
结构节点{
char*str;
内伦;
};
//&给出值的地址,*在地址处给出值
内部主(空){
结构节点**strarray=NULL;
整数计数=0,i=0;
printf(“hello\n”);
strarray=(结构节点**)realloc(strarray,(count+1)*sizeof(结构节点*);
/*为一个“结构节点”分配内存*/
strarray[count]=(结构节点*)malloc(sizeof(结构节点));
strarray=init(strarray);
返回0;
}
结构节点**init(结构节点**strarray){//这是导致问题的行
int i=0,count=0;
字符行[1024];
如果(fgets(第1024行,标准输入)!=NULL){
/*向数组中添加一个元素*/
strarray=(结构节点**)realloc(strarray,(count+1)*sizeof(结构节点*);
/*为一个“结构节点”分配内存*/
strarray[count]=(结构节点*)malloc(sizeof(结构节点));
/*将数据复制到新元素(结构)中*/
strarray[count]->str=strdup(行);
strarray[count]->len=strlen(行);
计数++;
返回**strarray;
}
}
void printary(){
对于(i=0;istr:%s”,i,strarray[i]->str);
printf(“[%d]->len:%d\n”,i,strarray[i]->len);
}
}
我还没有使用printarray方法,我正在尝试使用函数声明和传递。目前,“init”的类型有冲突
结构节点**init(结构节点**strarray)
这个错误我已经尝试了很多次修复,但都没有用 您的问题是您正在取消对返回的变量的限制。 做 而不是
return **strarray
以下是整个功能:
struct node ** init(struct node ** strarray){
int i = 0, count = 0;
char line[1024];
if(fgets(line, 1024, stdin) != NULL) {
/* add ONE element to the array */
strarray = (struct node **)realloc(strarray, (count + 1) * sizeof(struct node *));
/* allocate memory for one `struct node` */
strarray[count] = (struct node *)malloc(sizeof(struct node));
/* copy the data into the new element (structure) */
strarray[count]->str = strdup(line);
strarray[count]->len = strlen(line);
count++;
return strarray;
}
}
您的问题是您正在取消接收返回的变量。 做 而不是
return **strarray
以下是整个功能:
struct node ** init(struct node ** strarray){
int i = 0, count = 0;
char line[1024];
if(fgets(line, 1024, stdin) != NULL) {
/* add ONE element to the array */
strarray = (struct node **)realloc(strarray, (count + 1) * sizeof(struct node *));
/* allocate memory for one `struct node` */
strarray[count] = (struct node *)malloc(sizeof(struct node));
/* copy the data into the new element (structure) */
strarray[count]->str = strdup(line);
strarray[count]->len = strlen(line);
count++;
return strarray;
}
}
您应该在尝试调用函数之前声明它们。您不应该强制转换
malloc
或realloc
的结果。您还应该使用“gcc-Wall”进行编译,因为您有很多警告强制转换realloc
的结果不是一个好的做法。原因很清楚,这很有趣,不知道虚空已经覆盖了它。谢谢您应该在尝试调用函数之前声明它们。您不应该强制转换malloc
或realloc
的结果。您还应该使用“gcc-Wall”进行编译,因为您有很多警告强制转换realloc
的结果不是一个好的做法。原因很清楚,这很有趣,不知道虚空已经覆盖了它。谢谢您好,感谢您的回复,我以前确实尝试过该修复,但后来被以下错误取代:struct node**init(struct node**strarray)中的init类型冲突行更改返回语句本身,而不是我的答案@IsaacChan中所述的函数定义,因为我编译了它,但没有得到错误,并且您的函数声明是同一行?i、 e.struct node**init(struct node**strarray)原因我将return语句改为return strarray;现在我得到了冲突类型错误。你介意发布你要编译的代码吗?我会试着看看是否有什么不同,我可能没有注意到OnnerMind,我在这个问题上花了7个小时,我才意识到整个过程中,我从来没有初始化过这个函数……很抱歉,我实际上也声明了函数头@IsaacChanHi,谢谢你的回复,我以前确实尝试过这个修复,但是,它被以下错误所取代:struct node**init(struct node**strarray)行中init的冲突类型更改返回语句本身,而不是我的答案@IsaacChan中所述的函数定义,因为我编译了它,但没有得到错误,而您的函数声明是同一行?i、 e.struct node**init(struct node**strarray)原因我将return语句改为return strarray;现在我得到了冲突类型错误。你介意发布你要编译的代码吗?我会试着看看有没有什么不同,我可能没有注意到OnnerMind,我在这个问题上花了7个小时,我才意识到整个过程中,我从未初始化过这个函数……很抱歉,我实际上也声明了函数头@IsaacChan