Changing address contained by pointer using function

后端 未结 5 592
终归单人心
终归单人心 2020-11-21 06:49

If I\'ve declared a pointer p as int *p; in main module, I can change the address contained by p by assigning p=&a; w

相关标签:
5条回答
  • 2020-11-21 06:54

    In C, functions arguments are passed by value. Thus a copy is made of your argument and the change is made to that copy, not the actual pointer object that you are expecting to see modified. You will need to change your function to accept a pointer-to-pointer argument and make the change to the dereferenced argument if you want to do this. For example

     void foo(int** p) {
          *p = NULL;  /* set pointer to null */
     }
     void foo2(int* p) {
          p = NULL;  /* makes copy of p and copy is set to null*/
     }
    
     int main() {
         int* k;
         foo2(k);   /* k unchanged */
         foo(&k);   /* NOW k == NULL */
     }
    

    If you have the luxury of using C++ an alternative way would be to change the function to accept a reference to a pointer.

    0 讨论(0)
  • 2020-11-21 07:06

    For a primitive data type such as an int, the double pointers are not necessary. You can write directly into the address where the int is stored, treating its address as a pointer in the function being called. This is unlike a char array ("string") where the size of what is pointed to is variable and you must therefore use another level of indirection when changing it from within a called function. Try this:

    void foo(int *oldVal)
    {
        int newVal = 99;    // or whatever you want
        *oldVal = newVal;
    }
    
    int main(int argc, char *argv[])
    {
        int someVal = 0;
        foo(&someVal);      // we send its address to foo()
    
        printf("someVal is now %d.\n", someVal);
    
        return EXIT_SUCCESS;
    }
    
    0 讨论(0)
  • 2020-11-21 07:14

    In C, variables are passed by value - a copy of the pointer is passed to the function. Use another pointer to the pointer instead:

    void change(int **p, int *someOtherAddress)
    {
        *p = someOtherAddress;
    }
    
    int a = 1, b = 2;
    int *p = &a;
    
    printf("*p = %d\n", *p);
    change(&p, &b);
    printf("*p = %d\n", *p);
    

    This prints

    *p = 1
    *p = 2
    
    0 讨论(0)
  • 2020-11-21 07:15

    This won't change the actual value ofp because the q in function is local to that and change in that function will not reflect in main so pass the address of p instead of passing p by value

    Use this syntax below

      void change_adrs(int **q)
      {
      int * otheraddess;
      *q = otheraddress; 
      }
    

    and call like this change_adrs(&p);

    Or, you have other way around : change the return type of function and catch the returned address.

    int* change_adrs(int *q)
          {
          int * otheraddess;
          q = otheraddress;
          return q; 
          }
    
    
    int main()
    {
     p=change_adrs(p);
     return 0;
    }
    
    0 讨论(0)
  • 2020-11-21 07:18

    If you want to alter the content of a variable in a function in C, pointer is a kinda variable as well, you have to pass it by pointer or indirect reference by using always & address and * dereference operators. I mean * operator is always used and preceded when changing the value of a variable.

    #include <stdio.h>
    #include <stdlib.h>
    
    
    void changeIntVal(int *x) {
        *x = 5;
    }
    
    void changePointerAddr(int **q) {
        int *newad;
        *q = newad;
    }
    
    void changePPAddr(int ***q) {
        int **dummy;
        *q = dummy;
    }
    
    int main() {
        int *p;
        int **pp;
        int *tempForPP;
        int a = 0;
        printf("\n The address pointing by p -> %p, pp -> %p, value of a -> %d ", p, pp, a);
    
    
        p = &a;
        pp = &tempForPP;
        printf("\n The address pointing by p -> %p, pp -> %p, value of a -> %d ", p, pp, a);
    
        changeIntVal(&a);        // ----
                                 //    |---
        changePointerAddr(&p);   // ----  |---->  parts of what I mean
                                 //    |---
        changePPAddr(&pp);       // ----
    
        printf("\n The address pointing by p -> %p, pp -> %p, value of a -> %d ", p, pp, a);
    
        return 0;
    
    }
    
    0 讨论(0)
提交回复
热议问题