C Language Pointers

Source: Internet
Author: User

First, pointers and constants

Constants are not directly amplitude to the pointer, if you want to give the pointer amplitude constant, you need to do the following:

int cal=8;

int *pcal=&cal;

Pcal points to a 8 stored address, *pcal the data stored for that address 8.

Second, pointer and array

Pointers and arrays have a natural connection, in fact the array is a continuous address holds the constant, we look at the following example:

int arry[3]={1,3,5};

Then Arry is the first address of the array, and *arry is the data 1,* (arry+1) that holds the first address of the array, and the data stored in the second position of the array 3

As can be seen from the above:

*arry and Arry[1] also point to the first element of the array.

Further extended, as follows:

A, int *data[3] is an array of pointers, each element in the array is a pointer to the int data, and is assigned the following values:

int arry[3]={1,3,5};

int *data[3]={arry,arry+1,arry+2};

Data[0] to point to Arry first address, *data[0] data stored for this address 1. Data is the first address of the data array, *data and Data[0] are the first addresses of the arrays Arry. In fact, it can be said that three arrays of data[0], data[1] and data[2] are defined.

b, Int (*data) [3] is an array pointer, an array, each element to hold an address, assigned the following values:

int arry[3]={1,3,5};

Int (*data) [3]=&arry;

Then *data is the address of the first element, and **data is the element 1 where the first address is located.

Third, the pointer and the structure body

struct person

{

Char *pername;

int age;

}person;

If you want to give the *pername amplitude, you can do the following:

Person.pername= "Jack";//Use the pointer to complete the assignment

If the struct is defined as

struct person

{

Char pername[20];

int age;

}person;

Person.pername[20]= "Jack" is not possible, the reason is that the structure is an abstract data type, do not allocate storage units, so the array assignment does not pass. If you use pointers to assign values, you can solve such problems.

Iv. Pointers and functions

Pointers to functions are common in Linux operating systems

int print (int a, int b, char *p)

{printf ("in Put number sum was%d,input string is%s\n", a+b,p);//three parameters, int int and char*

return a+b;}

int main ()

{

Int (* pprint) (Int,int,char *);//define pointer function *pprint to function print, three arguments, int int and char*

pprint=print;//assigning values to pointer functions

int C;

C=pprint (3,6,var);

printf ("%d\n", c);//output print return value

}

A function is actually a special function program that starts from an address, whose function name is the first address of the program, so you can assign a value to the pointer function: pprint=print;//assigns a value to the pointer function


This article from "Big Plum" blog, declined reprint!

C Language Pointers

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.