程序示例(仅示例不能正常工作):
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
TITLE *part_struct(char *line);
LEN *lenght(TITLE *title);
int main(){
char line[]="Thomas,Bukurest";
part_struct(line); //this function must return reference on type TITLE*
//when is structure filled with data I need to pass them to another function
lenght(title); //this does not compile and should return reference on type LEN*
return 0;
}
typedef struct title {
char* city;
char* name;
}TITLE;
typedef struct len {
int x;
int y;
}LEN;
TITLE *part_struct(char *line){ //this line must stay as it is
TITLE *title = (TITLE*)malloc(sizeof(TITLE));
char* buffer;
char copy[20];
strcpy(copy,line);
buffer = strtok (copy,",");
title->name=(char*) malloc(sizeof (char)*(strlen(buffer)));
title->name=buffer;
buffer = strtok (NULL, ",");
title->city=(char*) malloc(sizeof (char)*(strlen(buffer)));
title->city=buffer;
return level; //I am not sure if I am returning it as reference on type TITLE*
}
LEN *lenght(TITLE *title){ //this line must stay as it is
LEN *len = (LEN*)malloc(sizeof(LEN));
len->x=strlen(level->name);
len->y=strlen(level->city);
return len;
}
答案 0 :(得分:0)
如何在函数中返回TITLE *和LEN *类型的引用。
TITLE *part_struct(char *line) { //this line must stay as it is
TITLE *title = (TITLE*)malloc(sizeof(TITLE));
...
return(title);
}
LEN *lenght(TITLE *title){ //this line must stay as it is
LEN *len = (LEN*)malloc(sizeof(LEN));
...
return(len);
}
如何将填充结构从一个函数传递到另一个函数(通过main)
int main(){
char line[]="Thomas,Bukurest";
TITLE *title = NULL;
LEN *len = NULL;
title=part_struct(line); //this function must return reference on type TITLE*
//when is structure filled with data I need to pass them to another function
len = lenght(title); //does not compile and should return reference on type LEN*
return 0;
}