"In-depth understanding of the C language" pointer

Source: Internet
Author: User

This paper makes a brief analysis of the pointer problem in the "in-depth comprehension of C language" in Coolshell.

#include <stdio.h>intMainvoid){    inta[5]; printf ("%x\n", a); printf ("%x\n", A +1); printf ("%x\n", &a); printf ("%x\n", &a +1); return 0;}

Suppose the address of a is: 0xbfe2e100, and the 32-bit machine, then what will the program output?

#1: The first printf statement should have no problem, that is bfe2e100

#2: The second printf statement you might think is bfe2e101. That's wrong, a+1, the compiler compiles A + 1*sizeof (int), and int is 4 bytes under 32 bits, so it's

Plus 4, which is bfe2e104. "A is a pointer to data of type int"

#3: The third printf statement may be your biggest headache, how do we know the address of a? I don't know? It's not bfe2e100. Wouldn't that be a a==&a? How is that?

Yes? To save your own? maybe a lot of people think that pointers and arrays are one thing, then you're wrong . if it is int *a, because A is a pointer, so &a is the address of the pointer, A and

  &a No same . but this is an array ah a[], so &a is actually compiled into &a[0].

#4: The fourth printf statement is natural, that is bfe2e104. Or not, because &a is an array and is considered int (*) [5], so sizeof (a) is 5, which is

5*sizeof (int), also known as bfe2e114. "&a can be understood as a pointer to an int [5] type, i.e. A is an array type"

Reference:

In-depth understanding of C language: http://coolshell.cn/articles/5761.html

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.