Pointer-to-Pointer

This is a discussion on Pointer-to-Pointer within the C Programming forums, part of the General Programming Boards category; Ok, I really appreciate the help I have received on the forums here. Now I'm coming from a Java background ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    42

    Pointer-to-Pointer

    Ok, I really appreciate the help I have received on the forums here. Now I'm coming from a Java background and trying to learn C. Now one thing that I'm starting to get a feel of are pointers...however when I see a pointer-to-pointer I start to get confused. I am curious what are the advantages of using a pointer-to-pointer? I appreciate any help on understanding this concept.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want to change a pointer's address, in a called function in your program, you need to send that function a pointer to your pointer.

    Without it, you will send a copy of your pointer, which you can change in your function, but when your function returns - bingo! The pointer NOW will be unchanged, because the pointer you changed in your function was just a COPY, and not the real thing.

    Probably the best way to understand it, is to make up a tiny little example program, for yourself. See how the pointer you pass is just a copy, and it's values will terminate when the function returns. Then try it with a pointer to a pointer, and see how it doesn't terminate at all.

    Don't be afraid to make little experiments like this - curiosity with C, is an excellent character trait to have, imo.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    Thanks, I will do that. I have a list of a few experiments I want to do this weekend.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    I am not a big fan of Java, since in my experience it's difficult to transition into C, where we need to know for damn sure the difference between a pointer and actual memory. In Java, of course, if we have a class Foo, we can say Foo f = new Foo() and Foo g = f; We note that f sort of "owns" the memory of the actual object, and g also points at the object. But neither f or g are actually the object. They are references TO the object. In fact, they are pointers to the object. They hold the address of the object. Which is exactly what pointers in C are: addresses of the first byte in memory of whatever object.

    The difficulty with pointers in my experience has been how do we interpret them? Suppose I have
    Code:
    char* str = "Hello, World!";
    char* str2 = str;
    char* str3 = &str[4]; // or str + 4 if you prefer
    How do I interpret this? I will often say str is a string holding "Hello, World!". In the strict sense, that's not true, it's only a valid interpretation of the fact that str is a byte address to the first character in the "string" (more properly a contiguous block of memory), which is 'H'. I think of str2 not as a string but as a marker marching along the string, because if I declare another pointer pointing at the same thing, I stop thinking "this is the object" and start thinking "this is a marker." A valid interpretation, but not correct in the strict sense. Finally, what is str3? I think of str3 as "the substring 5 characters into str." So then str3 can be THOUGHT of as ", World!" But it is not. If str = 0x450f0040, then str3 = 0x450f0044. That's all these are in the strict sense: byte addresses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a function pointer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 05-30-2010, 05:13 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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