Reputation: 29
For an assignment, I have to make a grade book in C consisting of several interlocking functions. I've gotten through the first few without (too many) headaches, but then there's this thing. Basically, I'm trying to take a character string and copy it into a two-dimensional array inside of a structure. I think I'm on the cusp of success, but the strcpy
keeps throwing these two charming fellow in my face:
passing argument 1 of
strcpy
makes pointer from integer without a castexpected 'char *restrict' but argument is of type 'char'.
Here's my code. gb
is a pointer pointing to the structure. If I haven't clarified anything enough, please ask.
int add_assignment(Gradebook *gb, const char assgn[MAX_NUMBER_OF_ASSIGNMENTS]) {
int i, j;
for(i=0; i < MAX_NUMBER_OF_ASSIGNMENTS; i++) {
for(j=0; j < (MAX_NAME_LEN+1); j++) {
strcpy(gb->assignment_names[i][j], assgn);
}
}
return 1;
}
Here is the Gradebook structure:
typedef struct gradebook {
int number_of_students;
Students students[MAX_NUMBER_OF_STUDENTS];
int number_of_assignments;
char assignment_names[MAX_NUMBER_OF_ASSIGNMENTS][MAX_NAME_LEN + 1];
int scores[MAX_NUMBER_OF_STUDENTS][MAX_NUMBER_OF_ASSIGNMENTS];
} Gradebook;
Upvotes: 2
Views: 296
Reputation: 4873
The problem is in the use of two loops:
for(i=0; i < MAX_NUMBER_OF_ASSIGNMENTS; i++) {
for(j=0; j < (MAX_NAME_LEN+1); j++) {
strcpy(gb->assignment_names[i][j], assgn);
}
}
The inner loop is trying to copy a string (assgn
) into a single char
. You probably meant
for(i=0; i < MAX_NUMBER_OF_ASSIGNMENTS; i++) {
strcpy(gb->assignment_names[i], assgn);
}
instead, but this may not be what you want either, where one assignment overwrites the entire list of assignments. It's hard to tell what logic you are looking for.
Upvotes: 2
Reputation: 781731
assignment_names[i][j]
is a single character, not a string. assignment_names[i]
is a character array, so you should copy to that. You don't need the inner loop.
int add_assignment(Gradebook *gb, const char assgn[]) {
int i;
for(i=0; i < MAX_NUMBER_OF_ASSIGNMENTS; i++) {
strcpy(gb->assignment_names[i], assgn);
}
return 1;
}
Upvotes: 1