Pointers in the C language

Source: Internet
Author: User
Tags integer numbers

1. What is a pointer?

The pointer is the address.

To clarify what a pointer is, you must first figure out how the data is stored in memory and how it is read.

If a variable is defined in the program, the system assigns an internal deposit element to the program when it is compiled. The compilation system allocates a certain length of space according to the type of variable defined in the program.

Each byte in the memory area has a number, which is the "address", which corresponds to the room number in the hotel, and the data stored in the memory unit marked by the address is equivalent to the passenger residing in the hotel room.

The address of a variable, called a pointer to the variable.

2. Pointer variable

(1), the variable that stores the address is called the pointer variable.

(2), the general form of defining pointer variables is: type name * pointer variable name; For example: int * pointer_1;

(3), the "*" in front of the pointer variable indicates that the variable is of type pointer type.

(4), the meaning of a pointer to a variable includes two aspects: one is the address represented by the storage unit number, and the other is the data type of the storage unit it points to.

(5), how to represent the pointer type. Pointer types that point to integer data are represented as "int *" and read as: a pointer to int or an int pointer.

2.1. How to reference pointer variables

(1), assigning values to pointer variables

int a =ten; int *p; // define a pointer variable pp=&a; // assign the address of a to the pointer variable p

The value of the pointer variable p is the address of the variable A, p points to a;

(2), referring to the variable that the pointer variable points to

printf ("%d\n", *p); // the value of the output variable a

(3), reference the value of the pointer variable

printf ("%o\n", p); // outputs the address of the variable A in eight binary form, that is, the value of the pointer variable p

Note: (1), & take address operator.   &a is the address of variable A. (2), * pointer operator.

3. Referencing arrays by pointers

(1), pointer to array element

1, a variable has an address, an array contains a number of elements, each element of the array in memory occupies the memory unit, they have the corresponding address. Pointer variables can point to variables and, of course, to array elements. A pointer to an array element is actually the address of an array element.

2, there are two methods of referencing array elements: (1), subscript method, (2), pointer method.

3, note: The array name does not represent the entire array, only represents the first address of the array,

(2), the operation of pointers when referencing array elements

When the pointer points to an array element, we can perform the following operations:

int a[]; int *=&a; // equivalent to p=&a[0]

1, p+1 points to a[1], that is, p points to an element in an array element, p+1 points to the next element in the array element, and p-1 points to the previous element in the array element.

2. If the initial value of P is &a[0], then p+i and A+i are the addresses of the array element A[i]. That is, P+i and a+i point to the element of the a array ordinal to I. Note: A In this case represents the address of the first element of the array.

3, * (P+i) and * (A+i) are the array elements that p+i and A+i point to, that is, a[i].

(3), referencing array elements by pointers

There are 2 ways to refer to array elements by pointers:

1. Subscript Method:

#include <stdio.h>intMainintargcChar**argv) {       inta[Ten]; inti; printf ("Please input the integer numbers:");  for(i=0;i<Ten; i++) {scanf ("%d",&A[i]); }              for(i=0;i<Ten; i++) {printf ("%d\n", A[i]); }              return 0;} 

2. Pointer method

(1), the address of the array element is computed by the array name, and the value of the element is found.

#include <stdio.h>intMainintargcChar*argv[]) {    inta[Ten]; inti; printf ("Please input the integer numbers:");  for(i=0;i<Ten; i++) {scanf ("%d",&A[i]); }       for(i=0;i<Ten; i++) {printf ("%d", * (A +i)); } printf ("\ n"); return 0;}

(2), using pointer variables to point to array elements

#include <stdio.h>intMainintargcChar*argv[]) {    inta[Ten]; inti; int*p; printf ("Please input the integer numbers:");  for(i=0;i<Ten; i++) {scanf ("%d",&A[i]); }       for(p=a;p< (A +Ten);p + +) {printf ("%d",*p); } printf ("\ n"); return 0;}

(4), using array function parameters

intMain () {voidFunintArr[],intn);//Declaration of the fun function    intarray[Ten];//Defining array Arrays      .      .     . Fun (Array,Ten);//parameter of function with array name    return 0;}//define the fun functionvoidFunintArr[],intN) {   ....}

Array is the real parameter group name, and ARR is the parameter group name. When using an array of famous parameters, if the values of elements in the parameter group change, the value of the elements of the real parameter group varies. Why is that?

When the function is called, the system creates a pointer variable, arr, in the fun function to hold the address of the first element of the real parameter group passed from the key function.

When ARR receives the first element address of a real parameter group, arr points to the first element of the real parameter group, which is point to array[0]. Therefore, *arr is array[0]. Arr+1 point to Array[1],arr+2 array[2],arr+3 point to array[3]. This means: * (arr+1), * (arr+2), * (ARR+3) respectively array[1], array[2], array[3].

C Language Call function when the combination of virtual method is the "value of the" way, when the variable name as a function parameter is passed the value of the variable, when using the array name as a function parameter, when the array name is represented by the array of the first element of the address, so the value passed is the address, so the request parameter is a pointer variable.

(5), referencing multidimensional arrays by pointers

A pointer variable can point to an element in a one-dimensional array, or to an element in a multidimensional array.

4. Referencing strings by pointers

(1), how strings are referenced

(2), character pointers as function parameters

(3), using the character pointer variable and the character array comparison

5. Pointers to functions

(1), what is a function pointer?

If a function is defined in the program, at compile time, the compilation system allocates a storage space for the function code, and the starting address (also called the entry address) of the storage space is called a pointer to the function.

You can define a pointer variable to a function that holds the starting address of a function, which means that this pointer variable points to the function, for example:

int (*p) (int ,int);

The definition p is a pointer variable that points to a function that can point to a function whose type is shaped and has two integer arguments. The type of P is represented by an int (*) (int int).

Here we need to look at the difference between a pointer function and a function pointer.

(2), call function with function pointer variable

(3), how to define and use pointer variables pointing to functions

(4), using pointers to functions as function parameters

6. Functions that return pointer values

7. Pointer array and multiple pointers

(1), what is an array of pointers

(2), pointer to pointer data

(3), pointer array as parameters of main function

1, int main ()

Or

int main (void)

The parentheses are empty or "void", which means that the main function has no arguments and the main function is called without an argument.

2, int main (int argc,char *argv[])

where argc and argv are the parameters of the main function, which are the "command line arguments" of the program.

ARGC (argument count abbreviation, meaning the number of parameters);

argv (argument vector abbreviation, meaning parameter vector), is an array of *char pointers, each element in the array (whose value is a pointer), pointing to a string in the command line.

-----------------------------------------------

8. Dynamic memory allocation and pointer variables pointing to it

(1), what is the dynamic allocation of memory

(2), how to establish the dynamic allocation of memory

(3), void pointer type

9, the summary of the relevant pointers

Pointers in the C language

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.