Thread: Don't understand (int *a, a) etc etc

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    5

    Don't understand (int *a, a) etc etc

    Hello. I'm new to programming in C and the one thing i cannot get my head around is what does things like "int *example(int a, int *b)" mean cannot you jus use "int example(int a, int b)" When a function requires parameters? Does it do this to do additional checks of different types to ensure its receiving the correct type?

    I see alot of this when looking in headers and i wanna get my head around them so i can start using juicy functions :-D

    Please help

    Thank you all

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As I like to write it:
    int* example(int a, int* b)
    They are pointers.
    You should probably study them a bit. Incredibly basic concept in C. And also dangerous.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    int * means pointer to integer data type(s). It could be single value or multi-value of integers.

    http://www.cprogramming.com/tutorial/lesson6.html

    When int * declared at function's parameter (e.g. int example(int a, int *b)) it means passing parameter by reference.

    http://faq.cprogramming.com/cgi-bin/...1043284351#016

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, don't use the term "passed by reference." >_<
    Use passed by pointer instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    References don't exist in C (at least standard C) -- they do in C++ however. References != Pointers.

    Similar but not the same.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    No, don't use the term "passed by reference." >_<
    Use passed by pointer instead.
    It's a commonly used and well-accepted term. I don't see why someone shouldn't use it.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because it's flawed and it conflicts with the C++ term references.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > Because it's flawed

    How is it flawed?

    > and it conflicts with the C++ term references.

    I'd lean toward the side that C++ references conflict with C, since C++ references came after C pass by reference. So maybe the C++ terminology should be changed?

    But I don't see much of a conflict, since they both do the same thing.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by robwhit View Post
    >Because it's flawed
    How is it flawed?
    Because it is not passed by reference.
    If you want to be totally correct, say it's passed by address instead. But I find pass by pointer less confusing.

    >and it conflicts with the C++ term references.
    I'd lean toward the side that C++ references conflict with C, since C++ references came after C pass by reference. So maybe the C++ terminology should be changed?
    You can complain to Bjarne or whoever decided it should be named references. But the point is that they are named such, and should be used by its true name.
    Just like we say pass by reference when we do indeed pass by reference in C++, we can equally say pass by pointer in C instead.
    It's also clearer to what we're really doing.

    But I don't see much of a conflict, since they both do the same thing.
    They are not the same thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > They are not the same thing.

    Right, but they both do the same thing. Any text that requires more specificity as to which mechanism is required would specify such and such as such.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Words and terms are powerful, you know. Choosing the right one can avoid a great deal of confusion.
    Choosing the wrong one can have... bad effects.
    And instead of writing do X in Y way like Z, I'd rather write do X.
    Therefore, references are not pointers and are best separated so.
    Or perhaps the next C standard could implement references the way C++ does. Why they don't have references is actually beyond me since it would save a lot of confusion and pain.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    C has no references, never had them and probably never will.
    Since 1978, at least - and consequently before C++ - the term "pass-by-reference" has been used to describe pointer arguments, can be found on the C ISO document, and is part of the lingo of many people who have been programming in C for more years than you can probably count.

    So... just who do you think you are to change that? And since when references have the exclusive on pass-by-reference? At least, Elysia when you are corrected admit you made a mistake. I have never once saw you done that. And that's what ticks most people off. Your assertions and the fact you stick to them up to the point of it becoming ridiculous, without ever admitting you make mistakes like everybody else... I swear, you do more harm then good with that attitude of yours. And if you think you are being helpful, think again. And you owe aAudine an apology for your mistake.
    Last edited by Mario F.; 07-01-2008 at 07:27 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    You can pass arguments by reference, there is no argument.
    You are passing the address, that's passing by reference.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    We are not talking semantics about what it IS called. That cannot be corrected. I have merely stated it should be called "pass by pointer" rather than "pass by reference" because it is misleading, and I have suggested similar things in the past, as to avoid confusion between C and C++.
    It's already a mess that we currently have two languages and two standards. Why make it worse?
    And for your point about mistakes - I have done many and I have acknowledged them.
    I have not made a mistake in trying to rename or change something to something else here. I have merely suggested.

    However, it is clear that it is going nowhere and there will always be people on both sides of the fence - those who desire pass by pointer and those who desire pass by reference. Clearly this is leading nowhere, and I am therefore inclined to stop right here.

    And when you pass the address, you pass the address, not pass by reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Pass by reference is correct, since this is C. The term is also applicable to assembly, and other languages.

    Pass by reference is a concept, not a syntax. It is c++ that confuses things by making there be two ways to pass a parameter by reference, and only one of them is called a "reference". But again, this is C, so there is no ambiguity.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed