Thread: Questions regarding pointers

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    1

    Questions regarding pointers

    I am still learning C programming and had a question regarding pointers. If I create a pointer in main and assign a value, then pass the pointer to another function, should it contain the same address? That is, if I print the address in main, then print the address in the second function, should the addresses be the same?

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    5
    When you have a variable such as

    Code:
    int var = 1;
    and you pass it to a function

    Code:
    function(var);
    A copy of the value stored inside var is passed to function. In this case the integer 1.

    If instead you were to pass a pointer to var to function

    Code:
    int var = 1;
    int *ptr = &var;
    
    function(ptr);
    A copy of the value stored inside ptr is passed to function. In this case the address of var.

    So if inside main you had

    Code:
    printf("%p\n", &var); // could also use ptr
    and inside function you had

    Code:
    printf("%p\n", ptr);
    You would see they would print the same addresses which is to be expected.

    Hope this helps.
    Last edited by wbpribs; 05-30-2021 at 06:58 PM.

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    19
    What wbpribs said. But also, the address passed to your function is a copy of the address held in the pointer variable you created in main(). So if your function updates the pointer copy, the original pointer will not be changed.

    Pointers are the core of C, where all the power lies, but they are also where most newbies come unstuck. Use pointers, don't steer clear of them. But learn what they are and how they're used, or they will bite you, hard. Unit-testing/TDD/TFD will probably help in trying things out, and confirming they work as you think they do/should.

    Good luck with C - the old ones are still the best!

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,608
    Be careful when printing with printf(), using the wrong type of variable for the format specifier can invoke undefined behavior. The "%p" specifier expects a void * so you need to cast your pointers to this type:

    Code:
    int var = 1;
    int *ptr = &var;
    printf("%p\n", (void*)&var); 
    printf("%p\n", (void*)ptr);

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    5
    Quote Originally Posted by jimblumberg View Post
    Be careful when printing with printf(), using the wrong type of variable for the format specifier can invoke undefined behavior. The "%p" specifier expects a void * so you need to cast your pointers to this type:

    Code:
    int var = 1;
    int *ptr = &var;
    printf("%p\n", (void*)&var); 
    printf("%p\n", (void*)ptr);
    Thanks, I forgot about the cast.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some questions with pointers
    By kirchhoff in forum C Programming
    Replies: 5
    Last Post: 11-26-2012, 09:07 PM
  2. Two questions about pointers
    By Dynesclan in forum C Programming
    Replies: 4
    Last Post: 06-10-2012, 11:55 AM
  3. A few questions on pointers
    By dgs012 in forum C Programming
    Replies: 1
    Last Post: 12-01-2010, 09:53 AM
  4. questions about pointers
    By radxxx in forum C Programming
    Replies: 3
    Last Post: 04-21-2009, 11:38 AM
  5. Some questions about pointers
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2002, 02:25 PM

Tags for this Thread