C language beginners-preliminary reference

Source: Internet
Author: User

Pointer:
The pointer is a 4 bytes Integers and the memory address is saved,

1. Definition:
Type * variable name:
Int * p; defined in this way, p has an address and a value, but the value is a random value.
It should be initialized: int * p = null;
Different types of application are not recommended:
Char c = 'E ';
Int * p = & c;
It is not recommended to directly make it equal to an address:
Int * p = 0xffff22;
2. Use const to declare pointer Variables
1) pointer variable pointing to const
Int const * p; equivalent to const int * p;
The content of the memory to which the pointer variable points is immutable.
Eg. const int a = 1;
Const int * p = &;
The value of a cannot be changed.
2). const pointer variable
Int * const p;
The memory space to be pointed to is fixed, and cannot be directed to other spaces after initialization, that is, p cannot be equal to other addresses.
3). const pointer variable pointing to const
Const int * const p;
The value of the pointer variable and the value of the space pointed to by the pointer cannot be changed.

3.
[Csharp]
Int;
Int * p;
P = &;

 
This is correct. The value of a is invalid, but the address is valid when the system allocates memory space to it. Only the items recycled by the system and defined by the user are invalid.

4. pointer Parameters
You can pass the address of a memory space to the function and use this address to change the memory space on the address.
Declaration method: void func (int * p, int * q );
Eg:
Exchange a and B values:
The common method is to perform the value transfer operation, so that the exchange cannot be realized.
[Csharp]
# Include <stdio. h>
# Include <math. h>
Void swap (int, int );
 
Int main (){
Int a = 1;
Int B = 2;
Swap (a, B );
Printf ("a = % d B = % d \ n", a, B );
Return 0;
}
 
Void swap (int a, int B ){
Int temp = 0;
Temp =;
A = B;
B = temp;
Printf ("a = % d B = % d \ n", a, B );
}
The correct method is to transfer an address (pointer) to replace it.
[Csharp]
# Include <stdio. h>
# Include <math. h>
 
Void exchange (int *, int *); // Declaration
 
Int main (){
// Define two int-type numbers
Int a = 5;
Int B = 7;
Printf ("a = % d B = % d \ n", a, B); // output values of a and B
Exchange (& a, & B); // call
Return 0;
}
Void exchange (int * x, int * y ){
Int temp; // defines the intermediate variable
Temp = * x;
* X = * y;
* Y = temp;
Printf ("a = % d B = % d \ n", * x, * y); // output
}


5. pointer function value
The return value of a function can also be pointer-type data, that is, the address.
Data Type * function name (parameter list );
Eg:
[Csharp]
# Include <stdio. h>
Int * max (int *, int *); // Declaration
Int main (){
Int a = 1;
Int B = 2;
Int * p = NULL; // defines an int pointer to receive a function.
 
Return Value of add
P = max (& a, & B); // pass the address
Printf ("% d \ n", * p); // output

Return 0;
}
Int * max (int * a, int * B ){
Int * p = NULL; // Initialization
If (* a> * B ){
P =;
} Else {
P = B;
}
Return p;
}

Note:
Disordered time and space:
[Csharp]
Float * mistake (){
Float temp = 12;
Float * p = & temp;
Return p;
}

 
Temp is only valid in the mistake method block. If a return statement exists, it may or may not be used in the outside world, sometimes no error occurs .. Www.2cto.com

6. function pointer
Pointer to the function, which can indicate the address of the function.
There is such a function declaration:
Int add (const int a, const int B );
Use this function type to define a function pointer:
Int (* fq) (const int a, const int B );
It is equivalent to assigning the function variable func with the address of the function to fq.
Pointer: fq = add;
Note: (* fq) the parentheses must not be omitted.
Two methods to call: fq (3, 3); or (* fq) (3, 3)
Eg:
[Csharp]
# Include <stdio. h>
Int add (const int a, const int B ){
Return a + B;
}
Int main (){
// Define the function pointer
Int (* fp) (const int a, const int B );
// Assign a value
Fp = add;
// Call and Output
Printf ("% d \ n", fp (3, 4 ));
Printf ("% d \ n", (* fp) (3, 4 ));
 
// Output function address
Printf ("% p \ n", add );
Printf ("% p \ n", & add );
Printf ("% p \ n", fp );
Printf ("% p \ n", * fp );
Return 0;
}

7. void pointer
It can point to a space that stores any data type, that is, it contains all
Type, which is often used as a form parameter. It can point to any type of pointer variable.
Can be used as the function value.
Definition form: void * variable name;

8.
Pointer call: (use of level-1 and level-3 pointers)
[Csharp]
# Include <stdio. h>
# Include <math. h>
Int main (){
Int a = 1;
Int * B;
B = &;
Printf ("B value (address of a): % p \ n", B );
Printf ("value of a: % d \ n", * B );
Printf ("B address: % p \ n", & B );
Printf ("--------------------------- \ n ");
Int ** q;
Q = & B;
Printf ("q value (address of B): % p \ n", q );
Printf ("value of B (address of a): % p \ n", * q );
Printf ("value of a: % d \ n", ** q );
Printf ("q address: % p \ n", & q );
Printf ("--------------------------- \ n ");
Int *** r;
R = & q;
Printf ("r value (address of q): % p \ n", r );
Printf ("q value (address of B): % p \ n", * r );
Printf ("value of B (address of a): % p \ n", ** r );
Printf ("value of a: % d \ n", *** r );
Printf ("r address: % p \ n", & r );
Return 0;
}


From like7xiaoben

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.