Thread: help with structures

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    13

    Question help with structures

    I am working on a project using structures and when I compile it says " error: request for member `fName' in something not a structure or union" and it says the same thing for the different elements that I am trying to access. My code so far is:
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #define SIZE 30
       
     typedef  struct   //student records
    {
       char fName[20];
       char lName[20];
       int gLevel;
       char ssn[11];
       int month;
       int day;
       int year;
    }STUDENT;
    
    void fillStruct(char** argv,STUDENT* sarr[],int num);
    void getFile(char** argv,STUDENT* sarr[]);
    void sortFile(STUDENT* sarr[]);
    void srchSort(STUDENT* pstud[],int argc);
    void prntFile(STUDENT* sarr[],int num);
    void swap(int *num1, int *num2);
    void search(int arr[]);
    int  binSrch(int arr[], int item, int lb, int ub);
             
    int main(int argc,char *argv[])
    {
       STUDENT* stud[SIZE];
       int num;
       if(argc==3)
         { fillStruct(argv,stud,num);
           prntFile(stud, num);
         }
          
             
      return 0;
    }
    
    void prntFile(STUDENT* sarr[],int num)
    {
       int i;
       char name[40];
       char bday[11];
             
         printf("\n%30s%7s%15s%13s","Name","Level","SSN","Birthdate");
         printf("\n%30s%7s%15s%13s","----","-----","---","---------");
         for(i=0;i<num;i++)
         {
             strcpy(name,sarr[i].fName); //put name on a sinle line
             strcat(name," ");
             strcat(name,sarr[i].lName);
     
             strcpy(bday,sarr[i].month); //put birthday on a single line
             strcat(bday,"/");
             strcat(bday,sarr[i].day);
             strcat(bday,"/");
             strcat(bday,sarr[i].year);
     
             printf("\n%30s%7d%15s%13s",name,sarr[i].level,sarr[i].ssn,bday);
         }
         printf("\n\n");
       
       
    }
    If anyone could please tell me what I am doing wrong I would greatly appreciate it.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't be confused.
    void prntFile(STUDENT* sarr[],int num)
    sarr is an array of POINTERS (essentially, STUDENT**). So when you do sarr[n], you get STUDENT*, not STUDENT, therefore you must use -> syntax.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    13
    I was able to get rid of that error by having a different function call it after filling it, but now that has caused a different problem. the code I changed is:
    Code:
    int main(int argc,char *argv[])
    {
       STUDENT* stud[SIZE];
       int num;
       if(argc==3)
         { fillStruct(argv,stud,num);
    
         }
        
        
      return 0;
    }  
    void fillStruct(char** argv,STUDENT* sarr[],int num)
    {  int i;
             
     num=atoi(argv[1]);
     if(num<SIZE)
       {for(i=0; i<num; i++)
          {
            getFile(argv, &sarr[i]);
            prntFile(sarr[i]);
           }
       }
             
     
    }
    
    void prntFile(STUDENT stud) 
    {
       int i;
       char name[40];
       char bday[11];
             
         printf("\n&#37;30s%7s%15s%13s","Name","Level","SSN","Birthdate");
         printf("\n%30s%7s%15s%13s","----","-----","---","---------");
       
                 
             strcpy(name,stud.fName); //put name on a sinle line
             strcat(name," ");
             strcat(name,stud.lName);
        
             strcpy(bday,stud.month); //put birthday on a single line
             strcat(bday,"/");
             strcat(bday,stud.day);
             strcat(bday,"/");
             strcat(bday,stud.year);
     
             printf("\n%30s%7d%15s%13s",name,stud.level,stud.ssn,bday);
     
         printf("\n\n");
       
       
    }
    now the error that is coming up is "In function `fillStruct':
    proj5a.c:45: error: incompatible type for argument 1 of `prntFile'"
    and " In function `prntFile':
    proj5a.c:88: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast"

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    sarr[i] is still a STUDENT *, not a STUDENT.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    13
    ok, i thought I understood what you were saying but I don't know how to fix it, with out causing more errors.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The best way to "fix it" is to have prntFile() take a pointer to a STUDENT
    Code:
    void prntFile(STUDENT *stud)
    and in that function, instead of using this,
    Code:
    strcpy(name,stud.fName);
    use (*stud).fName or stud->fName.

    That's assuming your original declaration is correct. You might want this instead.
    Code:
    STUDENT stud[SIZE];
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM