Thread: Passing Pointers by reference

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Passing Pointers by reference

    I know how to simulate passing by reference by using pointers. How does one do this for something that already IS a pointer? For example a C-style string?
    Code:
    int main() {
      char *mystring;
      int myint = 4;
    
      make_my_string(mystring, &myint);
    }
    
    void make_my_string(char *some_string, int *some_int) {
      some_string="hello";
      some_int = 0;
    
    }
    In this example, mystring is still null, and myint is now 0. I want mystring = "hello"

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why would it change? Pass a pointer to the thing you want to be "by reference".

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As I explained in an earlier post, a copy of the variable you pass is made, so if you want to change the original, you must pass the address.
    The type of the pointer is always the type it points to first, and then a *.
    So the type becomes: char**.

    And string literals should be const, because they are non-modifiable.
    And your code is bugged, since it will change the pointer to 0 (which will not even raise a warning, sadly).

    And it's impossible to pass anything by reference in C (I just had to add that one!). It's only possible to pass by address or pointer!
    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.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    #1) Never do this:
    Code:
    void make_my_string(char *some_string, int *some_int) {
      some_string="hello";
    altho you could do this:
    Code:
    void make_my_string(char *some_string, int *some_int) {
      strcpy(some_string,"hello");
    However, in your example mystring does not have any memory space allocated to it! To do that you must use malloc, in main():
    Code:
    int main() {
      char mystring=malloc(6);
    Be sure you understand that this means some_string now points to 6 bytes of memory into which you can store data. If you redirect this pointer, that is, if you use it on the left side of an = equal sign, then you have reassigned it, and those 6 bytes of storage DO NOT come with it!!!!
    Last edited by MK27; 02-13-2009 at 09:36 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's pretty much already summed up: http://apps.sourceforge.net/mediawik...llocating_them
    And if you're going to strcpy, you had better make sure you know the size of the buffer to avoid buffer overruns.
    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.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Post

    On second thought I don't have anything more to say.
    Last edited by MK27; 02-13-2009 at 09:34 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But you're assigning tmp to the local copy of some_string, so that's kinda useless.
    That means you'll have to return a pointer, making the whole char* some_string argument useless.
    And this also means you have to free the pointer, so you can't simply pass in a buffer. And allocating a buffer on the stack vs malloc, especially for small amounts, is much faster, so it's advantages vs disadvantages, which is usually why functions take the buffer as argument, along with its size.
    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
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    But you're assigning tmp to the local copy of some_string, so that's kinda useless.
    That means you'll have to return a pointer, making the whole char* some_string argument useless.
    Sorry, you're right, I don't know how many times I have to get this wrong
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Well you've been very helpful in telling me what was wrong with my little "off the top of my head" example. However you've not helped me understand what I need to do to pass a pointer into a function so that when I modify its value inside the function, the value outside changes as well... I went to Elysia's post on another thread and used her examples and I get about 3 errors/warnings about suspicious pointer conversions.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I did mention it.
    Code:
    int main()
    {
        const char* mystring;
        int myint = 4;
        make_my_string(&mystring, &myint);
    }
    
    void make_my_string(const char** some_string, int* some_int)
    {
        *some_string = "hello";
        *some_int = 0;
    }
    And I have no idea what warnings and things you got.
    The examples were 100% correct, but they were missing including the header, string.h, and a prototype for the called function. Things every C programmer should know about.
    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.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Ah... My bad. I forgot to add the '*' in the body of the function...

    "Thanks Elysia. You're a programming master! How the hell do you know every thing?"

    :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing by Reference. Simple question.
    By d3m105 in forum C Programming
    Replies: 6
    Last Post: 10-31-2007, 12:47 PM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. Passing an array as reference to a function?
    By Kylecito in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2006, 02:25 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM