函数中定义的指针的打印值和地址? [英] Print value and address of pointer defined in function?

查看:98
本文介绍了函数中定义的指针的打印值和地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一件非常容易编写的事情,但是我在使用C语法时遇到了麻烦,我刚刚使用C ++进行了编程.

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 <stdio.h>
#include <stdlib.h>

void pointerFuncA(int* iptr){
/*Print the value pointed to by iptr*/
printf("Value:  %x\n", &iptr );

/*Print the address pointed to by iptr*/

/*Print the address of iptr itself*/
}

int main(){

void pointerFuncA(int* iptr); 

return 0;
}

很显然,这段代码只是一个框架,但我想知道如何在函数和主要工作之间进行通信,以及打印指向iptr和iptr本身的地址的语法?由于该函数无效,如何将所有三个值发送到main?

Obviously this code is just a skeleton but I'm wondering how I can get the communication between the function and the main working, and the syntax for printing the address pointed to and of iptr itself? Since the function is void, how can I send all three values to main?

我认为地址类似于:

printf("Address of iptr variable: %x\n", &iptr );

我知道这是一个简单的问题,但是我在网上找到的所有示例都具有该值,但是在main中将其定义为

I know it's a simple question, but all the examples I found online just got the value, but it was defined in main as something like

int iptr = 0;

我需要创建一些任意值吗?

Would I need to create some arbitrary value?

谢谢!

推荐答案

阅读评论

#include <stdio.h>
#include <stdlib.h>

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;
}

输出:

Value:  1234
Value:  0xffe2ac6c
Value:  0xffe2ac44

这篇关于函数中定义的指针的打印值和地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆