Talk C together (123rd back: C language instance -- display the address of the variable and function)

Source: Internet
Author: User

Talk C together (123rd back: C language instance -- display the address of the variable and function)

Hello, everyone. We talked about the multi-threaded example in the last time. This example shows the addresses of variables and functions. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

When writing a program, you sometimes need to get the addresses of variables and functions in the program. Today we will introduce how to get their addresses.

Get the variable address

When getting the address of a variable, you only need to use the get address character to directly operate on the variable. For example: int a = 1; then the address of variable a is & a. This method applies to most variables, but some special variables cannot use this method. Next we will introduce how to obtain the addresses of these special variables.

Get the address of the array variable

The array itself is an address, so you can directly output it without the need to operate on it using an address operator. For example, int set [3] can be directly output to the program, which is the address of the array.

Obtains the address of a string variable.

A string variable is similar to an array. It is also an address. Therefore, you do not need to use an address character to operate on it. For example, char * pStr = "string ". You can directly output the pStr content, which is the string address.

Obtain the function address.

The function address is also special. The function name itself represents the function address and can be output directly. Or define a function pointer to indirectly output the address of the function. The function is long, so we will not directly illustrate the example. Please refer to the example in the code below.

For more information, see the following code.

# Include
  
   
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); // display 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); // use the function pointer to display the function address printf (" address of show: % p \ n ", show ); // directly display the function address return 0 ;}
  

In the preceding example, the address value is directly output through the p parameter of the printf function.The following is the running result of the program.For more information, see:

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

In addition to directly outputting the address value in the program, we can also view the address of the function when debugging the program. We can use the GDB debugging tool to view the address of the function. The specific method is as follows:

1. use the-g parameter to add debugging information (gcc-g file. c-s out); 2. use GDB for debugging, and then start running debugging (gdb out, start); 3. use the info command of GDB to view the function address (info address function );

The following is a specific example. In this example, GDB is used to debug the program:

Gcc-g show_address.c-o s // Add the debugging information during compilation. gdb s // use GDB to debug GNU gdb (Ubuntu 7.7-0ubuntu3. 1) 7.7 Copyright (C) 2014 Free Software Foundation, Inc. license GPLv3 +: gnu gpl version 3 or later
  
   
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:
   
    
. Find the GDB manual and other documentation resources online:
    
     
. 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: 1818 int a = 1; (gdb) info address hello // display the address of the function hello Symbol "hello" is a function at address 0x804844d. (gdb) info address show // display the function show address Symbol "show" is a function at address 0x8048466. (gdb) info address main // display the main function's main address Symbol "main" is a function at address 0x804847f.
    
   
  

Let's talk about the example of displaying variables and function addresses. I want to know what examples will be provided later, and I will try again.

Related Article

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.