Thread: New to Structures; compilation error

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    20

    New to Structures; compilation error

    Hi all,
    I'm a novice C programmer and am trying to write a program that reads in a file (which describes menus shown on a certain application). Eventually I'll make the program write out a report of the contents. At the moment I'm not very au fait with structures and I'm getting the compilation error
    "Type mismatch in parameter '__dest' in call to strcpy".

    I'll post the code below. Many thanks for any help/advice.
    Code:
     main()
      {
       char buf1[5479];
       char menubuf[5479];
       int comp01;
    
     struct  menuDetail {
    	char MenuDes0[25];
    	char  filler01[06];
            char MenuNam0[06];
    	char  filler02[02];
            char MenuTyp0[01];
    	char  filler03[04];
            char  MenuSec0[02];
    	char filler04[452];
    	char MenuDes1[25];
    <snip>
            char MenuDes10[25];
    	char  filler41[06];
            char MenuNam10[06];
    	char  filler42[02];
            char MenuTyp10[01];
    	char  filler43[04];
            char  MenuSec10[02];
    	char filler44[452];
    };
    
    struct menuDetail *menuRec;
    
       FILE *fpin1, *fpout2;
    
       if ((fpin1=fopen("bm7000.men","r"))==NULL)
        {
         printf("Error input file \n");
         exit (1);
        }
       if ((fpout2=fopen("menulst.txt","w"))==NULL)
        {
         printf("Error output file \n");
         exit (1);
        }
      
       while (fgets(buf1,5478,fpin1)!= NULL)
      {
        strcpy(*menuRec,buf1);    ***   ERROR ON THIS LINE   ***
        strncpy((*menuRec).MenuDes0,&menubuf[0],25);
        (*menuRec).MenuDes0[26]='\0';
        strncpy((*menuRec).MenuNam0,&menubuf[32],06);
        (*menuRec).MenuNam0[06]='\0';
      }
    
      endpart:
      ;

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    menuRec is ALREADY a pointer to a char array. Just drop the '*'.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    What are you trying to copy into into menuRec?

    you need to refer to an char* element of the array, as you do on the following line....

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    20
    I'm not too hot on pointers either! OK, I removed the * and instead of an error I got a warning "Suspicious pointer conversion".

    What I'm trying to do is take the data read in to buf1 and move it to the structure, so I can access the individual fields within it.

    Thanks...

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    That's because it's now convering a char* to an array of char * (your structure)

    When you refer to your strucure like that, you are refering to the first element in your structure, it's probably best if you refer to the elements by name - for example, it wouldn't work if you added an int to the start of yor structure

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    20
    Relooked at my code. Sorry, menubuf was superfluous. Changed code to this...
    Code:
    /*    strcpy(menuRec,buf1);   */
        strncpy((*menuRec).MenuDes0,&buf1[0],25);
        (*menuRec).MenuDes0[26]='\0';
        strncpy((*menuRec).MenuNam0,&buf1[32],06);
        (*menuRec).MenuNam0[06]='\0';
    It now compiles cleanly but
    Code:
    fprintf(fpout2," %s %s \n",(*menuRec).MenuDes0,(*menuRec).MenuNam0);
    is printing out blank.

    This could be because my input file isn't laid out quite how I thought, so I'll double check that. However, if you could let me know if I'm at least doing it correctly in the program that would be great!

    Thanks guys...
    Last edited by emanresu; 06-30-2005 at 09:32 AM.

  7. #7
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I don't see where you are allocating memory for menuDetail.

    BTW, ever hear of 'arrow notation'? It is used for pointers.

    fprintf(fpout2," %s %s \n",(*menuRec).MenuDes0,(*menuRec).MenuNam0);

    becomes

    fprintf(fpout2," %s %s \n", menuRec->MenuDes0, menuRec->MenuNam0);

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    You can also use ->

    eg
    Code:
    fprintf(fpout2," %s %s \n",menuRec->MenuDes0,menuRec->MenuNam0);

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    Quote Originally Posted by mitakeet
    BTW, ever hear of 'arrow notation'? It is used for pointers.

    fprintf(fpout2," %s %s \n",(*menuRec).MenuDes0,(*menuRec).MenuNam0);

    becomes

    fprintf(fpout2," %s %s \n", menuRec->MenuDes0, menuRec->MenuNam0);
    You beat me!

    If you drop the pointer (*) on where you declare MenuDes, and go back to using the . operator without dereferencing it, you should be OK......

    ie
    Code:
    fprintf(fpout2," %s %s \n",menuRec.MenuDes0,menuRec.MenuNam0);
    Which might have been what you were trying to achieve in the first place!
    Last edited by chambece; 06-30-2005 at 09:52 AM.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    20
    Looks neater doesn't it (arrow notation)! I'll use it from now on.

    menuDetail is the tag (I think!) for the structure. i.e. "struct menuDetail {...".
    Is its memory not allocated by the items within it?

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    Quote Originally Posted by emanresu
    Looks neater doesn't it (arrow notation)! I'll use it from now on.

    menuDetail is the tag (I think!) for the structure. i.e. "struct menuDetail {...".
    Is its memory not allocated by the items within it?
    Yes, it is, but you only have a pointer to one, not an actual one

    Code:
    struct menuDetail *menuRecPtr;  // ponter
    struct menuDetail menuRec; // real one!
    If you use this, you'll have to go back to using the . operator, as in my previous post

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    20

    Thumbs up

    OK, I now have
    Code:
     struct  menuDetail {
    	char MenuDes0[25];
    	char  filler01[06];
            char MenuNam0[06];
    	<snip>
    };
    
    struct menuDetail menuRec;
    
       FILE *fpin1, *fpout2;
    
       if ((fpin1=fopen("bm7000.men","r"))==NULL)
        {
         printf("Error input file \n");
         exit (1);
        }
       if ((fpout2=fopen("menulst.txt","w"))==NULL)
        {
         printf("Error output file \n");
         exit (1);
        }
      
       while (fgets(buf1,5478,fpin1)!= NULL)
      {
        strncpy(menuRec.MenuDes0,&buf1[0],25);
        menuRec.MenuDes0[26]='\0';
        strncpy(menuRec.MenuNam0,&buf1[32],06);
        menuRec.MenuNam0[06]='\0';
      }
    
      endpart:
      ;
    
      fprintf(fpout2," %s %s \n",menuRec.MenuDes0,menuRec.MenuNam0);
    It compiles successfully, and once I took rubbish out of input file it worked great! Thanks for all your help guys!!
    Last edited by emanresu; 06-30-2005 at 10:13 AM.

  13. #13
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You do not need to NULL terminate after you use strncpy, it will do that for you (which is why it is critical to leave enough space for the NULL).

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM