一起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 
");

    return 0;
}

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

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

    printf("address of a :%p 
",&a);            //显示变量的地址
    printf("address of set :%p 
",set);         //显示数组的地址
    printf("address of string:%p 
",pStr);      //显示字符串的地址
    printf("address of Function:%p 
",pFunc);   //通过函数指针显示函数的地址
    printf("address of show :%p 
",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.

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