c - Typedef struct and passing to functions -


i'm trying declare typedef struct array , pass function i'm getting errors because i'm not proper syntax, appreciated. here code:

#include <stdlib.h> #include <stdio.h>  #define max_courses 50  typedef struct courses      //creating struct 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 course id: ");  //prompting info     scanf("%d%*c", cors.course_id);     printf("enter name of course: ");     scanf("%s%*c", cors.course_name);      cors_count++;   //adding count      printf("%p\n", cors_count);     return; } 

the errors i'm getting are:

error: incompatible type argument 1 of ‘add_course’

test2.c:28:6: note: expected ‘course’ argument of type ‘struct course *’

test2.c: in function ‘add_course’:

test2.c:81:2: warning: format ‘%d’ expects argument of type ‘int *’, argument 2 has type ‘int’ [-wformat]

any appreciated

you passing array function expecting instance of struct course, try this

add_course(cors[cors_count], &cors_count); 

but modified in add_course need

void add_course(course *cors, int *cors_count) {     printf("enter course id: ");     /* wrong, pass address of `course_id' */     scanf("%d%*c", &cors->course_id);     /* also, check return value `scanf' */     printf("enter name of course: ");     scanf("%s%*c", cors->course_name);      /* need dereference pointer here */     (*cors_count)++; /* incrementing pointer */      return; } 

and can

for (int index = 0 ; index < max_courses ; ++index)     add_course(&cors[index], &cors_count); 

although cors_count equal max_courses - 1 in case, migh useful after all.


Comments