[英]Nested structures and pointers in C
The compiler gives two complaints: 编译器有两个抱怨:
error: incompatible type for argument 1 of 'printout'
错误:“打印输出”的参数1的类型不兼容
warning: expected struct users * but argument is of type 'users'
警告:预期的结构用户*但参数的类型为'users'
How do I fix these? 我该如何解决? And please check if the function
printout
is OK. 并且请检查功能
printout
是否正常。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char username[25];
char firstname[25];
char lastname[25];
int age;
}users;
typedef struct{
users data;
int id;
}publicusers;
users createuser(char *uname, char *fname, char *lname, int fage);
void printout(users *data);
int main(){
printout(createuser("kway","minseon","huh", 25));
return 0;
}
users createuser(char *uname, char *fname, char *lname, int fage){
publicusers user1;
strcpy(user1.data.username, uname);
strcpy(user1.data.firstname, fname);
strcpy(user1.data.lastname, lname);
user1.data.age = fage;
return user1.data;
}
void printout(users *data){
printf("username: %s\n",data->username);
printf("firstname: %s\n",data->firstname);
printf("lastname: %s\n",data->lastname);
printf("age: %d\n",data->age);
}
Your createuser
function returns the users
type and your function printout
accepts users*
as its argument which is a pointer to users
. 您的
createuser
函数返回users
类型,并且函数printout
users*
作为其参数,这是指向users
的指针。
Because you are declaring your struct publicusers user1
inside your createuser
function you cannot send its address as a return value. 因为要在
createuser
函数中声明struct publicusers user1
,所以不能将其地址作为返回值发送。 The fast solution is to change the function definition from: 快速的解决方案是将函数定义从以下位置更改:
void printout(users *data);
void printout(users *data){
printf("username: %s\n",data->username);
printf("firstname: %s\n",data->firstname);
printf("lastname: %s\n",data->lastname);
printf("age: %d\n",data->age);
}
to 至
void printout(users data);
void printout(users data){
printf("username: %s\n",data.username);
printf("firstname: %s\n",data.firstname);
printf("lastname: %s\n",data.lastname);
printf("age: %d\n",data.age);
}
This will create a copy of the struct users
. 这将创建struct
users
的副本。 If you want to avoid creating a copy you should consider creating publicusers user1
in your main and send it as an extra argument to the users createuser
function. 如果要避免创建副本,则应考虑在主目录中创建
publicusers user1
并将其作为附加参数发送给users createuser
函数。
Your warning and error messages are generated from the following line: 您的警告和错误消息是从以下行生成的:
printout(createuser("kway","minseon","huh", 25));
Function printout
expects user *
type value as its argument but function createuser
returns user
type value. 函数
printout
期望user *
类型值作为其参数,但函数createuser
返回user
类型值。
To fix this, you should pass user *
type value as parameter using &
operator. 要解决此问题,您应该使用
&
运算符将user *
类型值作为参数传递。 Change above one line code into two lines of code: 将以上一行代码更改为两行代码:
users u = createuser("kway","minseon","huh", 25);
printout(&u);
There's another way to fix this. 还有另一种解决方法。 You can change the parameter type of function
printout
from user *
to user
. 您可以将功能
printout
的参数类型从user *
更改为user
。
Your code should be: 您的代码应为:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char username[25];
char firstname[25];
char lastname[25];
int age;
}users;
typedef struct{
users data;
int id;
}publicusers;
users createuser(char *uname, char *fname, char *lname, int fage);
void printout(users data); // parameter type changed
int main(){
printout(createuser("kway","minseon","huh", 25));
return 0;
}
users createuser(char *uname, char *fname, char *lname, int fage){
publicusers user1;
strcpy(user1.data.username, uname);
strcpy(user1.data.firstname, fname);
strcpy(user1.data.lastname, lname);
user1.data.age = fage;
return user1.data;
}
void printout(users data){ // parameter type changed
printf("username: %s\n",data.username); // -> operator changed to . operator
printf("firstname: %s\n",data.firstname); // -> operator changed to . operator
printf("lastname: %s\n",data.lastname); // -> operator changed to . operator
printf("age: %d\n",data.age); // -> operator changed to . operator
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.