Thread: Changing adress stored by pointers inside a function in C

  1. #1
    Registered User Mortanius's Avatar
    Join Date
    Jul 2016
    Posts
    7

    Changing adress stored by pointers inside a function in C

    I was trying to change the adresses of two pointers (one by another) instead of changing their values.
    The goal was to do it with arrays, but here's the thing:
    I made it work with pointers, but it kinda didn't work with arrays.
    (Plus: Some values of the arrays are changed, but not their adresses.)
    Could you help me understand why?

    Here are the codes:

    Pointers:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void invert_ad (int** a, int** b)
    {
        int* temp;
        temp = *a;
        *a = *b;
        *b = temp;
    }
    main()
    {
        int a = 10;
        int* pointer_a = &a;
        int** ppointer_a = &pointer_a;
        int b = 20;
        int* pointer_b = &b;
        int** ppointer_b = &pointer_b;
        printf("pointer_a Adress: %p\n", pointer_a);
        printf("pointer_b adress: %p\n", pointer_b);
        invert_ad (ppointer_a, ppointer_b);
        printf("pointer_a adress: %p\n", pointer_a);
        printf("pointer_b adress: %p\n", pointer_b);
    }
    Arrays:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void invert_ad (int** a, int** b)
    {
        int* temp;
        temp = *a;
        *a = *b;
        *b = temp;
    }
    main()
    {
        int array_a[5] = {0, 1, 2, 3, 4};
        int array_b[5] = {5, 6, 7, 8, 9};
        int** parray_a = &array_a;
        int** parray_b = &array_b;
        printf("array_a adress %p\n", array_a);
        printf("array_b adress %p\n", array_b);
        invert_ad (parray_a, parray_b);
        printf("array_a adress %p\n", array_a);
        printf("array_b adress %p\n", array_b);
    }

  2. #2
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    You are confusing a pointer to a pointer with a simple pointer. Try to always define variables for their use not their calling convention usage. For example, changing the parray_a and parray_b from a doulbe pointer to a simple pointer will let you initialize them correctly. Passing the address of the parray_a and parray_B instead of the their values to the invert_ad (which by the way it is better known as swap) it will yield the results you are after. Can you see what is the problem on your code now?
    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    void invert_ad (int** a, int** b)
    {
        int* temp;
        temp = *a;
    
        *a = *b;
        *b = temp;
    }
    main()
    {
        int array_a[5] = {0, 1, 2, 3, 4};
    
        int array_b[5] = {5, 6, 7, 8, 9};
    
        int* parray_a = &array_a;
    
        int* parray_b = &array_b;
    
        printf("array_a adress %p\n", array_a);
    
        printf("array_b adress %p\n", array_b);
    
        invert_ad (&parray_a, &parray_b);
    
        printf("array_a adress %p\n", array_a);
    
        printf("array_b adress %p\n", array_b);
    }

  3. #3
    Registered User Mortanius's Avatar
    Join Date
    Jul 2016
    Posts
    7
    Hey taazz, Thanks for answering.
    Their adress are still the same in this code. And I didn't get this: If an array is also a pointer, why should I use a simple pointer instead of a pointer to a pointer?
    *I realized it's wrong now, but I though that: 'array_a' is a pointer to 'array_a[0]', 'parray_a' has the adress of 'array_a', so: *parray_a should be the adress of 'array_a[0]', and '**parray_a' should be the value of 'array_a[0]'. I don't understand why it is not like this.
    Last edited by Mortanius; 07-10-2016 at 11:37 PM.

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    An array has more info to it than a pointer, hence why sizeof() works on it.
    Last edited by CodeSlapper; 07-10-2016 at 11:40 PM.

  5. #5
    Registered User Mortanius's Avatar
    Join Date
    Jul 2016
    Posts
    7
    Quote Originally Posted by CodeSlapper View Post
    I don't believe you can do the second one since array is more than a pointer, hence why sizeof reports the correct value. My guess is you can change the address of a variable on the stack like that. I could be way off though.
    I believe that an array of integers for example has some extra informations about itself beyond its values. But even so I thought I could, let's say 'realloc' to a space where was previously another array.

  6. #6
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by Mortanius View Post
    Hey taazz, Thanks for answering.
    Their adress are still the same in this code. And I didn't get this: If an array is also a pointer, why should I use a simple pointer instead of a pointer to a pointer?
    An array is not a pointer is a static structure with predefined size, in your case 5 cells * 4 bytes each integer = 20 bytes each. All variables are in memory, either heap or stuck and all variables have a memory address, that does not make them pointers.
    A pointer is a variable which contains a memory address as a value and as a variable it has its own memory address. Now a pointer to a pointer is a bit tricky to explain especially with my limited English skills bat here it goes.

    When you declare a pointer you instruct the compiler to give you a place in memory where you will save data with a size of a pointer. When you declare a pointer to a pointer you instruct the compiler to give you a place in memory where you will put a memory address (aka a pointer) and to remember that the address you just saved also contains a memory address. It does not acquire for you enough enough space to save two pointers, which makes it impossible to initialize correctly with out a pointer variable to begin with. A pointer to a pointer is only a mnemonic for the compiler what to expect later on in the compilation process. In your case by initializing the parray_X variables to the address of your static arrays you instructing the compiler that what ever is found if &array_x address is a pointer to an integer instead of the integer it self. Now your invert_ad function expects to get two memory addresses where pointers are saved and make sure that that adresses swap their values.
    Nothing can change the memory address of static variables array_a, array_b you can only change what parray_X variables point to.

    Quote Originally Posted by Mortanius View Post
    *I realized it's wrong now, but I though that: 'array_a' is a pointer to 'array_a[0]', 'parray_a' has the adress of 'array_a', so: *parray_a should be the adress of 'array_a[0]', and '**parray_a' should be the value of 'array_a[0]'. I don't understand why it is not like this.
    No sorry, array_a is a label that helps you recognise what ever you are accessing the compiler will discard the name array_a and in its place will put the memory address where the data are in memory. This can not be changed after compilation (aka at runtime). If you do not save a memory address in the variable then the variable is not a pointer, it makes no difference if it is typed or untyped even that is a compiler only instruction not a run time restriction.
    Last edited by taazz; 07-11-2016 at 12:04 AM.

  7. #7
    Registered User Mortanius's Avatar
    Join Date
    Jul 2016
    Posts
    7
    Quote Originally Posted by taazz View Post
    An array is not a pointer is a static structure with predefined size, in your case 5 cells * 4 bytes each integer = 20 bytes each. All variables are in memory, either heap or stuck and all variables have a memory address, that does not make them pointers.
    A pointer is a variable which contains a memory address as a value and as a variable it has its own memory address. Now a pointer to a pointer is a bit tricky to explain especially with my limited English skills bat here it goes.

    When you declare a pointer you instruct the compiler to give you a place in memory where you will save data with a size of a pointer. When you declare a pointer to a pointer you instruct the compiler to give you a place in memory where you will put a memory address (aka a pointer) and to remember that the address you just saved also contains a memory address. It does not acquire for you enough enough space to save two pointers, which makes it impossible to initialize correctly with out a pointer variable to begin with. A pointer to a pointer is only a mnemonic for the compiler what to expect later on in the compilation process. In your case by initializing the parray_X variables to the address of your static arrays you instructing the compiler that what ever is found if &array_x address is a pointer to an integer instead of the integer it self. Now your invert_ad function expects to get two memory addresses where pointers are saved and make sure that that adresses swap their values.
    Nothing can change the memory address of static variables array_a, array_b you can only change what parray_X variables point to.

    No sorry, array_a is a label that helps you recognise what ever you are accessing the compiler will discard the name array_a and in its place will put the memory address where the data are in memory. This can not be changed after compilation (aka at runtime). If you do not save a memory address in the variable then the variable is not a pointer, it makes no difference if it is typed or untyped even that is a compiler only instruction not a run time restriction.
    I think I got it, Thanks!

  8. #8
    Registered User Mortanius's Avatar
    Join Date
    Jul 2016
    Posts
    7
    Quote Originally Posted by taazz View Post
    An array is not a pointer is a static structure with predefined size, in your case 5 cells * 4 bytes each integer = 20 bytes each. All variables are in memory, either heap or stuck and all variables have a memory address, that does not make them pointers.
    A pointer is a variable which contains a memory address as a value and as a variable it has its own memory address. Now a pointer to a pointer is a bit tricky to explain especially with my limited English skills bat here it goes.

    When you declare a pointer you instruct the compiler to give you a place in memory where you will save data with a size of a pointer. When you declare a pointer to a pointer you instruct the compiler to give you a place in memory where you will put a memory address (aka a pointer) and to remember that the address you just saved also contains a memory address. It does not acquire for you enough enough space to save two pointers, which makes it impossible to initialize correctly with out a pointer variable to begin with. A pointer to a pointer is only a mnemonic for the compiler what to expect later on in the compilation process. In your case by initializing the parray_X variables to the address of your static arrays you instructing the compiler that what ever is found if &array_x address is a pointer to an integer instead of the integer it self. Now your invert_ad function expects to get two memory addresses where pointers are saved and make sure that that adresses swap their values.
    Nothing can change the memory address of static variables array_a, array_b you can only change what parray_X variables point to.

    No sorry, array_a is a label that helps you recognise what ever you are accessing the compiler will discard the name array_a and in its place will put the memory address where the data are in memory. This can not be changed after compilation (aka at runtime). If you do not save a memory address in the variable then the variable is not a pointer, it makes no difference if it is typed or untyped even that is a compiler only instruction not a run time restriction.
    OOH, now I see, man. I can't use arrays but I can use vectors with dynamic allocation. Thank you!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where Function pointers are stored ?
    By 2001goran in forum C Programming
    Replies: 1
    Last Post: 11-04-2012, 01:36 AM
  2. pointers and function calls inside a function
    By lexitron in forum C Programming
    Replies: 1
    Last Post: 12-03-2011, 05:43 PM
  3. Replies: 17
    Last Post: 07-06-2011, 11:44 AM
  4. Help changing the values inside an array.
    By zbred in forum C Programming
    Replies: 3
    Last Post: 04-10-2011, 11:06 PM
  5. Changing pointer inside function?
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2008, 05:10 AM

Tags for this Thread