Passing Pointers???

This is a discussion on Passing Pointers??? within the C Programming forums, part of the General Programming Boards category; Code: void test(char *testptr) { testptr = "hello world"; // 2. GDB says tesptr has value: 0x80487cc "hello world" } ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    Passing Pointers???

    Code:
    void test(char *testptr)
    {
    	testptr = "hello world";
    	
    	// 2. GDB says tesptr has value: 0x80487cc "hello world"
    }
    
    int main()
    {
    	char *testptr;
    
    	// 1. GDB says testptr has value: 0x8006d0 and a bunch of crap: U\211aWVSeo\224
    
    	test(testptr);
    	
    	// 3. GDB says testptr again has value 0x8006d0 and a bunch of crap: U\211aWVSeo\224
    }
    Why is the pointer not keeping its value????

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Don't think I understand the question; what value are you referring to?

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    To retain the value you either have to return the pointer from the function, or pass it by reference. You also arent allocating any space to hold your string; this is a bad thing btw.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Don't think I understand the question; what value are you referring to?
    testptr

    I pass it to function. It gets the correct value.

    Back in main it has crap value.

    Why doesn't it retain the value set in the function?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by mike_g View Post
    To retain the value you either have to return the pointer from the function, or pass it by reference. You also arent allocating any space to hold your string; this is a bad thing btw.
    How do I pass it by reference?

    I tried test( &testptr ) but that gives a warning: passing argument 1 of \u2018test\u2019 from incompatible pointer type

    And still doesn't save the value either =\
    Last edited by Paul22000; 02-16-2009 at 04:50 PM.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Heres how you pass by reference:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX_LEN 50
    
    void test(char **testptr)
    {
    	strncpy(*testptr, "goodbye world", MAX_LEN);
    }
    
    int main()
    {
        char* testptr = (char*)malloc(MAX_LEN+1); 
        strncpy(testptr, "hello world", MAX_LEN);
        printf("%s\n", testptr);
    
        test(&testptr);
        printf("%s\n", testptr);	
        
        free(testptr);
    
    }
    Theres some extra stuff in the code that you should get your head around if you want to work with strings in C. Basically allocating/freeing space and making sure you dont get buffer overflows.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Thank you! The extra * in void test(char **testptr) along with the & in main was what I needed! Perfect!

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,434
    Code:
    void test(const char** testptr)
    {
    	*testptr = "hello world";
    }
    
    int main()
    {
    	const char* testptr;
    	test(&testptr);
    }
    Pointer Passing Question
    String literals should be const char
    Last edited by Elysia; 02-17-2009 at 08:59 AM.
    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.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Elysia - you forgot the & in test(&testptr);

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,434
    Yes, silly me. I just copied and pasted >_<
    Phew. Thanks for the correction.
    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
    Anytime

  12. #12
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    I have always had trouble with pointer to pointers. Especially when using a char.

    So I tried it without the pointer to pointer. However, I got printed out a load of rubbish in my printf statement.
    Code:
    void test(const char *testptr)
    {
        testptr = "hello world";
    }
    
    int main(void)
    {
        const char *testptr;
        test(testptr);
    
        printf("testptr: %s\n", testptr);
    
        return 0;
    }
    I understand that a pointer to pointer is a pointer that points to another pointer.

    So here you are passing the address of the pointer:
    test(&testptr)

    So here you are dereferencing the pointer and assigning a string.
    *testptr = "hello world";

    Which basically is like doing this, declaring and initializing a pointer to a string
    char *mytest = "hello world";

    But why would you need a pointer to pointer. Can't the same thing be done without one.

    Correct me if I am wrong with the above examples.

    Just one more question. Why have you got const
    const char *testptr.

    Is this so that the pointer cannot point to another datatype i.e. int, float, etc.

    Many thanks correcting my understanding.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    It has to be done in this case because you are passing the pointer to a function which is always by value...

    here's a link that may be useful...

    http://www.augustcouncil.com/~tgibson/tutorial/ptr.html

    When you pass the pointer to the function, like you did originally, the compiler creates a COPY of that pointer. So any chages made to this copy will not change the original. When you pass the address of the original pointer into the function, any changes you make are now being made to the original address and therefore to the original pointer.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You could just use a pointer. But if you did so, the change to testptr would not be seen in main. Nothing passed in to a function can be changed, but you can use the address to change what is pointed to.

    And const just means that the chars cannot be changed. "hello world" is a pointer to constant characters, so that's the type we use.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,434
    Quote Originally Posted by steve1_rm View Post
    But why would you need a pointer to pointer. Can't the same thing be done without one.
    Because of the reason I explained in the link I posted. Did you not read it or did you not understand it?
    And no, it can't be done without.

    Just one more question. Why have you got const
    const char *testptr.
    Is this so that the pointer cannot point to another datatype i.e. int, float, etc.
    Again, because of the reason explained on the wiki link I posted. Did you not read it or did you not understand it?
    It's so that you won't accidentally modify the contents of what the pointer points to.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a string array to a function using pointers
    By asofaihp in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 11:31 AM
  2. Passing Pointers by reference
    By Bladactania in forum C Programming
    Replies: 10
    Last Post: 02-13-2009, 09:14 AM
  3. passing pointers outside of functions
    By thomas41546 in forum C Programming
    Replies: 6
    Last Post: 01-26-2008, 05:00 PM
  4. Passing pointers
    By cyberCLoWn in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2004, 12:17 PM
  5. Pointers and reference passing
    By Denis Itchy in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 12:36 AM

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