Passing by Reference. Simple question.

This is a discussion on Passing by Reference. Simple question. within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm stuck. Like everyone else posting in here I guess Anyway, what I'm trying to do is really ...

  1. #1
    Registered User d3m105's Avatar
    Join Date
    Oct 2007
    Location
    Mauritius
    Posts
    3

    Angry Passing by Reference. Simple question.

    Hey guys,

    I'm stuck. Like everyone else posting in here I guess

    Anyway, what I'm trying to do is really simple. I have a struct, and a function to modify any struct of the same type passed as param. That's the issue here... I know that I should be using pointers, but my app just refuses to compile!

    I'm using the default C compiler that comes with Dev C++ 4.9.9.0.
    Here goes my code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    struct Test
    {
        int a;
        int b;  
    };    
    
    int main()
    {
    
        //create a test structure and assign values
        struct Test myTest;
        myTest.a = 10;
        myTest.b = -50;
        
        //Modify the test structure
        ModifyStruct(&myTest);
        
        //Display values
        printf("%d\t%d", myTest.a, myTest.b);
        getch();
    
        //End program
        return 0;
    }    
    
    //Function to modify a structure
    void ModifyStruct(struct Test * someTest)
    {
        someTest.a = 10;
        someTest.b = 10;
    }
    This is insane! I get a few errors, but it all seems to be because of:

    32 C:\...\test.c request for member `a' in something not a structure or union
    33 C:\...\test.c request for member `b' in something not a structure or union



    Help!
    I would appreciate an example with reference parameter passing with structs

    -Rowan.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void ModifyStruct(struct Test * someTest)
    {
        someTest.a = 10;
        someTest.b = 10;
    }
    The red part declares your argument as a pointer.

    The blue code tries to use the pointer as a struct.

    You need to change it to use '->' instead of '.'

    Or you could use the more obscure (*sometest).a etc. [But this is more typing and less readable, in my opinion].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User d3m105's Avatar
    Join Date
    Oct 2007
    Location
    Mauritius
    Posts
    3

    Wink It Works!

    Hey there, matsp!

    Thank you! I tried the code. It works!!!

    Thank you so much man

    For anybody who'd like to see the full code, here goes:


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    struct Test
    {
        int a;
        int b;  
    };    
    
    int main()
    {
    
        //create a test structure and assign values
        struct Test myTest;
        myTest.a = 10;
        myTest.b = -50;
        
        //Modify the test structure
        ModifyStruct(&myTest);
        
        //Display values
        printf("%d\t%d", myTest.a, myTest.b);
        getch();
    
        //End program
        return 0;
    }    
    
    //Function to modify a structure
    void ModifyStruct(struct Test * someTest)
    {
        someTest->a = 10;
        someTest->b = 10;
    }
    And yes, I think -> looks better

    -Rowan.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    You're passing by a pointer, not reference. References are only possible in C++.
    Remember that you need to add * to dereference a pointer (and obj->a is basically another syntax for (*obj).a).

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311
    It's really a matter of terminology.

    Passing by a pointer is a way of "passing by reference" as opposed to "pass by value" because you can modify what the pointer points to. You can't modify the pointer itself, though, as that value is passed by value. If you wanted to modify the address of a pointer you'd have to pass a pointer to a pointer.

    Pass by reference is a general-use term that can apply to any programming language and does not refer to the C++ data type reference.
    GCC on Fedora Core 10

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    Well, I'd rather say pass by pointer because that's what you're doing in sense of the language. It's called a pointer, so it would be pass by pointer.
    Ah well.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Technically, C has only one passing mechanism: By value. If you want to use a "reference", the way to do that is to pass the value of a pointer to the data you want to refer to.

    You may say that arrays are passed by reference, but really, technically, they are just passing the value of the pointer to the first element.

    I agree that "pass by pointer" makes somewhat more sense than "pass by reference", especially since the latter IS a term used in C++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  2. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  3. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  4. question about .net to 6.0 change causing errors
    By jverkoey in forum C++ Programming
    Replies: 17
    Last Post: 03-23-2004, 09:45 AM
  5. question about passing arg in constructor??
    By smd in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2003, 07:31 PM

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