Print value and address of pointer defined in function?

后端 未结 5 1948
[愿得一人]
[愿得一人] 2021-02-07 23:29

I think this is a really easy thing to code, but I\'m having trouble with the syntax in C, I\'ve just programmed in C++.

#include 
#include 

        
5条回答
  •  臣服心动
    2021-02-07 23:50

    Read the comments

    #include 
    #include 
        
    void pointerFuncA(int* iptr){
      /*Print the value pointed to by iptr*/
      printf("Value:  %d\n", *iptr );
        
      /*Print the address pointed to by iptr*/
      printf("Value:  %p\n", iptr );
    
      /*Print the address of iptr itself*/
      printf("Value:  %p\n", &iptr );
    }
        
    int main(){
      int i = 1234; //Create a variable to get the address of
      int* foo = &i; //Get the address of the variable named i and pass it to the integer pointer named foo
      pointerFuncA(foo); //Pass foo to the function. See I removed void here because we are not declaring a function, but calling it.
       
      return 0;
    }
    

    Output:

    Value:  1234
    Value:  0xffe2ac6c
    Value:  0xffe2ac44
    

提交回复
热议问题