Structure pass-by-reference - help?

This is a discussion on Structure pass-by-reference - help? within the C Programming forums, part of the General Programming Boards category; I'm writing a structure pass-by-reference example, but somehow I'm coming up with this error... any idea? Linking... LIBCD.lib(crt0.obj) : error ...

  1. #1
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283

    Structure pass-by-reference - help?

    I'm writing a structure pass-by-reference example, but somehow I'm coming up with this error... any idea?

    Linking...
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/Win32 Console Practice.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    Win32 Console Practice.exe - 2 error(s), 0 warning(s)


    Code:
    #include <stdio.h>
    
    struct Common
    {
    	char *name;
    	int age;
    };
    
    void pass(struct Common *pPC);
    
    int main(void)
    {
    	struct Common PC;
    	PC.name = "Phil";
    	PC.age = 23;
    
    	pass(&PC);
    
    	printf("%s", PC.name);
    	printf("%d", PC.age);
    
    	return 0;
    }
    
    void pass(struct Common *pPC)
    {
    	pPC->name = "Dude";
    	pPC->age = 24;
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    C has no pass-by-reference it is purly pass-by-value. However your problem appears to be related to an invalid setting in your Project.

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by orbitz
    C has no pass-by-reference it is purly pass-by-value. However your problem appears to be related to an invalid setting in your Project.
    I did'nt know this concept where it is written can you please post a link or tutorial about this.

    Your code seems correct to me it should work.There is some linking error in this.May be the standard lib is not present.Try doing it on some other compiler

  4. #4
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    C has no pass-by-reference it is purly pass-by-value. However your problem appears to be related to an invalid setting in your Project.
    TheFunc ( &valAddr )
    That depends on how one defines pass-by-reference I suppose, a value does still get pushed onto the stack, but it's the effective address.
    Last edited by valis; 08-20-2005 at 02:07 PM.

  5. #5
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    In C, you can pass by reference. This is mentioned in C++: The Complete Reference in the C section. The address gets pushed onto the stack. I made a new project, pasted the code, and it compiled without a problem. I'm guessing something in the first project got corrupted. As I thought, the code looked fine. Thanks anyway.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >In C, you can pass by reference.
    The C standard makes no mention of passing by reference. You can fake it by passing a pointer, which is what the Bullschildt you've been reading was poorly explaining, but that's not true pass-by-reference, it's pass-by-value using a pointer as the value.

    >This is mentioned in C++: The Complete Reference in the C section.
    Take everything Herbie says with a grain of salt. He's usually wrong.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Ok, if you are correct in stating that you can pass by reference in C then this should be simple: Show me the reference. If you think a pointer is a reference then you need to show me where that connection is made in the C Standard, because I don't see it. People seem to really want to beleive C can pass-by-reference and fight hard to make some weak connection between pointers and references. C is a pass-by-value language, get used to it.

  8. #8
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    If it does the trick, then it does the job. It is still pass-by-reference because you're pointing to an address, not a value. By the way, I always thought he said by reference, but in the book, he's discussing how to pass the address, which he describes as structure pointers. The code is working now after making a new project, so you guys can chat all you want. Thanks for the help.
    Last edited by dxfoo; 08-20-2005 at 02:35 PM.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Based on what - your reading of a book which isn't about C, by an author who doesn't know C?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    He was a member of the ANSI/ISO committee that standardized C++. After selling millions of books internationally, I think he deserves respect. He teaches the language, not feed you bull........ that you see every day on message boards.

    You guys need to study more. When a pointer to a structure is passed to a function, only the address of the structure is pushed on the stack. This makes for very fast function calls. A second advantage, in some cases, is when a function needs to reference the actual structure used as the arguement, instead of a copy. By passing a pointer, the function can modify the contents of the structure used in the call. Same as a reference!

    You guys argue over the dumbest things on message boards. No wonder why professionals I know say to stay away from them. No one in the field cares about listening to the most misinformed BS coming from a young generation online. Bye!
    Last edited by dxfoo; 08-20-2005 at 02:57 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dxfoo
    He was a member of the ANSI/ISO committee that standardized C++. After selling millions of books internationally, I think he deserves respect. He teaches the language, not feed you bull........ that you see every day on message boards.

    You guys need to study more. When a pointer to a structure is passed to a function, only the address of the structure is pushed on the stack. This makes for very fast function calls. A second advantage, in some cases, is when a function needs to reference the actual structure used as the arguement, instead of a copy. By passing a pointer, the function can modify the contents of the structure used in the call. Same as a reference! You guys argue over the dumbest things on message boards. No wonder why professionals stay away from them. There's no educational value in it.
    1) Schildt is a horrible author. Don't get your feelings all hurt because we don't like an author of a book you own. It's not an attack on you. No one cares if you bought a book and who wrote it. People mention the fact that he's a horrible author because he is in fact a horrible author. Go do a search on your favourite search engine for "schildt" and "bad book".

    2) You need to study more. A pointer is a variable which passes the value of the address to a function. It is not a reference. It is the value of an address. It is a copy of the address. That's why it's still "passed by value". The value of the address is passed. Not a reference. There is a difference. You need to study an author who knows what they're talking about.

    It is mistakenly referred to as "by reference", however, C has no "by reference" feature. It is simulated using pointers. However, it is by value, and no matter how much you wish it wasn't, it isn't going to change the fact.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >He was a member of the ANSI/ISO committee that standardized C++.
    There's a difference between an active member and an observer. Schildt was an observer, and not a very good one judging from his mistakes.

    >After selling millions of books internationally, I think he deserves respect.
    He deserves a slap in the face. It doesn't matter how many books you sell if they're all crap. Herbert Schildt is well known for being able to explain incorrect concepts in an easy to understand way. We've been correcting his students for years, so don't expect any respect for him around here.

    >He teaches the language, not feed you bull........ that you see every day on message boards.
    We're not forcing you to post here. If you trust your book more than us, then go read your book and leave us alone.

    >You guys need to study more.
    You're mixed up, it's the other way around.

    >You guys argue over the dumbest things on message boards.
    The difference between an expert and everyone else is that the expert understands the nuances while everyone else just settles for "close enough".

    >No wonder why professionals stay away from them.
    Quite a few of us are professionals.
    My best code is written with the delete key.

  13. #13
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    EDIT EDIT EDIT EDIT EDITE DITE ESJREGJHGWKHWGWG: I'm stupid.

    Okay. I was also interested in this, so I did a little debugging.

    void a(int& x) { x += 2; }
    int main() { int b = 2; a(b); }

    Disassembly:
    Code:
    // main()
    00401088  |. C745 FC 020000>MOV DWORD PTR SS:[EBP-4],2
    0040108F  |. 8D45 FC        LEA EAX,DWORD PTR SS:[EBP-4]
    00401092  |. 50             PUSH EAX
    00401093  |. E8 77FFFFFF    CALL test.0040100F
    // a()
    00401038  |. 8B45 08        MOV EAX,DWORD PTR SS:[EBP+8]
    0040103B  |. 8B08           MOV ECX,DWORD PTR DS:[EAX]
    0040103D  |. 83C1 02        ADD ECX,2
    00401040  |. 8B55 08        MOV EDX,DWORD PTR SS:[EBP+8]
    00401043  |. 890A           MOV DWORD PTR DS:[EDX],ECX
    void a(int* x) { *x += 2; }
    int main() { int b = 2; a(&b); }

    Disassembly:
    Code:
    // main()
    00401078  |. C745 FC 020000>MOV DWORD PTR SS:[EBP-4],2
    0040107F  |. 8D45 FC        LEA EAX,DWORD PTR SS:[EBP-4]
    00401082  |. 50             PUSH EAX
    00401083  |. E8 87FFFFFF    CALL test.0040100F
    // a()
    00401038  |. 8B45 08        MOV EAX,DWORD PTR SS:[EBP+8]
    0040103B  |. 8B08           MOV ECX,DWORD PTR DS:[EAX]
    0040103D  |. 83C1 02        ADD ECX,2
    00401040  |. 8B55 08        MOV EDX,DWORD PTR SS:[EBP+8]
    00401043  |. 890A           MOV DWORD PTR DS:[EDX],ECX
    So, my findings conclude that they are exactly the same, and that I didn't accidentally copy and paste the same thing twice. Both simply push a "reference" (the effective address) of our number 2 and modify that accordingly. The pass-by-refeference vs. faked pointer stuff seems to be only a higher level implementation to let you use '.' and not derenference the pointer on every use. I could and probably am wrong in saying this, and my compile (VC++) might have made bad assumptions. I don't know.

    Edit: Dearly sorry for the edit's but gcc gives the same results. And compiles the pass by reference. "meh"
    Last edited by Tonto; 08-20-2005 at 05:32 PM.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    I think we should just let the microsoft apologist get on with being a bullschilter.

    > He was a member of the ANSI/ISO committee that standardized C++
    *looks at forum - it says C*
    Nope - not impressed by that argument at all - same amount of credibility as if he'd been on say the pascal committee.

    > You guys argue over the dumbest things on message boards.
    Because the detail always bites you in the ass eventually.

    > misinformed BS coming from a young generation online. Bye!
    LOL - there are quite a few people here who've got a lot of years over you. If you want to extrapolate the universe from one bad book, then that's your problem.
    Oh, you're leaving - nevermind then.

    > So, my findings conclude that they are exactly the same
    Well that's what you get when you mix up language concepts (pointers and references) with what an implementation does with them.
    It's the same argument as saying pointers and arrays are the same thing - they're not, though they do have some similarities. Sure you can get away with thinking they're the same, but sooner or later you'll come unstuck (for example, most peoples first attempt at passing a 2D array to a function usually ends up with ** in there somewhere).
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    From the creator himself.

    In C, all function arguments are passed "by value". This means that the called function is given the values of its arguments in temporary variables rather than the originals
    It's in The C programming language.

Page 1 of 2 12 LastLast
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, 05:49 PM
  2. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  5. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 05:56 AM

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