strcpy

Hello, I'm in a beginners programming class, and I have been staring at this code for hours. I can't get the strcpy to work, and cannot figure out why.

#include<stdio.h>

typedef struct
{
char name;
short score;

} Student_record;

Student_record students[20];

int main()
{
int ctr = 0;
int num;
int n = 0;
char first[20];

FILE * iPtr;

iPtr = fopen("input.txt", "r");

if (iPtr == NULL)
{
printf("File did not read");
return 0;
}

while (feof(iPtr) == 0)
{
fscanf(iPtr, "%s", first);
fscanf(iPtr, "%d", &num);
students[ctr].score = num;
strcpy(students[ctr].name, first);
ctr++;
}

fclose(iPtr);

while (n < ctr)
{
printf("%d \n", students[n].score);
//printf("%s %d\n", students[n].name, students[n].score);
n++;
}
printf("press any key to end");
getch();
return 0;
}
Because this

strcpy(students[ctr].name, first);

'name' is 1 char and 'first' is an array of chars. You need to declare 'name' the same way you did with 'first'
Thank you. Saved me another 2 hours of staring at it. Never would have thought of it. :-/
Topic archived. No new replies allowed.