C Language Learning-pointer exploration

Source: Internet
Author: User


1: pointer definition format:
Format: Variable type * Variable name PURPOSE: Pointer variable is used to store the address (only), that is, according to the address value, access to the corresponding storage space.

Attention. int *p can only point to data of type int;

Example: int a =; int *= &A; // both p represents the address, and the *p represents the value // at this point the * represents the pointer variable p points to the storage space, the purpose: change p represents the address of the space. 

Any pointer type that consumes 8 bytes of storage space.

The length of the pointer is the length of data to be taken according to the address of the data pointed to, so to prevent the wrong data need to be consistent with the type that points to the data.

Note: pointer + 1:p+1--> to the next address , not +1 in mathematical sense depending on the type of definition, such as: int:+4,char:+1,double:+8;

Array of pointers:
int age[5]
int *p;
p = age;

Arrays are assigned pointers, and pointers also have the properties of arrays
P[i],

Below the upper and lower case of the same,
void change (int *array) {}

void Changge (int array[]) {}

Using the pointer to receive an array, the pointer variable Arry points to the first element of the array.

3: Pointers and strings

char name[] = "it"; Char // name2 points to the first character of the string, because it is data of type char, it can only point to the first char type. printf ("%c\n", *name2);     --I pointer name2 is the first character that represents the string printf ("%s\n", name2        ); --and it loses format  %s stands for, he will be consistent from the first address output, know to encounter the end of a .
Once you know the basic usage of pointers, there are two ways to define a string:
CharName[] = "it";Char*name2 = "It"; name[1]= ' A ';-->OK*name= ' A ',-->error. Reason: pointer-defined string, immutable, Difference: array: String variable (can change) pointer: string constant (cannot be changed)Char*n1= "It";Char*n2= "It";p rintf ("%p\n%p\n", n1,n2);//View%p View addressThe result is the same. Point to the same address* * The difference between a pointer array and a pointer string Charname[]= "Itcast"; Features: In the variable area, the content can be changed using the occasion: the memory of the string needs to modify the pointer string frequentlyChar*name= "Itcast"Character : This string is actually a constant string (in the constant area of memory), and the characters inside are immutable.                    In the case of use, the memory needs of the strings need not be modified and are often used. 

3: Array of pointers
Char *name[5]={"Jack", "Rose", "Jael"};
Equivalent and two-dimensional array of strings (array of strings)
Char name[3][5]={"Jack", "Rose", "Jael"};

4: pointer to function

Pointers are used to point to addresses, and functions have addresses, and pointers can also be used to refer to

We know the array name, which represents the address of the array, and the function names represent the address of the functions.  

//no return value, no parametervoidTest () {}//with return valueintSumintAintb) {returnA +b;}//with return values, colleague parameters also have pointer formattingDoubleMinDoubleAChar*s,inta) {}intMain () {//1, using void//(*P): Fixed format//void on the left: pointer variable p points to a function that has no return value//Right (): The function that represents P is not a formal parametervoid(*p) ();p= test;//pointer variable p points to test(*P) ()://Call the test function,or P ();//2, with return value int typeint(*p) (intAintb//Int (*p) (Int,int)p =sum;intc = P (4,5);return0;}//3,double FormatDouble(*p) (Double,Char*,int);p=min;//orDouble(*p) (Double,Char*,int)=min;

Darren

Weibo: @IT_ Siege Division

GitHub: @Darren90

Source: http://www.cnblogs.com/fengtengfei/

C language Learning-pointer exploration

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.