-
Simple Structure program
I wrote this very simple program to make sure I understood arrays of structures and file i/o. When I comple, I get an error on line 28 that states 'passing arg 2 to function strcpy create pointer without cast). I has gotten this error in a simpler version (sans structures) and was rid of it by declaring the function as:
Now, however, the same fix isn't working. Any insight would be appreciated. The program is meant to read in a text file of 5 rows, each with 5 symbols. It creates a structure with their symbol and location, and then spits it back out.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct wildroom{
int x;
int y;
char symbol[1];
} wr;
int main(){
FILE *minimap;
wr wilddata[24];
int x,y;
minimap = fopen("./minimap.txt","r");
if(minimap==NULL)
{
printf("Error in File Read\n");
exit(0);
}
for(x=0;x<5;x++){
for(y=0;y<5;y++){
wilddata[(x*5)+y].x=x;
wilddata[(x*5)+y].y=y;
strcpy(wilddata[(x*5)+y].symbol,fgetc(minimap));
}
}
for(x=0;x<5;x++){
for(y=0;y<5;y++){
printf("%s",wilddata[(x*5)+y].symbol);
}
printf("\n");
}
fclose(minimap);
return(1);
}
-
wilddata can only access wilddata[0] to wilddata[23]
-
Increasing the declaration to
It should take care of that, but it won't take care of the earlier warning, will it?
-
Quote:
Originally Posted by
dopejack
Code:
for(x=0;x<5;x++){
for(y=0;y<5;y++){
wilddata[(x*5)+y].x=x;
wilddata[(x*5)+y].y=y;
strcpy(wilddata[(x*5)+y].symbol,fgetc(minimap));
}
}
for(x=0;x<5;x++){
for(y=0;y<5;y++){
printf("%s",wilddata[(x*5)+y].symbol);
}
printf("\n");
}
Suggest you don't do things like that because it's confusing and prone to bugs and errors. Use two-dimensional arrays instead.
And remember that if you define something as wr wildata[24], you get 24 elements, ranging from 0 to 23, and not 1 to 24.
-
and why to encript your code?
Code:
wr wilddata[5][5];
wilddata[x][y].symbol
is a lot more simplier to read
-
-
Thanks for the help. I think I'm mopving in the right direction, since for the first time, I'm actually spitting back a 5x5 block of text. The code has changed to this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct wildroom{
int x;
int y;
char symbol[1];
} wr;
int main(){
FILE *minimap;
wr wilddata[5][5];
int x,y;
minimap = fopen("./minimap.txt","r");
if(minimap==NULL)
{
printf("Error in File Read\n");
exit(0);
}
for(x=0;x<5;x++){
for(y=0;y<5;y++){
wilddata[x][y].x=x;
wilddata[x][y].y=y;
fgets(wilddata[x][y].symbol, 1, minimap);
}
}
for(x=0;x<5;x++){
for(y=0;y<5;y++){
printf("%s",wilddata[x][y].symbol);
}
printf("\n");
}
fclose(minimap);
return(1);
}
However, it returns 5 lines of 5 garbage characters each.
Thanks for the point to tutorials that actually made sense.
-
Use fgetc() to read a char. Using fgets() to read a char is going to screw you up. Also, don't declare a char array with a size of one. That doesn't allow you space for a '\0', and it's just asking for trouble.
-
Changing lines 8 and 28 to:
and
Code:
wilddata[x][y].symbol=fgetc(minimap);
respectively, garners me an error:
Code:
struct.c: 28: error: incompatible types in assignment
*boggle* What am I missing here? Why do I have trouble assigning one variable of type char to another?
-
Because symbol is an array. You're trying to assign a char to a char array.
If symbol was a char, it would work.
If you want to keep it as an array, then you would either read the char into symbol[0], or more appropriately, you would use fgets().
TBH, I think you should change symbol to a char (instead of a char array), unless you really need it to be an array.
-
Once again, another step in the right direction and my thanks. Line 8 became
and now my map returned:
Code:
01234
56789
abcde
fghi
I upped the size of wilddatato
and increased each of my loops accordingly. That netted:
Code:
01234
56789
abcde
fghij
klmno˙˙˙
For clarification, the minimap file looks like this:
Code:
01234
56789
abcde
fghij
klmno
Anyone on why I appear to get 19 chars back the first time and
28 back later? Are the ends of the lines being counted as unseen characters?
-
I'm rather surprised that even works as well as it does, to be honest.
Code:
printf("%s",wilddata[x][y].symbol);
This should be with a %c.
-
My mistake. It already is %c. Been changed previously. Any other guesses?
-
Well, with your changes it works for me, except it prints an extra '\n' for each line, which you are correct, is from the file.
This is what I tested:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct wildroom{
int x;
int y;
char symbol;
} wr;
int main(){
FILE *minimap;
wr wilddata[6][6];
int x,y;
minimap = fopen("./minimap.txt","r");
if(minimap==NULL)
{
printf("Error in File Read\n");
exit(0);
}
for(x=0;x<6;x++){
for(y=0;y<6;y++){
wilddata[x][y].x=x;
wilddata[x][y].y=y;
wilddata[x][y].symbol = fgetc(minimap);
}
}
for(x=0;x<6;x++){
for(y=0;y<6;y++){
printf("%c",wilddata[x][y].symbol);
}
/*printf("\n");
*/
}
fclose(minimap);
return(1);
}
This is the file I used:
Code:
01234
56789
abcde
fghij
klmno
-
I'm using cygwin, so who knows. I've seen it be buggy before. Thanks. Tremendous confidence boost that it's at least working for SOMEONE. ;)