Pointers in C Language

Source: Internet
Author: User
Tags integer numbers

Pointers in C Language

1. What is a pointer?

The pointer is the address.

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

If a variable is defined in the program, the system allocates a memory unit to the variable during compilation. The compilation system allocates a certain length of space based on the variable types defined in the program.

Each byte in the memory area will have a number, which is the "Address", which is equivalent to the room number in the hotel, the data stored in the memory unit indicated by the address is equivalent to the passengers living in the hotel room.

The address of a variable.

2. pointer Variables

(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) "*" in front of the pointer variable indicates that the type of the variable is pointer type.

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

(5) indicates the pointer type. The pointer type pointing to the integer data is represented as "int *", read as: pointer pointing to the int or int pointer.

2.1. How to reference pointer Variables

(1) assign values to pointer Variables

Int a = 10; int * p; // defines a pointer variable pp = & a; // assigns the address of a to the pointer Variable p

The pointer Variable p is the address of variable a, and p points to;

(2) reference the variable pointed to by the pointer variable

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

(3) Reference pointer variable values

Printf ("% o \ n", p); // output the address of variable a in octal form, that is, the value of the pointer Variable p.

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

3. Use pointers to reference arrays.

(1) pointer to array elements

1. A variable has an address, and an array contains several elements. Each array element occupies memory units, and each element has a corresponding address. Pointer variables can point to both variables and array elements. The pointer to an array element is actually the address of an array element.

2. There are two methods to reference an array element: (1) subscripts; (2) pointer.

3. Note: The array name does not represent the entire array, but the first address of the array,

(2) pointer operation when referencing array elements

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

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

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

2. If the initial value of p is & a [0], p + I and a + I are the addresses of array element a [I. That is to say, p + I and a + I point to the element whose serial number is I in array. Note: Here, a represents the address of the first element of the array.

3. * (p + I) and * (a + I) are the array elements pointed to by p + I and a + I, that is, a [I].

(3) reference array elements through pointers

There are two methods to reference array elements through pointers:

1. subscript method:

#include <stdio.h>int main(int argc,char **argv){       int a[10];        int i;        printf("Please input 10 integer numbers: ");        for(i=0;i<10;i++)        {             scanf("%d",&a[i]);        }             for(i=0;i<10;i++)        {              printf("%d\n",a[i]);        }              return 0;}       

2. pointer Method

(1) Calculate the address of the array element through the array name to find the element value.

#include<stdio.h>int main(int argc,char *argv[]){    int a[10];    int i;    printf("Please input 10 integer numbers: ");    for(i=0;i<10;i++)    {         scanf("%d",&a[i]);    }      for(i=0;i<10;i++)    {         printf("%d",*(a+i));    }     printf("\n");    return 0;}

(2) Use Pointer variables to point to array elements

#include<stdio.h>int main(int argc,char *argv[]){    int a[10];    int i;    int *p;    printf("Please input 10 integer numbers: ");    for(i=0;i<10;i++)    {         scanf("%d",&a[i]);    }      for(p=a;p<(a+10);p++)    {         printf("%d",*p);    }     printf("\n");    return 0;}

(4) using array names as function parameters

Int main () {void fun (int arr [], int n); // Declaration of the fun function int array [10]; // defines the array... fun (array, 10); // return 0 as the function parameter using the array name;} // define the fun function void fun (int arr [], int n) {....}

Array is the name of the real parameter array, and arr is the name of the form parameter array. When the array name is used as the form parameter, if the values of each element in the form parameter array change, the values of the elements in the real parameter array change accordingly. Why?

When this function is called, the system creates a pointer variable arr in the fun function to store the address of the first element of the real parameter array passed from the main function.

After arr receives the first element address of the real parameter array, arr points to the first element of the real parameter array, that is, to array [0]. Therefore, * arr is array [0]. Arr + 1 points to array [1], arr + 2 points to array [2], and arr + 3 points to array [3]. That is to say, * (arr + 1), * (arr + 2), and * (arr + 3) are array [1], array [2], and array [3].

When a C language calls a function, the actual and actual functions are combined by the "value transfer" method. When the variable name is used as the function parameter, the variable value is transmitted. When the array name is used as the function parameter, the array name represents the address of the first element of the array. Therefore, the passed value is the address. Therefore, the parameter must be a pointer variable.

(5) Reference multi-dimensional arrays through pointers

Pointer variables can point to elements in a one-dimensional array or to elements in a multi-dimensional array.

 

4. Use a pointer to reference a string

(1) string Reference Method

(2) character pointers as function parameters

(3) Comparison of character pointer variables and character Arrays

5. pointer to function

(1) What is a function pointer?

If a function is defined in a program, the compilation system allocates a bucket for the function code during compilation. The starting address (also called the entry address) of the bucket is the pointer to the function.

You can define a pointer variable pointing to a function to store the starting address of a function. This means that the pointer variable points to the function. For example:

int (*p)(int ,int);

Define p as a pointer variable pointing to a function. It can point to a function with an integer type and two integer parameters. The p type is represented by int (*) (int.

Here we need to know the difference between pointer functions and function pointers.

(2) call a function with a function pointer variable

(3) how to define and Use Pointer variables pointing to Functions

(4) use pointers to functions as function parameters

6. functions that return pointer values

7. pointer array and multiple pointers

(1) What is a pointer array?

(2) pointer to pointer data

(3) pointer array as the main function parameter

1. int main ()

Or

Int main (void)

The brackets are empty or contain "void", indicating that the main function does not have any parameters. Real parameters are not required when calling the main function.

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

Argc and argv are the parameters of the main function. They are the "command line parameters" of the program ".

Argc (the abbreviation of argument count, indicating the number of parameters );

Argv is a * char pointer array. Each element in the array (its value is a pointer) points to a string in the command line.

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

8. Dynamic Memory Allocation and pointer variables pointing to it

(1) What is dynamic memory allocation?

(2) How to establish dynamic memory allocation

(3) void pointer type

9. pointer Summary

 

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.