一起talk C栗子吧(第一百二十三回:C语言实例--显示变量和函数的地址)


各位看官们,大家好,上一回中咱们说的是多线程的例子,这一回咱们说的例子是:显示变量和函数的地址。闲话休提,言归正转。让我们一起talk C栗子吧!

在编写程序时,有时候需要获取程序中变量和函数的地址,今天我们就来介绍下如何获取它们的地址。

获取变量的地址

获取变量的地址时,只需要使用取地址符对变量直接操作就可以。例如:int a = 1;那么变量a的地址就是&a.该方法适用于大部分变量,不过还有一些特殊的变量不能使用该方法。接下来我们分别介绍如何获取这些特殊变量的地址。

获取数组变量的地址

数组本身就是一个地址,因此直接输出就可以,不需要再使用取地址符对它操作。例如int set[3],直接在程序输出 set的值就可以,它就是该数组的地址。

获取字符串变量的地址

字符串变量和数组类似,它本身也是一个地址,因此,不需要再使用取地址符对它操作。例如char *pStr=”string”。直接输出pStr的内容就可以,它是字符串string的地址。

获取函数的地址

函数的地址也比较特殊,函数名本身就代表了函数的地址,可以直接输出。或者定义一个函数指针间接输出函数的地址。函数比较长,因此不再直接举例子说明,请大家参考下面代码中的例子。

看官们,下面是详细的代码,请大家参考。

#include<stdio.h>

int hello()
{
    printf("hello \n");

    return 0;
}

int show()
{
    printf("show function is running \n");

    return 0;
}
int main()
{
    int a = 1;
    int set[3];
    char *pStr = "string";
    int (*pFunc)() = hello;

    printf("address of a :%p \n",&a);            //显示变量的地址
    printf("address of set :%p \n",set);         //显示数组的地址
    printf("address of string:%p \n",pStr);      //显示字符串的地址
    printf("address of Function:%p \n",pFunc);   //通过函数指针显示函数的地址
    printf("address of show :%p \n",show);       //直接显示函数的地址

    return 0;
}

在上面的例子中, 我们通过printf函数的p参数直接输出的地址值。下面是程序的运行结果,请大家参考:

address of a :0xbfce7a18 
address of set :0xbfce7a24 
address of string:0x80485d1 
address of Function:0x804844d 
address of show :0x8048466 

另外,除了在程序中直接输出地址值外,我们还可以在调试程序时查看函数的地址,我们可以使用GDB调试工具来查看函数的地址。具体方法如下:

  • 1.编译时使用-g参数加入调试信息(gcc -g file.c -s out);
  • 2.使用GDB进行调试,然后开始运行调试(gdb out , start);
  • 3.使用GDB的info命令查看函数地址(info address function);

下面是一个具体的例子,例子中使用GDB对刚才的程序进行调试:

gcc - g show_address.c -o s                //编译时加入调试信息
gdb s                                      //使用GDB进行调试
GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from s...done.
(gdb) start 
Temporary breakpoint 1 at 0x8048488: file show_address.c, line 18.
Starting program: /home/talk_8/C_Example/s 

Temporary breakpoint 1, main () at show_address.c:18
18      int a = 1;
(gdb) info address hello                  //显示函数hello的地址
Symbol "hello" is a function at address 0x804844d.
(gdb) info address show                   //显示函数show的地址
Symbol "show" is a function at address 0x8048466.
(gdb) info address main                   //显示主函数main的地址
Symbol "main" is a function at address 0x804847f.

各位看官,关于显示变量和函数地址的例子咱们就说到这里。欲知后面还有什么例子,且听下回分解 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值