Error with structs and pointers X.x

This is a discussion on Error with structs and pointers X.x within the C Programming forums, part of the General Programming Boards category; The error I'm getting is for is the variables being passed to the input function, the message says: 37 J:\C\Clab6\Clab6.cpp ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Error with structs and pointers X.x

    The error I'm getting is for is the variables being passed to the input function, the message says:

    37 J:\C\Clab6\Clab6.cpp expected primary-expression before '*' token

    In Dev-C++ 4.9.9.2

    Code:
    struct CUSTOMER{
             char name[60];
             char citystate[60];
             char address[60];
             char status;
             int account;
             float preBal;
             float curPay;
             float newBal;
             };        
    
    void input(struct CUSTOMER *strptr);
    float calc(struct CUSTOMER *strptr);
    char status(struct CUSTOMER *strptr);
    float print();
    
    int main()
    {
        struct CUSTOMER st_cust[15];
        struct CUSTOMER *strptr;
        strptr = st_cust;
        char goon = 'y';
        
        while(goon == 'y')
        {
                input(CUSTOMER *strptr);
                print(CUSTOMER *strptr);
                printf("\n\nDo you wish to enter more Accounts? (y/n): ");
                fflush(stdin);
                scanf("%c", &goon);
        }
        
        getchar();
        fflush(stdin);
        return 0;
    }
    
    void input(struct CUSTOMER *strptr)
    {
          int cnt;
          
          printf("How many customers accounts need to be processed?\n");
          scanf("%d", &cnt);
          for(cnt = 0; cnt < strptr; cnt++)
          {
                  printf("Enter Name: ");
                  fflush(stdin);
                  gets(strptr.name);
    
                  printf("Enter Address: ");
                  fflush(stdin);
                  gets(strptr.address);
     
                  printf("Enter City & State: ");
                  fflush(stdin);
                  gets(strptr.citystate);
    
                  printf("Enter Account Number: ");
                  fflush(stdin);
                  scanf("%d", strptr.account);
    
                  printf("Enter Current Balance: ");
                  fflush(stdin);
                  scanf("%f", strptr.preBal);
                  
                  printf("Enter Amount To Pay: ");
                  fflush(stdin);
                  scanf("%f", strptr.curPay);
                  
                  strptr.newBal = calc(struct CUSTOMER *strptr);
                  strptr.status = accstatus(struct CUSTOMER *strptr);
           }
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I didn't check any of the logic of your program but it seems like you are not calling functions correctly.

    Instead of calling f(struct something *ptr) you should call them f(ptr). You don't repeat the argument type during a function call, only in a function header.

    If, for example you had:

    Code:
    int *d;
    
    /* and at some point a function defined as: void function(int *argument) */
    
    /* YOU WOULDN'T CALL IT LIKE: */
    
    function(int *d);
    
    /* BUT INSTEAD */
    
    function(d);

    Also note that fflush(stdin) and streams in general is undefined as far as I know, so it probably won't do what you expect.

    Also, when accessing members of pointers to structs you need to use -> instead of . (i.e. strptr->name).

    Also, your for loop is wrong for so many reasons, the first being: cnt<strptr????? How do you expect this comparison to be done? You are comparing an int to an address.
    Last edited by claudiu; 04-07-2010 at 08:02 AM.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Thumbs up

    Quote Originally Posted by claudiu View Post
    I didn't check any of the logic of your program but it seems like you are not calling functions correctly.

    Instead of calling f(struct something *ptr) you should call them f(ptr). You don't repeat the argument type during a function call, only in a function header.

    If, for example you had:

    Code:
    int *d;
    
    /* and at some point a function defined as: void function(int *argument) */
    
    /* YOU WOULDN'T CALL IT LIKE: */
    
    function(int *d);
    
    /* BUT INSTEAD */
    
    function(d);

    Also note that fflush(stdin) and streams in general is undefined as far as I know, so it probably won't do what you expect.

    Also, when accessing members of pointers to structs you need to use -> instead of . (i.e. strptr->name).

    Also, your for loop is wrong for so many reasons, the first being: cnt<strptr????? How do you expect this comparison to be done? You are comparing an int to an address.
    Thank you claudiu! Saved me a world of trouble. Pointers have been giving me a headache, but you really cleared it up. Same with the function issue! Appreciate you!

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Don't use gets() – It's inherently unsafe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with arrays, pointers, and structs.
    By RexInTheCity in forum C Programming
    Replies: 5
    Last Post: 03-29-2010, 03:30 PM
  2. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  3. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 06:14 PM
  4. pointers to pointers within structs
    By Lord_azrael99 in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 04:29 AM
  5. structs like pointers?
    By mart_man00 in forum C Programming
    Replies: 5
    Last Post: 03-14-2003, 02:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21