Reputation: 148
I'm trying to wite a code that would create an array of structures. I'm still at the early stages where I am trying to pass the structure to a function and though I figured pointer would have to be utitlized, I just can't figure out how to do it the correctly. Here's my code below.
#include <stdio.h>
struct age{
int num;
};
struct name{
char fname [15];
char lname[15];
struct age nameAge;
};
void input(struct name *info[]);
int main (void){
char choice;
char ans;
int i;
struct name record[10];
do{
printf("M E N U");
printf("\n\n[I]nput\n[D]isplay\n[S]earch\n[Q]uit");
printf("\n\nEnter choice: ");
choice=tolower(getche());
system("cls");
switch (choice){
case 'i': input(&record[]);
i++;
break;
case 'd': //function here
break;
case 's': //fucntion here
break;
case 'q': printf("The program will now close.");
break;
default: printf("Invalid character. Try again");
break;
}
getch();
system("cls");
}while (choice!='q');
return 0;
}
void input(struct name *info[]){
//codes here
}
Upvotes: 1
Views: 72
Reputation: 1313
another suggestion for you....
use a record counter.... - you have to call the routine for each time you want to add a record. (I have editted the code so you send the routine a pointer to a single record structure, rather than pass the whole structure).
#include <stdio.h>
struct age{
int num;
};
struct name{
char fname [15];
char lname[15];
struct age nameAge;
};
// void input(struct name *info[]);
void input(struct name *info);
int main (void){
char choice;
char ans;
int i;
int n_records=0;
struct name record[10];
do{
printf("M E N U");
printf("\n\n[I]nput\n[D]isplay\n[S]earch\n[Q]uit");
printf("\n\nEnter choice: ");
choice=tolower(getche());
system("cls");
switch (choice){
case 'i': input(&record[n_records]);
i++;
nrecords++;
break;
case 'd': //function here
break;
case 's': //fucntion here
break;
case 'q': printf("The program will now close.");
break;
default: printf("Invalid character. Try again");
break;
}
getch();
system("cls");
}while (choice!='q');
return 0;
}
// void input(struct name *info[]){
void input(struct name *info){
//codes here
}
Upvotes: 1
Reputation: 1722
You don't want to do this void input(struct name *info[]);
for your input function. You want to pass a reference to one of the info structs in the array to the function since you only will ever edit one info at a time.
So your altered function would be void input(struct name *info);
and you would call it using input(&record[i]);
which gives the address of record i. You also need to initialize i as you never set it to 0, but incitement it.
Upvotes: 1
Reputation: 20252
Keep in mind that The name of an array "decays" to a pointer to its first element. So, in your case, just use record
to pass the "array" (You can't really pass an array) to the function. You also need to change the function argument type to struct info*
as record
is of type struct info*
.
Here is the working code with the changes commented:
#include <stdio.h>
struct age{
int num;
};
struct name{
char fname[15];
char lname[15];
struct age nameAge;
};
/*void input(struct name *info[]); Note the change in the declaration */
void input(struct name *info);
int main (void){
char choice;
char ans;
int i;
struct name record[10];
do{
printf("M E N U");
printf("\n\n[I]nput\n[D]isplay\n[S]earch\n[Q]uit");
printf("\n\nEnter choice: ");
choice=tolower(getche());
system("cls");
switch (choice){
case 'i': input(record); /* Note the change here as well */
i++;
break;
case 'd': //function here
break;
case 's': //fucntion here
break;
case 'q': printf("The program will now close.");
break;
default: printf("Invalid character. Try again");
break;
}
getch();
system("cls");
}while (choice!='q');
return 0;
}
void input(struct name *info){ /* Note the change here in the function definition too */
//codes here
}
Upvotes: 2
Reputation: 942
Change the function definition and implementation and the function call like this (if you want to use arrays)
void input(struct name info[]);
...
input(record);
Or using pointer (also works with arrays as arguments)
void input(struct name *info);
...
input(record);
Upvotes: 1