Code:
/* LINK insert(FILE *ifp)
{
LINK head = NULL;
LINK tail = NULL;
char h[20];
fscanf(ifp,"%s",&h);
while(*h!=0){
tail = create( *h, tail );
fscanf(ifp,"%s",&h);
if( !head ) { head = tail; } /* If head == 0 (ie. no list): */
}
printf("\n");
return head;
} */
I'd suggest you go read the pointer tutorial in the FAQ. There's a few things you'll be having problems with here:
Code:
LINK head = NULL;
LINK tail = NULL;
You've never included the header which contains your node in any of your examples. So is LINK a pointer, or an instance? It had better be a pointer. Otherwise it's just plain messy. I suppose you could get it working, but it still wouldn't be as clean as using pointers here.
We'll assume it's a pointer for this discussion.
Code:
char h[20];
fscanf(ifp,"%s",&h);
while(*h!=0){
How about instead of checking your array (incorrectly I might add), you instead check the return value of fscanf to see if it failed or not?
If you insist on checking the array, consider the following instead:
Code:
char h[20];
fscanf(ifp,"%s",&h);
while( h[0] != 0 ){
Well actually, even that doesn't really work well, because your array to start is never initialized. So it may very well have a bunch of garbage values in it, and as such, even if fscanf fails here, it still may pass that loop.
Code:
char h[20] = {0};
fscanf(ifp,"%s",&h);
while( h[0] != 0 ){
But still, it would be better to just check the return value of fscanf.
Code:
tail = create( *h, tail );
fscanf(ifp,"%s",&h);
if( !head ) { head = tail; } /* If head == 0 (ie. no list): */
}
The above is again wrong, because you're trying to dereference an array. You don't dereference arrays. You access their elements. However, in this case, I believe you want to pass the string (ie: the whole contents of said array) to the function, correct?
In that case, you simply use the name of the array by itself. Not accessing an index of it, not trying to dereference it, simply pass the name along:
Code:
tail = create( h, tail );
fscanf(ifp,"%s",&h);
if( !head ) { head = tail; } /* If head == 0 (ie. no list): */
}
Also, you usually don't pass the address of an array around. Again, you simply use the name of the array like it were a pointer to the first element:
Code:
tail = create( h, tail );
fscanf(ifp,"%s",h);
if( !head ) { head = tail; } /* If head == 0 (ie. no list): */
}
And so, were we to rewrite this so it works a bit smoother:
Code:
while( fscanf( ifp, "%s", h ) == 1 )
{
tail = create( h, tail );
if( !head ) { head = tail; }
}
Now I'd again would really suggest you go read the pointer tutorial. You may also find following link helpful. Also, pay close attention to your compiler's warnings. They're there for a reason.
Quzah.