Thread: Simple Structure program

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    13

    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:
    Code:
    char blah[5];
    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);
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    wilddata can only access wilddata[0] to wilddata[23]

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Increasing the declaration to
    Code:
    wr wildata[25];
    It should take care of that, but it won't take care of the earlier warning, will it?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dopejack View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and why to encript your code?

    Code:
    wr wilddata[5][5];
    
    wilddata[x][y].symbol
    is a lot more simplier to read
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    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.
    Code:
    ^"%,
    ÄDÜčō
    
    etc...
    Thanks for the point to tutorials that actually made sense.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Changing lines 8 and 28 to:
    Code:
    char symbol[2];
    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?

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Once again, another step in the right direction and my thanks. Line 8 became
    Code:
    char symbol;
    and now my map returned:

    Code:
    01234
    56789
    abcde
    fghi
    I upped the size of wilddatato
    Code:
    wilddata[6][6]
    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?

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I'm rather surprised that even works as well as it does, to be honest.

    Code:
    printf("&#37;s",wilddata[x][y].symbol);
    This should be with a %c.

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    My mistake. It already is %c. Been changed previously. Any other guesses?

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    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("&#37;c",wilddata[x][y].symbol);
    		}
    		/*printf("\n");
    		*/
    	}
    			
    	fclose(minimap);
    	
    	return(1);
    }
    This is the file I used:

    Code:
    01234
    56789
    abcde
    fghij
    klmno

  15. #15
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 03:19 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. Help with small simple program
    By Sonor in forum C Programming
    Replies: 5
    Last Post: 04-02-2005, 07:40 AM
  5. Simple program structure.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-15-2002, 04:36 AM