Commitment c pointer (1) pointer is address

Source: Internet
Author: User

(1) is the address

The first thing to understand is that the pointer is the address. This is the first step in understanding the pointer.

Intuitive feel. The address of the variable

int main () {int Foo;int *foo_p;foo = 5;foo_p = &foo;printf ("   foo...%d\n", foo);p rintf ("*foo_p...%d\n", *foo_p); printf ("  &foo...%p\n", &foo);p rintf ("foo_p...%p\n", Foo_p);p rintf ("&foo_p...%p\n", &foo_p); return 0;}
Execution


A few notes:

    1. P in%p is the meaning of pointer (pointer). Specifically used to print the contents of a pointer variable.
    2. Sometimes you see a pointer print with%x. Although the result is the same, the meaning is completely different.

      %p: The address of a variable that is stored in the appropriate way (usually hexadecimal) in the output pointer variable;%x: The value of the variable is printed in hexadecimal form. And if you print pointer variables in my environment using%x, the previous 0 is omitted.

of the pointer variable
The upper -left corner is the variable name. The upper-right corner is the variable address. The middle is the contents of the variable store. The ability to understand pointers is that pointers are a special language mechanism that holds the addresses of other variables and is able to retrieve the contents of the address by referencing the operator *. This also creates a pointing relationship, such as Foo_p->foo.


High- order pointers like a level two pointer: A pointer to a pointer.

int main () {int foo = 5;int *foo_p = &foo;int **foo_pp = &foo_p;printf ("     foo...%d\n", foo);p rintf ("   &f Oo...%p\n ", &foo);p rintf ("  foo_p...%p\n ", foo_p);p rintf (" &foo_p...%p\n ", &foo_p);p rintf ("  * Foo_p...%d\n ", *foo_p);p rintf (" Foo_pp ... %p\n ", foo_pp);p rintf (" &foo_pp ... %p\n ", &foo_pp);p rintf (" *foo_pp ... %p\n ", *foo_pp);p rintf (" **foo_pp ... %d\n ", **foo_pp); return 0;}
Execution

by executing the result, draw the memory:
the type deduction for each variable. Foo_p is a pointer. and points to int, so the type of foo_p is int*. That is, adding int;foo_pp before the ' * ' is also a pointer and pointing to Foo_p. So the type of FOO_PP is int**, which is to add int* before ' * '. a higher-order pointer type. And so on
look at the amount of memory allocated for each basic type in my environment
int main () {printf ("sizeof (char) ...%d\n", sizeof (char));p rintf ("sizeof (int) ...%d\n", sizeof (int));p rintf ("sizeof ( float)%d\n ", sizeof (float));p rintf (" sizeof (double) ...%d\n ", sizeof (double));p rintf (" sizeof (int*) ...%d\n ", sizeof (int*)); return 0;}
Execution
In my environment, the size of the pointer type assignment is sizeof (int*) = 4, which means that the address of the variable is stored with a size of 4 bytes, which is the result of most of the current environment. Later discussions are based on this result.

As for the C standard, the size of the pointer type is not specified, and the detailed size depends on the detailed environment.

about sizeof It must be noted first: sizeof is an operator, not a function.

is misunderstood as a function, which may be the case in most cases. We all use it this way: sizeof (). In fact this is possible with the sizeof type, such as sizeof Int. The correct use is to assume that type is the name of the class. Then sizeof (type), assuming that the type is a variable, it can be added without parentheses.
discuss several problems (1) Actually the pointer holds the address of the variable, and in the same environment, the address is the same type of integer, such as 4-byte size, then why there is a pointer-type argument?

int main () {int Foo;int *int_p = &foo;//in c The following code gives a warning, but is executable, whereas in C + + it is a double *dou_p = &foo;foo = 5;printf ("foo ...%d\n ", foo);p rintf (" int_p...%p\n ", int_p);p rintf (" dou_p...%p\n ", dou_p);p rintf (" *int_p...%d\n ", *int_p);p rintf ( "*int_p...%f\n", *int_p);p rintf ("*dou_p...%d\n", *dou_p);p rintf ("*dou_p...%f\n", *dou_p); return 0;}
Execution

The result is so messy!

Why such a situation arises. The key is that different types of variables have different storage methods.

such as the 4-byte int and the same 4-byte float, the allocated space is the same size, but can represent a very large gap in the range of data. Detailed storage method, we can check.that's the explanation. When you do a dereference, you must indicate the appropriate type to get the value of the variable correctly. A pointer is a derived type whose type depends on the object it points to. Two ConceptsThe type of the pointer, the type that the pointer points toillustrated by example: int *p;(i) P is a pointer, and its type is int*(ii) p is a pointer. The type that it points to is intThere is also an example: int **p;(i) P is a pointer. Its type is int** (level two pointer, which will be described later)(ii) P is the pointer, and the type it points to is int*The method is very easy: is not a pointer type, just see if there is a * in the declaration, the type of pointer is to remove the variable name. The remaining part; the type that the pointer points to is to remove the variable name and its recent *. The rest is. the same reminds us: do not ignore the compiler warning!


(2) NULL pointer type: void*provides a type of pointer in ANSI C that can accept no matter what type: void* (null pointer type)int foo = 5;void *p = &foo; This sentence will not be an error.printf ("*p...%d\n", *p); This sentence cannot be compiledcannot be compiled because it is assumed that only the memory address of the variable is known, but the type of the variable is not known. The compiler does not know how to parse the contents of this address, that is, it cannot be dereferenced.
(3) printf () functionprintf (format control, output table);Function: Output the specified data in accordance with the prescribed format.
"Format Control" is a format that controls the conversion of strings in a double-quoted form. The data in the output table can be a valid constant, variable, and expression, corresponding to the format character one by one in format control. several format output characters%d--output integers in signed decimal form
%o--output integers in unsigned octal form
%x--output integers in unsigned 16 binary form
%u--output integers in unsigned decimal form
%c--output a single character as a character
%s--Output string
%f--output single, double-precision real numbers in decimal point form
%e--output single, double-precision real numbers in standard exponential form
%g--output real numbers in a format with a smaller output width


Columns folder:

    • C pointer
    • Data structure and algorithms


Copyright notice: This article Bo Master original articles, reproduced, reproduced please indicate the source.

Commitment c pointer (1) pointer is address

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.