Thread: Passing Structure by Reference

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    26

    Question Declaring Typedef Struct

    I know that declaring a struct using typedef is in this format:

    Code:
    typedef struct {
        int i;
        char a;
    } StructType;
    Then followed by

    Code:
    StructType Struct[100];
    My question is, what if you only want one instance of Struct, instead of 100, as shown above? It seems like you can't simply omit the [100]. Or am I wrong?

    P.S. Please bear with my because my terminologies may be incorrect.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Did you even try? You're wrong. Omit the [100] and see what happens. Also, Struct is perhaps the worst variable name I have ever seen. It's one shift key away from being a key word, and conveys no information about what the variable actually contains. StructType is not much better. I'm sure that's just for example code, but stick with something a little more recognizable as such, like "foo/footype", "something/sometype", etc.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    26
    Uh wow. 'Struct' was merely for example's sake. Of course I don't intend to use that in an actual code.

    I've tried omitting the [100] in a program I'm writing, and I get this type of error messages:

    error: request for member ‘User’ in something not a structure or union

    And here are the snippets of code directly involved with the struct in question:

    Code:
    typedef struct {    int UserNum;
        char User[31];
        char Pass[31];
        char Role[14];
    } DATATYPE_Login;
    
    DATATYPE_Login STRUCT_Login;

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Sorry to jump on you about naming. I didn't mean it to be harsh. I assumed you were fairly new to this given your question (you still may be), and many noobs don't know better. Plus, given the problem you were having, a missed shift key could very well be the culprit. Also, you didn't post any code that showed you using the variable, just declaring it. The declaration, without [100] is fine. FYI, it's usually pointless to post a paraphrase of the code that's producing a problem. Post the actual code and actual error messages, with line numbers. I guarantee you that error you posted had a line number. That line is not shown in the code you posted, so I can't say for sure. I would guess it's an issue of using . versus -> for member access. The first is for structs/unions, the second is for pointers to structs/unions. If that doesn't solve it, post your full code.

    EDIT: Maybe it's not pointless, but it's usually better to post the full code from the outset, save us going back and forth so much.
    Last edited by anduril462; 03-23-2012 at 12:35 PM.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    26
    Alright, I'll post the lines that the compiler said were erroneous. There are quite a lot of them, so I'll post a few samples to enable you to see the trend.

    Code:
    line 162: scanf("%s", STRUCT_Login.User);
    line 185: if (strcmp(STRUCT_Users[i].User, STRUCT_Login.User) == 0)
    line 219: strcpy(STRUCT_Login.Role, STRUCT_Users[STRUCT_Login.UserNum].Role);
    line 229: "  What would you like to do?\n\n", STRUCT_Login.User, STRUCT_Login.Role);
    And here's the declaration of STRUCT_Users (it's parallel to STRUCT_Login):
    Code:
    typedef struct { char User[31]; char Pass[31]; char Role[14]; } DATATYPE_Users; DATATYPE_Users STRUCT_Users;

    Last edited by Jyqft; 03-23-2012 at 12:49 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Half the time you're using STRUCT_Users as an array, half the time you're not. Make up your mind. You need to post the full code if you want more help. By full code, I mean everything I need to compile your program and get exactly the same error messages on exactly the same lines. It's really hard to help you with little snippets like this. They're completely out of context, which makes it difficult for me to tell if you're doing anything wrong. Also, it appears STRUCT_Users and STRUCT_Login are globals, which come with a whole slew of problems on their own. Read this: Global Variables Are Bad. Then, move them to main and pass them around as needed.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    26

    Question Passing Structure by Reference

    Okay, so this is how I declared my structure

    Code:
    typedef struct { // This is global
        int UserNum;
        char User[31];
        char Pass[31];
        char Role[14];
    } DATATYPE_Login;
    
    int main () {
    
    
    DATATYPE_Login STRUCT_Login; // This is inside main fxn
    
    ...
    
    }
    I have functions that use the structure as a parameter. I'm using a pointer because in this way, I can edit the actual fields of the parameter (rather than edit only a copy). I declared them like this:

    Code:
    int TXTDATA_Read_Rates(DATATYPE_Rates *STRUCT_Rates) {
    ...
    }
    But upon compilation, I get these kinds of error messages:

    Code:
    request for member ‘User’ in something not a structure or union
    If I remove the asterisks, it compiles, but I'm not getting to save the values into the struct fields.

    Help?
    Last edited by Jyqft; 03-23-2012 at 11:32 PM.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    26
    I don't think I explained it well. I've tried explaining my problem better in another topic.

    Here's the URL of the new topic: Passing Structure by Reference

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    One says DATATYPE_Login and the other says DATATYPE_Rates.

    Post a small and complete example, with error messages.
    Snipping out random lines and posting them doesn't work.

    Also, you seem to have multiple threads on the same subject, which is not a good thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jyqft
    I've tried explaining my problem better in another topic.
    The problem in the other topic is pretty much the problem in this one, so I have merged the threads together.

    Quote Originally Posted by Jyqft
    If I remove the asterisks, it compiles
    In other words, if you declare STRUCT_Rates to be a DATATYPE_Rates rather than a pointer to DATATYPE_Rates, it compiles. Looking at the error message, I guess that this means that you accessed STRUCT_Rates.User when you wanted to access STRUCT_Rates->User.

    You should post the smallest and simplest program that you expect to compile but which demonstrates the error. Talking vaguely about "these kinds of error messages" is... vague.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    26
    Code:
    void LOGIN_GetUsername();
    
    typedef struct {
        char OrigCurr[4];
        char WantCurr[4];
        float ExRate;
    } DATATYPE_Rates;
    
    
    typedef struct {
        char User[31];
        char Pass[31];
        char Role[14];
    } DATATYPE_Users;
    
    
    typedef struct {
        int UserNum;
        char User[31];
        char Pass[31];
        char Role[14];
    } DATATYPE_Login;
    
    
    int main() {
        
        DATATYPE_Rates STRUCT_Rates[100];
        DATATYPE_Users STRUCT_Users[100];
        DATATYPE_Login STRUCT_Login;
    
        int COUNT_Struct_Rates, COUNT_Struct_Users;
    
        LOGIN_GetUsername(STRUCT_Users, STRUCT_Login, COUNT_Struct_Users);
    
        ...
    
    }
    
    
    void LOGIN_GetUsername(DATATYPE_Users *STRUCT_Users, DATATYPE_Login *STRUCT_Login, int COUNT_Struct_Users) {
        
        int Done = 0;
        
        while (Done == 0) {
            
            printf("  Username » ");
            scanf("%s", STRUCT_Login.User);
            
            STRUCT_Login.UserNum = LOGIN_GetUserNum(STRUCT_Users, STRUCT_Login, COUNT_Struct_Users);
    
    
            if (STRUCT_Login.UserNum != COUNT_Struct_Users) {
                printf("  ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙\n");
                break;
            }
            else {
                printf("  [Error!] Username is not in the database.\n\n");
                continue;
            }
            
        }
        
    }
    
    
    
    
    int LOGIN_GetUserNum(DATATYPE_Users *STRUCT_Users, DATATYPE_Login *STRUCT_Login, int COUNT_Struct_Users) {
        
        int i;
    
    
        for (i = 0; i < COUNT_Struct_Users; i++) {
            if (strcmp(STRUCT_Users[i].User, STRUCT_Login.User) == 0) {
                break;
            }
            
        }
        
        return i;
        
    }
    And here are the errors:

    Code:
    In function ‘LOGIN_GetUsername’:
    error: request for member ‘User’ in something not a structure or union
    error: request for member ‘UserNum’ in something not a structure or union
    error: request for member ‘UserNum’ in something not a structure or union
    
    
    In function ‘LOGIN_GetUserNum’:
    error: request for member ‘User’ in something not a structure or union

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Indeed, it is exactly what I said:
    Code:
    scanf("%s", STRUCT_Login.User);
    STRUCT_Login is a pointer. Therefore, STRUCT_Login.User does not make sense, since a pointer does not have a member. You should write STRUCT_Login->User or (*STRUCT_Login).User.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    26
    @laserlight: I did what you told me to do. So now it's

    Code:
            scanf("%s", (*STRUCT_Login).User);
    But I get a segmentation fault 11. What's up with that?

    Why is it that for these two structures:

    Code:
        DATATYPE_Rates STRUCT_Rates[100];
        DATATYPE_Users STRUCT_Users[100];
    I'm not having the same problem? I'm also using pointers to pass them as arguments.
    Last edited by Jyqft; 03-24-2012 at 12:39 AM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jyqft
    But I get a segmentation fault 11. What's up with that?
    Let's take a look:
    Code:
    DATATYPE_Login STRUCT_Login;
    Code:
    LOGIN_GetUsername(STRUCT_Users, STRUCT_Login, COUNT_Struct_Users);
    Code:
    void LOGIN_GetUsername(DATATYPE_Users *STRUCT_Users, DATATYPE_Login *STRUCT_Login, int COUNT_Struct_Users)
    So, in main, STRUCT_Login's type is DATATYPE_Login. You pass STRUCT_Login to LOGIN_GetUsername, but LOGIN_GetUsername's STRUCT_Login is a pointer to DATATYPE_Login.

    This is a mistake. The compiler should have warned you about this, and in fact you should have forward declared LOGIN_GetUsername before the definition of main. If you are not compiling with warnings turned on, get into the habit of doing so.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int COUNT_Struct_Rates, COUNT_Struct_Users;
    > LOGIN_GetUsername(STRUCT_Users, STRUCT_Login, COUNT_Struct_Users);
    What value is this (you don't initialise it).

    > for (i = 0; i < COUNT_Struct_Users; i++)
    If you don't initialise it, then this will quite easily run off the end of your array and into the land of segfaults.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structure by reference or pointer?
    By 7force in forum C Programming
    Replies: 8
    Last Post: 12-13-2010, 06:49 PM
  2. Passing structure by reference through 4 functions
    By nickdavid35 in forum C Programming
    Replies: 14
    Last Post: 04-30-2010, 09:39 AM
  3. Problem Passing Structure Reference To Function
    By soj0mq3 in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 10:27 AM
  4. Reference to a Structure
    By boschow in forum C Programming
    Replies: 3
    Last Post: 03-28-2008, 02:37 PM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM

Tags for this Thread