一起talk C栗子吧(第一百二十三回:C语言实例--显示变量和函数的地址)
各位看官們,大家好,上一回中咱們說的是多線程的樣例。這一回咱們說的樣例是:顯示變量和函數(shù)的地址。
閑話休提,言歸正轉(zhuǎn)。讓我們一起talk C栗子吧!
在編敲代碼時(shí),有時(shí)候須要獲取程序中變量和函數(shù)的地址。今天我們就來介紹下怎樣獲取它們的地址。
獲取變量的地址
獲取變量的地址時(shí),僅僅須要使用取地址符對(duì)變量直接操作就能夠。比如:int a = 1;那么變量a的地址就是&a.該方法適用于大部分變量,只是另一些特殊的變量不能使用該方法。接下來我們分別介紹怎樣獲取這些特殊變量的地址。
獲取數(shù)組變量的地址
數(shù)組本身就是一個(gè)地址,因此直接輸出就能夠。不須要再使用取地址符對(duì)它操作。比如int set[3],直接在程序輸出 set的值就能夠,它就是該數(shù)組的地址。
獲取字符串變量的地址
字符串變量和數(shù)組相似,它本身也是一個(gè)地址。因此,不須要再使用取地址符對(duì)它操作。比如char *pStr=”string”。直接輸出pStr的內(nèi)容就能夠。它是字符串string的地址。
獲取函數(shù)的地址
函數(shù)的地址也比較特殊。函數(shù)名本身就代表了函數(shù)的地址,能夠直接輸出?;蛘叨x一個(gè)函數(shù)指針間接輸出函數(shù)的地址。函數(shù)比較長,因此不再直接舉樣例說明。請(qǐng)大家參考以下代碼中的樣例。
看官們。以下是詳細(xì)的代碼,請(qǐng)大家參考。
#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); //顯示數(shù)組的地址printf("address of string:%p \n",pStr); //顯示字符串的地址printf("address of Function:%p \n",pFunc); //通過函數(shù)指針顯示函數(shù)的地址printf("address of show :%p \n",show); //直接顯示函數(shù)的地址return 0; }在上面的樣例中。 我們通過printf函數(shù)的p參數(shù)直接輸出的地址值。以下是程序的執(zhí)行結(jié)果,請(qǐng)大家參考:
address of a :0xbfce7a18 address of set :0xbfce7a24 address of string:0x80485d1 address of Function:0x804844d address of show :0x8048466另外。除了在程序中直接輸出地址值外,我們還能夠在調(diào)試程序時(shí)查看函數(shù)的地址,我們能夠使用GDB調(diào)試工具來查看函數(shù)的地址。詳細(xì)方法例如以下:
- 1.編譯時(shí)使用-g參數(shù)增加調(diào)試信息(gcc -g file.c -s out)。
- 2.使用GDB進(jìn)行調(diào)試,然后開始執(zhí)行調(diào)試(gdb out , start);
- 3.使用GDB的info命令查看函數(shù)地址(info address function);
以下是一個(gè)詳細(xì)的樣例,樣例中使用GDB對(duì)剛才的程序進(jìn)行調(diào)試:
gcc - g show_address.c -o s //編譯時(shí)增加調(diào)試信息 gdb s //使用GDB進(jìn)行調(diào)試 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 //顯示函數(shù)hello的地址 Symbol "hello" is a function at address 0x804844d. (gdb) info address show //顯示函數(shù)show的地址 Symbol "show" is a function at address 0x8048466. (gdb) info address main //顯示主函數(shù)main的地址 Symbol "main" is a function at address 0x804847f.各位看官,關(guān)于顯示變量和函數(shù)地址的樣例咱們就講到這里。欲知后面還有什么樣例,且聽下回分解 。
轉(zhuǎn)載于:https://www.cnblogs.com/brucemengbm/p/7131728.html
總結(jié)
以上是生活随笔為你收集整理的一起talk C栗子吧(第一百二十三回:C语言实例--显示变量和函数的地址)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 当系统扩展遇到违背OO的里氏原则(LSP
- 下一篇: PTA 09-排序3 Insertion