char * A, char * * A, char * a[], char a[][], char * a[][], char * * a[][], char * A [][][], and so on

Source: Internet
Author: User

http://blog.163.com/digoal@126/blog/static/163877040201271195312138/


This article tests the environment: x86-64 bit architecture server CentOS x64 5.x gcc version 4.1.2 20080704
Pointers and arrays are C's more difficult to understand the knowledge point, need to combine memory to learn, thank you very much brothers for my guidance maze. Here is a summary: First of all, the C program in the runtime, different content or variables stored in where? Divided a few pieces of area is, code, constants, Global, heap, stack; (memory address from low to high) where the constants storage constants (constant values are not allowed to be modified), global variables defined outside all functions (global variables allow modification), heap is a dynamic memory region (can hold persistent content, will not automatically free memory), A local variable in a stack store function (the memory occupied by a local variable is automatically released when the function finishes);
Figure: Next, we'll introduce two symbols * and ampersand. 1. * Used to define a variable type, or to force a type conversion, to indicate that you want to define a pointer or convert the variable type to a pointer (and tell what type of data the pointer is pointing to). 2. * Used in front of a pointer variable, indicates that the pointer points to the content of the address store (what type of content depends on the definition of the pointer: tell what type of data the pointer is pointing to, and the memory address of the pointer's addition and subtraction is related to what type of data the pointer is pointing to, int * a,a+1 The resulting address is 4 bytes on the basis of the address a points to. 3. & used in front of the variable name to indicate the address of the variable.
Example 1:

[Root@db-172-16-3-33 zzz]# cat B.C #include <stdio.h> int main () { char * a = "Hello"; fprintf (stdout, "&a:%p, a:%p, a:%s\n", &a, A, a); return 0; } Results: [root@db-172-16-3-33 zzz]# gcc-o3-wall-wextra-werror-g./b.c-o b && a &a:0x7fff71b22230, a:0x400618, A:hello


&a represents the address of a.  A represents a stored content because a is a pointer, so its contents are read as an address.  *a represents the content stored by this address stored by a pointer. Diagram in memory: This diagram contains a few pieces of content: 1. In a 64-bit system, the pointer occupies 8 bytes. Because the address is stored in the pointer, and the address is 64-bit. So it takes 8 bytes. 2. In the x86 architecture machine, the memory padding is from low to high. So hello is stored in memory: Address: Content

0x400618:0x68 (ascii:h) 0x400619:0x65 (ascii:e) 0x40061a:0x6c (ascii:l) 0x40061b:0x6c (ASCII : l) 0x40061c:0x6f (ascii:o) 0x40061d:0x68 (ascii:null)


3. How does that prove to be correct? It's simple, just print each byte to see. It is important to note that the address that the pointer is added to and subtracted from the address that the pointer points to is the type of content that is stored. Conversely, to have the pointer plus 1 just get the next byte, it tells the compiler that the content of the address to which the pointer points is the char type, because sizeof (char) = 1 bytes. First to print Hello is not stored according to the above mentioned?

[Root@db-172-16-3-33 zzz]# cat B.C #include <stdio.h> int main () {  char * a = "hell O ";   fprintf (stdout, "&a:%p, a:%p, a:%s\n", &a, A, a);   fprintf (stdout, "sizeof (char *):%lu, sizeof (char):%lu\n", sizeof (char *), sizeof (char));   fprintf (stdout, "a+0:%p, * (a+0):%x, * (a+0):%c\n", a+0, * (a+0), * (a+0));   fprintf (stdout, "a+1:%p, * (a+1):%x, * (a+1):%c\n", A+1, * (a+1), * (a+1));   fprintf (stdout, "a+2:%p, * (a+2):%x, * (a+2):%c\n", a+2, * (a+2), * (a+2));   fprintf (stdout, "a+3:%p, * (a+3):%x, * (a+3):%c\n", A+3, * (a+3), * (a+3));   fprintf (stdout, "a+4:%p, * (a+4):%x, * (a+4):%c\n", a+4, * (a+4), * (a+4));   fprintf (stdout, "a+5:%p, * (a+5):%x, * (a+5):%c\n", A+5, * (a+5), * (a+5));   return 0; Results:  [root@db-172-16-3-33 zzz]# gcc-o3-wall-wextr A-werror-g./b.c-o && b &a:0x7ffffe249680, A:0x4006f8, A:hello

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.