-
variable passing problem
Here is was I am trying todo I am calling a function within a function and passing to it a few things an array, a couple of ints and a string I am getting an error on the string when I call it from the function and try to place it in a struct I have include the function I am having problems and the struct. The error I am getting is incompatible types in assignment. If anyone can explain my error to me because I can't find it I would be very greatful.
I *'ed the lines that were popping up as errors
Thanks
Code:
//struct to hold information from file
typedef struct{
int id;
char name[50];
int age;
}record_t;
the function
Code:
//inserts items into array of structs
//Pre-Condition: fileptr points to a file and table
//is an empty array
//Post-Condition: table has element added as long as
//table is already full
void inserttable(int id, char name[50], int age, int firstloc,
record_t table[41])
{
int index;
index = firstloc;
if (table[index].id == -1){
table[index].id = id;
table[index].name = name;*
table[index].age = (int)age;
}else{
index = (index+1)%41;
while(index != firstloc || table[index].id != -1){
index = (index+1)%41;
}
if (index == firstloc){
printf("Unable to add item array full.\n\n");
}else{
table[index].id = id;
table[index].name = name;*
table[index].age = (int)age;
}
}
}
-
You can't assign strings using the = operator. Strings aren't single values but an array of characters. Use strcpy() to copy a string.
Also, I'd suggest you change char name[50] in your argument list into char* name to pass a pointer to the string (4 bytes) instead of passing the whole string (50 bytes).
-
>>I'd suggest you change char name[50] in your argument list into char* name to pass a pointer to the string (4 bytes) instead of passing the whole string (50 bytes).
If your refering to this:
>void inserttable(int id, char name[50], int age, int firstloc, record_t table[41])
it wont make any difference. When you're talking arrays, you only pass a pointer to the array, not the array itself.
-
*Making a test program*
You're right. My fault...