[英]Typedef struct and passing to functions
I'm trying to declare a typedef struct array and then pass it to a function but i'm getting errors because i'm not exactly certain the proper syntax, help would be greatly appreciated. 我试图声明一个typedef结构数组,然后将其传递给一个函数,但由于我不确定确切的语法,因此出现了错误,将不胜感激。 Here is my code:
这是我的代码:
#include <stdlib.h>
#include <stdio.h>
#define MAX_COURSES 50
typedef struct courses //creating struct for course info
{
int Course_Count;
int Course_ID;
char Course_Name[40];
}course;
void Add_Course(course , int *);
int main()
{
course cors[MAX_COURSES];
int cors_count = 0;
Add_Course(cors, &cors_count);
return 0;
}
void Add_Course(course cors, int *cors_count)
{
printf("Enter a Course ID: "); //prompting for info
scanf("%d%*c", cors.Course_ID);
printf("Enter the name of the Course: ");
scanf("%s%*c", cors.Course_Name);
cors_count++; //adding to count
printf("%p\n", cors_count);
return;
}
The errors i'm getting are: 我得到的错误是:
error: incompatible type for argument 1 of 'Add_Course'
错误:“ Add_Course”的参数1的类型不兼容
test2.c:28:6: note: expected 'course' but argument is of type 'struct course *'
test2.c:28:6:注意:预期的“课程”,但参数的类型为“结构课程*”
test2.c: In function 'Add_Course':
test2.c:在函数“ Add_Course”中:
test2.c:81:2: warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat]
test2.c:81:2:警告:格式'%d'期望类型为'int *'的参数,但是参数2的类型为'int'[-Wformat]
Any help would be appreciated 任何帮助,将不胜感激
You are passing an array to a function expecting an instance of struct course
, try like this 您正在将数组传递给需要
struct course
实例的struct course
,请尝试这样
Add_Course(cors[cors_count], &cors_count);
But then it will only be modified in Add_Course
so you need 但是,这只会在
Add_Course
进行修改,因此您需要
void Add_Course(course *cors, int *cors_count)
{
printf("Enter a Course ID: ");
/* this was wrong, pass the address of `Course_ID' */
scanf("%d%*c", &cors->Course_ID);
/* Also, check the return value from `scanf' */
printf("Enter the name of the Course: ");
scanf("%s%*c", cors->Course_Name);
/* You need to dereference the pointer here */
(*cors_count)++; /* it was only incrementing the pointer */
return;
}
And now you can 现在你可以
for (int index = 0 ; index < MAX_COURSES ; ++index)
Add_Course(&cors[index], &cors_count);
although cors_count
will be equal to MAX_COURSES - 1
in this case, it migh be useful after all. 虽然
cors_count
将等于MAX_COURSES - 1
在这种情况下,migh终究是有用的。
In your Add_Course()
function, the first argument is of type course
but what you're passing is an array of type course
which are not the same. 在您的
Add_Course()
函数中,第一个参数是course
类型的,但是您要传递的是一个不相同的course
类型的数组。 You need to have a pointer to course
as the first argument if you want to pass an array. 如果要传递数组,则需要有一个指向
course
的指针作为第一个参数。
Next, scanf("%d%*c", cors.Course_ID);
接下来,
scanf("%d%*c", cors.Course_ID);
is also wrong, scanf()
expects the argument to the format specifier as a pointer to the variable. 这也是错误的,
scanf()
期望格式说明符的参数作为变量的指针。 You need to provide the address of the variable. 您需要提供变量的地址。 Also,
printf("%p\\n", cors_count);
另外,
printf("%p\\n", cors_count);
should obviously be printf("%d\\n", *cors_count);
显然应该是
printf("%d\\n", *cors_count);
. 。
That said, cors_count++;
也就是说,
cors_count++;
is not probably what you wanted to have there. 可能不是您想要的东西。 You want to increment the value , not the pointer itself.
您要增加值 ,而不是指针本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.