Reputation: 13
I'm working on a homework assignment for CS1, it's supposed to bring us back up to speed if we had forgotten some of our C (it's mostly about pointers and memory allocation). I've been working on it for well over 15 hours and I am in need of some serious help.
The problem tells us to use a struct as follows:
typedef struct LottoPlayer {
char first[20];
char last[20];
int nums[6];
} KBLP;
We are supposed to read a file and dynamically allocate memory for an array of these structures, and then we can use that information to select winners yada yada. Whenever I ran my code, I kept getting junk back. So this is what I have, I think the issue is my pointer arithmetic or the way I have set up my malloc.
int main() {
//Code to open the files
int NumTickets;
fscanf(infile, "%d", &NumTickets);
KBLP *size=malloc(NumTickets*sizeof(KBLP));
And then, when I'm reading the file and writing it to the array, I do
int index;
int i;
for (index=0; index < NumTickets; index++) {
fscanf(infile, "%s", size[index].last);
fscanf(infile, "%s", size[index].first);
for (i=0; i<6; i++) {
fscanf(infile, "%d", size[index].nums[i]);
}
}
Now, I feel like I'm missing some dumb here, because when I debug and try to access the size[index].first and things of that nature, I get back garbage memory. Am I not allocating it correctly? Am I not writing to it correctly? Or both?
Upvotes: 1
Views: 3928
Reputation: 320481
It is supposed to be
fscanf(infile, "%d", &size[index].nums[i]);
Note the &
operator in the second argument. Of course, the concerns expressed in @missingno's answer are also valid.
The functions in scanf
group expect pointers as arguments for each data recipient. It is your responsibility to apply the &
operator whenever it is necessary. The compiler generally cannot verify whether you are passing a value of proper type, since the trailing arguments of these functions are variadic, i.e. they have no predetermined type. However, some compilers (like GCC) do make efforts to perform the check, meaning that your code would trigger a warning from GCC compiler for the above issue.
Note that when you are passing arguments of array type, the array type automatically "decays" to pointer type, which is why the first two fscanf
s in your code require no application of &
operator. Yet, if you so desire, you can rewrite these fscanf
s as
fscanf(infile, "%s", &size[index].last[0]);
fscanf(infile, "%s", &size[index].first[0]);
Upvotes: 7
Reputation: 2568
first and last populating OK but you're getting garbage in nums?
Hint: fscanf("%d"....) is going to want to populate an integer field. For that it's going to need a pointer. What are you giving it?
Upvotes: 4
Reputation: 69934
Two things come to my mind:
fscanf(infile, "%d", size[index].nums[i])
is missing a &
before size[...
. Are you sure this compiled without warnings?
The fscanf(..., "%s", size[index].last)
can overwrite stuff if your string has more than 19 characters (remember you need an extra char for the terminator). Print your strings to be sure they have the correct lengths.
Upvotes: 4