Pointers Basic Concepts

Source: Internet
Author: User

First, the pointer Prelude 1. The importance of pointers

Pointers are a very important type of data in C, and if you say that you have a good study in c except for pointers, you simply say you have not learned C language.

2. Small needs

(1) When the Void change (int n) function call is complete, the value of the argument is changed

(2) Analysis: Change the value of the argument, find storage space, address

Second, the pointer variable definition 1. The format of the definition

(1) class name identifier * pointer variable name;

int *p;

2. Define the Post assignment first

(1) Simple value

int a = 10;

int *p;

p = &a;

printf ("%d", *p);

(2) Simple change of value

*p = 9;

3. Assigning values at the same time as defined

int a = 10;

int *p = &a;

4. Implement modify argument 5. Watch out.

(1) int *p; p = 1000;

(2) int *p; *p = 100;

(3) int *p; *p = &a;

(4) The address value stored in the%p output pointer

(5) Other pointer type description, such as float *p; Char *p;

(6) The type cannot be used indiscriminately, such as int a = 10; float *p = &a;

6. Clear the pointer

(1) p = 0;

(2) p = NULL;

Third, pointer instances

(1) void swap (char *a, char *b) (note temp=a; A = b; b = temp;)

(2) int Sumandminus (int a, int b, int *minus)

Iv. the research of pointers

(1) The storage space occupied by the pointer variable

(2) Why are pointer variables divided into types?

int i = 2;

char c = 1;

int *p = &c;

printf ("%d", *p);

V. Pointers and Arrays 1. Pointer to a one-dimensional array element 2. Traversing one-dimensional array elements with pointers

(1) Iterate through the char array (' I ', ' t '), and then iterate through the array of int types

(2) * (P+i) and * (p++) difference

(3) A+i and a++

(4) p[0], p[1]

3. Exercises

(1) A function is designed: int arraysum (int a[], int n), and the number of the first n of a one-dimensional array is obtained. Now replace int a[with int *p]

Six, pointers and strings 1. String review

Char s[] = "MJ";

2. Other ways to define strings

Char *s = "MJ";

Or

Char *s;

s = "MJ";

3. The difference between the two ways of definition

(1) Memory analysis

(2) Drawing analysis

(3) The difference between a constant and a variable string

(4) Constant memory address view

4. Exercises

(1) write an int string_len (char *s) to return the character length of the string s

Functions for returning pointers

(1) Pointers are also a type of data in C, so the return value of a function can certainly be a pointer-type

(2) The general form of a function that returns a pointer is: type name * function name ( parameter list)

Eight, pointer to function 1. Why can pointers point to a function?

function as a program, in memory also occupy a portion of storage space, it also has a starting address, that is, the entry address of the function. function has its own address, that's good, our pointer variable is used to store the address. Therefore, a pointer can be used to point to a function. Where the function name represents the address of the function.

2. Definition of pointers to functions

General form of definition: The return value type of the function (* pointer variable name) ( parameter 1, parameter 2, ...);

3. Use note

(1) Since this type of pointer variable stores the entry address of a function, it is meaningless to add or subtract them (such as p++).

(2) Pointer variables pointing to functions have a primary purpose of two:

    • Calling functions
    • Passing functions as arguments between functions

Pointers Basic Concepts

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.