Talk C Chestnut Bar (123th: C-language instance-shows the address of variables and functions)

Source: Internet
Author: User

Ladies and gentlemen, good crossing, the last time we were talking about multithreading examples. This time we say the example is: Display the variable and function address.

Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Sometimes you need to get the address of variables and functions in your program when you are typing code. Today we'll show you how to get their addresses.

Get the address of a variable

When you get the address of a variable, you simply need to manipulate the variable directly using the address character. For example: int a = 1; then the address of variable A is &a. This method applies to most variables, except that the method cannot be used by other special variables. Next we describe how to get the addresses of these special variables.

Get the address of an array variable

The array itself is an address, so the direct output is possible. You do not need to use the fetch address character to manipulate it. such as int set[3], directly in the program output set value can be, it is the address of the array.

Get the address of a string variable

String variables and arrays are similar, and are itself an address. Therefore, you do not need to use the address character to manipulate it. For example, char *pstr= "string". Direct output of PSTR content is possible. It is the address of a string.

Gets the address of the function

The address of the function is also more special. The function name itself represents the address of the function and can be output directly. or define a function pointer to the address of the indirect output function. function is longer, so it is no longer straightforward to give a sample description. Please refer to the sample in the code below.

Crossing, they are. Here is the detailed code, please refer to the test.

#include <stdio.h>intHello () {printf("Hello \ n");return 0;}intShow () {printf("show function is running \ n");return 0;}intMain () {intA =1;int Set[3];Char*pstr ="string";int(*pfunc) () = Hello;printf("Address of a:%p \ n", &a);//Show the address of the variable    printf("Address of Set:%p \ n",Set);//Display the address of the array    printf("Address of string:%p \ n", PSTR);//Display the address of the string    printf("Address of function:%p \ n", PFunc);//Displays the address of the function through a function pointer    printf("Address of Show:%p \ n", show);//directly displays the address of the function    return 0;}

In the example above. We directly output the address value through the P-parameters of the printf function. The following is the results of the implementation of the program , please refer to:

of a :0ofset :0ofstring:0ofFunction:0of show :0

Other than that. In addition to outputting the address values directly in the program, we were able to see the address of the function while the program was being debugged, and we were able to use the GDB debugging tool to see the address of the function. Detailed methods such as the following:

    • 1. Compile with the-G parameter to increase debug information (Gcc-g file.c-s out).
    • 2. Debug with GdB and start debugging (GdB out, start);
    • 3. Use GDB's info command to view the function address (info address function);

Here is a detailed example, using GDB to debug a program just now:

Gcc-g Show_address.c-o S//compile-time Add debug information gdb S//use GDB to debug GNU gdb (ubunt U7.7-0Ubuntu3. 1)7.7Copyright (C) theFree software Foundation, inc.license gplv3+: GNU GPL version3 orLater  isFree software:you is free toChange andRedistribute it. There isNO WARRANTY, toThe extent permitted by law.Type "Show copying" and "Show warranty"  forDetails. This GDB is configured as"I686-linux-gnu".Type "Show Configuration"  for ConfigurationDetails. forBug reporting instructions, please see: andOther documentation resources online at: forHelptype "Help".Type "Apropos word"  toSearch forCommands related to "word"... Reading symbols from S...done. (GDB) Start temporary breakpoint1At0x8048488:fileSHOW_ADDRESS.C, line.Starting program:/home/talk_8/c_example/s Temporary Breakpoint1, Main () at SHOW_ADDRESS.C: - -int a =1;(gdb) Info address Hello//display function Hello addresses symbol"Hello"  isAfunctionAt address0x804844d. (GDB) Info address show//show function show addresses symbol"Show"  isAfunctionAt address0x8048466. (GDB) Info address main//Show main function main"Main"  isAfunctionAt address0x804847f.

You crossing, the example of displaying variables and function addresses is here. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (123th: C-language instance-shows the address of variables and functions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.