C Language-supplements (2) Pointers && declarations

Source: Internet
Author: User

Several easy-to-confuse concepts:

Pointer constants: That is, the value of the pointer itself is immutable, and the value of the variable pointed to by the pointer can be changed;

Constant pointer: that is, the value of the variable pointed to by the pointer is immutable, and the value of the pointer itself can be changed; Pointer to constant

Pointer function: A function that returns a pointer

function pointers: pointers, pointing to a function (where you can continue to nest, the function that points to returns a function pointer ...)

Array pointers: pointers, pointing to arrays

Pointer array: The pointer is stored inside the array

Pointers and Arrays

1.: The array corresponds to an area of memory (present in the symbol table), The pointer is to a piece of memory area. Its address and capacity will not change in the lifetime, only the contents of the array can be changed, but the pointer is different, it points to the size of the memory area can be changed at any time, and when the pointer to a constant string, its contents can not be modified, otherwise at runtime error (in the static area)

The array is equal to the pointer only when the function is passed, and the other pointer is converted more than the array once (the array is determined by the symbol table before the address) so corresponds to two file definitions to be consistent

2. Using the operator sizeof can calculate the capacity (number of bytes) of the array, and sizeof is unable to calculate the capacity of the pointer memory, the result with sizeof (p) is always 4 or 2 (that is, the pointer variable occupies the memory unit of bytes, In general, pointer variables account for 2 or 4 bytes of memory units. When a parameter is passed, the array is automatically degraded to the same type of pointer.

Pointers and references: When a reference is passed as a function argument, it is essentially passing the argument itself

Exp1///sizeof A is different from &a a represents the first element of the array first address.  And &a is the first address of the array sizeof overtime, a+1 and a[0]+1 the same///pointer plus one minus one operand: a pointer of type T moves, with sizeof (T) as the Moving unit int a[5]={1,2,3,4,5};   int *ptr= (int *) (&a+1); printf ("%d,%d", * (a+1), * (ptr-1)); 2 5///exp2int a,*b=&a,**c=&b;float d,*e=&d,**f=&e;* (int * * *) &d=c;d= (float) **c;d=** (float *) C;///exp3///constint b=2;const int* a1=&b;///to int* that the modifier points to the content int* const a2=&b;///to the pointer variable itself, the variable itself is immutable a1= (int *) 2; *a2=2;///exp4///1. The function pointer returns a value of general pointer///2. The function pointer returns a value of function pointer///3. The function pointer returns a value of pointer array///4. The function pointer returns a value of///5 for the array pointer. 1 pointer array 2 pointer array 3 pointer array 4 pointer array char * (*X1) (char *), char * (* (*X2) (char *)) (char *), char * (* (*X3) (char *)) [];char * (*x4) (ch AR *), char * (*X5[10]) (char *), char * (* (*x6[10]) (char *)) (char *), char * (* (*x7[10]) (char *)) [];char * (* (*x8[10]) (Cha R *)) (char *);///exp5void (*p) (void); * (int *) &p= (int) func; The left is the address, and the right side is the content, so forcing the type to convert its address space type (int) func///Specifies that the entry point of this function is changed to int (int) p//or to the content of P, int is constant non-lvalue (int *) p///p is int * and the right int does not match the contents of the * (int *) P///p as a pointer to find an address holding the right value * (int *) &P////Take the address of P to create a temporary pointer to an int * to hold the right value (temporarily explain the space)///EXP6 force type conversion int I=2;float f=i; Convert to 2.0 in copy f=* (float *) &i;//bit copy in memory 01 sequence and Interpretation mode///exp7char * Const * (*next) ();///const use, Next is a pointer to a function, This function returns another pointer to a constant pointer of type char///EXP7 (* (void (*) ()) 0)///for 0 memory location as a function interface to invoke. Similar to typedef * (void (*P) () (* (p) 0) () Understood as a data type


Statement

C Language-supplements (2) Pointers && declarations

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.