Thread: Passing structures... I can't get it right.

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    Passing structures... I can't get it right.

    I'm getting an error in my switch statement:
    switch (menuchoice)
    {

    case 1: addrecord(lname[], fname[], phoneNo[]); break;/*Edit*/

    case 2: deleterecord(lname[], fname[], phoneNo[]); break;/*Edit*/

    case 3: editrecord(lname[], fname[], phoneNo[]); break;/*Edit*/

    case 4: search(lname[], fname[], phoneNo[]); break;/*Edit*/

    case 5: view(lname[], fname[], phoneNo[]); break;/*Edit*/

    case 6: return 0;
    }

    return 0;
    }

    here are my compiler's comments:
    --------------------Configuration: program1 - Win32 Debug--------------------
    Compiling...
    program1.c
    C:\programming\program1.c(43) : error C2059: syntax error : ']'
    C:\programming\program1.c(56) : error C2059: syntax error : ']'
    C:\programming\program1.c(58) : error C2059: syntax error : ']'
    C:\programming\program1.c(60) : error C2059: syntax error : ']'
    C:\programming\program1.c(62) : error C2059: syntax error : ']'
    C:\programming\program1.c(64) : error C2059: syntax error : ']'
    Error executing cl.exe.

    program1.exe - 6 error(s), 0 warning(s)


    What did I do wrong and can you help me out with passing structures I have looked through my 2 books and they are not helping me at all. I included the full program below if you would like to look at the rest of the code. Thanks



    #include <stdio.h>


    char lname[20], fname[15], phoneNo[12];
    int menuchoice, record, i, cnt, totalcnt;
    FILE *infile, *outfile;


    struct record{
    char lname[20];
    char fname[15];
    char phoneNo[12];
    };


    int menu(char *, char *, char *);
    void addrecord(char *, char *, char *);
    void deleterecord(char *, char *, char *);
    void editrecord(char *, char *, char *);
    void search(char *, char *, char *);
    void view(char *, char *, char *);
    void sort(char *, char *, char *);




    int main (void)
    {

    struct record temp;
    char filename[20];

    printf("Enter file name to open");
    scanf("%s", &filename);
    infile=fopen(filename,"r");
    if (infile == NULL){printf("Unable to open file");return(0);}


    while(fscanf(infile, "%s %s %s", &temp.lname[cnt], &temp.fname[cnt], &temp.phoneNo[cnt]) != EOF) cnt++;

    cnt = totalcnt;

    menu(temp.lname[], temp.fname[], temp.phoneNo[]);

    return 0;
    }


    int menu(char lname[], char fname[], char phoneNo[]){
    printf("\nPhonebook menu\n1) Add new record\n2) Delete record\n3) Edit record\n4) Search phone book\n5) View phone book\n6) Quit\nEnter choice #: ");
    scanf("%d", &menuchoice);

    switch (menuchoice)
    {

    case 1: addrecord(lname[], fname[], phoneNo[]); break;/*Edit*/

    case 2: deleterecord(lname[], fname[], phoneNo[]); break;/*Edit*/

    case 3: editrecord(lname[], fname[], phoneNo[]); break;/*Edit*/

    case 4: search(lname[], fname[], phoneNo[]); break;/*Edit*/

    case 5: view(lname[], fname[], phoneNo[]); break;/*Edit*/

    case 6: return 0;
    }

    return 0;
    }



    void addrecord(char *lname, char *fname, char *phoneNo){
    printf("\nAdd Record\n");
    printf("Last Name: ");
    scanf("%s", &lname[totalcnt+1] ); /*Edit*/
    printf("\nFirst Name: ");
    scanf("%s", &fname[totalcnt+1]); /*Edit*/
    printf("\nPhone Number: ");
    scanf("%s", &phoneNo[totalcnt+1]); /*Edit*/
    printf("\nPhone number added");
    ++totalcnt;
    }

    void deleterecord(char *lname, char *fname, char *phoneNo){
    char deletelname[20];

    printf("\nDelete Record\n");
    printf("Last Name: ");
    scanf("%s", &deletelname);
    /*Edit*/
    /*Edit*/
    /*Edit*/


    }

    void editrecord(char *lname, char *fname, char *phoneNo){
    char editlname[20];

    printf("\nEdit Record\n");
    printf("Last name: ");
    scanf("%s", &editlname);
    /*Edit*/
    /*Edit*/
    /*Edit*/


    }

    void search(char *lname, char *fname, char *phoneNo){
    char searchfor[20];

    printf("\nSearch\n");
    printf("Search for: ");
    scanf("%s", &searchfor);
    /*Edit*/
    /*Edit*/
    /*Edit*/

    }

    void view(char *lname, char *fname, char *phoneNo){
    printf("\nView Records\n");
    for (i=0; i < totalcnt; i++)
    {
    printf("\n%s %s %d", lname[i], fname[i], phoneNo[i]); /*Edit*/
    }
    }

    void sort(char *lname, char *fname, char *phoneNo){
    /*Edit*/
    /*Edit*/
    /*Edit*/
    }

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    ...

    You should look here, to see how to pass structs as parameters to functions, http://www.iota-six.co.uk/c/33_struc_s.htm

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Dunno about technical merit, but try looking at this, and see if it can offer anything useful.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct mydata
    {
        float income;
        int bucks;
        char name[20];
    };
    
    void showStuff(const mydata &);
    void storeStuff(mydata &);
    
    int main(void)
    {
        struct mydata junk;
        storeStuff(junk);
        showStuff(junk);
        
         printf("The end");
         return 0;
    }
    
    void showStuff(const mydata &newdata)
    {
       printf("float var: %.2f\n", newdata.income);
       printf("int var: %d\n", newdata.bucks);
       printf("string var: %s\n", newdata.name);
    }
    
    void storeStuff(mydata &newdata)
    {
      newdata.income = 212.5;
      newdata.bucks = 50;
      strcpy(newdata.name, "some name");
    }
    Saving as file.c doesn't like the &, but when creative saving is used - file.cpp my compilers don't seem to protest. :P

    HTH

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    So I need to pass the whole structure as one thing ie. pass record instead of passin it as parts.. lname, fname and phoneNo.. right?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Right, but you also have some syntax problems. Look:

    char * A(char * s){ return s; }
    char * B(char []){ return s; } //...same as above func

    int main(){
    char data[100];
    char * d = A(data);
    char *d2 = B(data);
    char * c = A(&data[0]);
    }

    So when a function expects an array, just pass the name of the array, or else pass the address of the first element.

    Now structs:


    Struct * A(Struct * s){ return s; }

    int main(){
    Struct a;
    Struct * b = A(&a);
    Struct * c = A(b);
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Saving as file.c doesn't like the &, but when creative saving is
    >used - file.cpp my compilers don't seem to protest. :P

    Your code is no C-code, but C++.

    If you have a structure like this:

    struct mydata record;

    And you want to pass only the name, you can pass it like this:

    array_only (record.name);

    Where array_only is a function declared as:

    void array_only (char *name_str);

    If you want to pass a whole structure to a function to get info from it you can pass it like this:

    print_structure (record);

    Where print_structure is a function declared as:

    void print_structure (struct mydata record);

    If you want to adapt a structure in a function, you must pass a pointer to that structure to the function:

    adapt_structure (&record);

    Where adapt_structure is declared as:

    void adapt_structure (struct mydata *record);

    Note that when a pointer is passed, you cannot access the members like this:

    variable = record.bucks;

    But you have to do it with the -> operator, like this:

    variable = record->bucks;

    Not that this notation is the same as:

    variable = (*record).bucks;

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    I got it working thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing An Array Of Structures To A Function
    By nexusdarkblue in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 07:24 AM
  2. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 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. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM