简体   繁体   中英

Can I change the address of the pointer variable in C language?

For example, if there is a pointer variable int *a , int *b and each address value is 1000 , 2000 , can the address value of the pointer variable be swapped as if it were swapping the value of the common variable?

Note, I mean the address of the pointer . Not the value it contains.

As far as I know, the address value of the pointer variable is fixed and cannot be changed, is that right?

From comments:

A lot of people answered, but I think I asked the wrong question. I'm sorry, but my question is not about "change content of a pointer variable", but "change address of a pointer variable."

No, that's impossible for any variable. Not just pointers.

Can I change the address of the pointer variable in c language?

While the answer to your question would be yes for many other languages , including C++, kind of:

"While C++ doesn't directly support putting a variable at a given address, the same effect can be achieved by creating a reference to that address:" ...

...the answer for C is No. "A pointer has an address like other variables (which can't and needn't be changed)" . (quote from 4th response in link.)

For C , the address of all variables (with the exception of those declared as register ) are assigned at the time of declaration as a memory location provided by the operating system, and are immutable for the remainder of their life, no matter the scope in which they were created.
So, while the address pointed to by a pointer variable can be changed, the address of the variable itself, cannot.

Yes, you can:

void swapPointers(void **a, void **b) {
    void *t = *a;
    *a = *b;
    *b = t;
}

void someOtherFunc() {
    int *x = malloc(sizeof(int));
    int *y = malloc(sizeof(int));
    *x = 3;
    *y = 5;
    printf("x = %d y = %d\n", *x, *y);

    swapPointers(&x, &y);
    printf("x = %d y = %d\n", *x, *y);
}

I think you mean swapping addresses stored in pointers because you can not swap addresses of pointers as declared variables themselves.

To swap two objects in a function you have to pass them by reference. Otherwise the function will deal with copies of the values of passed directly to it objects.

In C passing by reference means passing an object indirectly through a pointer to it.

Pointers are the same objects.

So you can write the swap function the following way

void swap_ptr( int **a, int **b )
{
    int *tmp = *a;
    *a = *b;
    *b = tmp;
}

And the function can be called like

int *a = /*...*/;
int *b = /*...*/;

//...

swap_ptr( &a, &b );

Yes, you can swap the address the pointer points to.

int main()
{
     int x = 5, y = 10;
     int* p1 = &x;
     int* p2 = &y;
     printf("Address pointed by p1 is: %p\n", p1);
     printf("Address pointed by p2 is: %p\n", p2);
     //Here goes the swapping
     int* p3 = p1;
     p1 = p2;
     p2 = p3; 
     printf("\n--After swap--\n");
     printf("Address pointed by p1 is: %p\n", p1);
     printf("Address pointed by p2 is: %p\n", p2);
}

yes you can you just need to swap where each pointer point to. declare a 3rd pointer then do the swap as follow.

//we have *a and *b

int *tmp;
tmp = a;
a = b;
b = tmp;

yes you can swap the address stored in two pointer using a temporary pointer variable. But if the pointer is declared as constant pointer for exapmle const int *ptr1; then it not possible because the address stored in ptr1 cannot be modified

#include <iostream>
using namespace std;

int main() {
   int a=10;
   int b=20;
   int *ptr1;
   int *ptr2;
   ptr1=&a;
   ptr2=&b;
   cout<<ptr1<<" "<<ptr2<<"\n";
   int *temp;
   temp=ptr1;
   ptr1=ptr2;
   ptr2=temp;
   cout<<ptr1<<" "<<ptr2;

   return 0;

   }

you can change the syntax to c because I thought that I was answering a question from c++ section

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM