C Language Pointer Basics

Source: Internet
Author: User

C Language Pointers

Preamble Program

1#include <stdio.h>2 3  4 5 voidChangeint*);6 7 intMain ()8 9 {Ten  One intA= -; A  -Change (&a); -  theprintf"a=%d\n", a); -  - return 0; -  + } -  +   A  at voidChangeint*N) -  - { -  -*n=Ten; -  in}
View Code

I. Basic points of knowledge

Int a=10;

int *p;//defines a pointer of type int

p=&a;//pointer variable p points to the variable a

*p=20;//the value of variable A is 20 by using the pointer without directly modifying the variable

*p represents access to the storage space that the pointer variable p points to

Pointer one function: Ability to access (value |) According to an address value Assignment) corresponds to the amount of storage space

int in front of pointer variable p, indicating the type of pointer

①. Int *p;

②. *p=10;

Two * difference: the previous function, indicating that the defined p is a pointer, the latter's * represents the address space pointed to by the access P

Second, the use of pointers note

①. Int *p;

Double d=10.0;

p=&d;//does not recommend this approach

②. Int *p;

p=200;//pointer variable can only store address

③. Int *p;

printf ("%d\n", *p);//pointer variable uninitialized, do not use to indirectly access other storage space

④. int *p=&a; but cannot be written as int *p;*p=&a; this notation has no meaning and can be thought of as being used with a type character.

⑤. * is a pointer operator that accesses the space pointed to by the pointer

Third, pointer pointing pointer

Int a=10;

int *p=&a;//pointer to type int

INT **p1=&p;//Pointer to pointer

Int ***p2=&p1;//Level Three pointer

*P2 equivalent to visiting P1;

**P2 equivalent to access p;

P2 equivalent to access A;

*P1 equivalent to access p;

A star and a line.

Four, hands exercise

Write a function that calculates the and difference between A and B (a function returns two values)

Tip: One of the pointers: implementing a function that has multiple return values

View Code

V. Questions relating to pointers

Note: If a pointer of any type occupies 8 bytes of storage space, why do you add a type to the pointer?

A memory analysis of the following code can prove the serious consequences of incorrect pointer type.

Int i=2;

Char c=1;

int *p=&c;//should be of type char, written in int type

Printf ("Value of C is%d\n", *p);//print result is 513, not 1

Printf ("C's value is%d\n", c);//value is 1

The following is a memory analysis of the results of the above code:

The pointer p is supposed to be 1 bytes of data, because the pointer is of type int, so the program naturally starts reading 4 bytes of data from the point of Address 0x0a, and the data accessed from 1 becomes 513.

Tip: The data type of the pointer is clarified, and the pointer is able to correctly access the spatial data that should be accessed.

Vi. Pointers and Arrays

Int ages[5]={10,9,8,7,6};

Iterating through an array

for (int i=0;i<5;i++)

Printf ("%d\n", Ages[i]);

Iterating through an array using pointers

Int *p;

p=ages;//can also be written as p=&ages[0]; the pointer variable p points to the first element of the array

The address of the element:

Address of the first element P &ages[0]

Address of the second element p+1 &ages[1]

Address of the third element p+2 &ages[2]

The value of the element

*p Ages[0]

* (p+1) ages[1]

* (p+2) ages[2]

Use the pointer as an array:

for (int i=0;i<5;i++)

Printf ("%d\n", * (P+i));

(1) Three types of access to array elements:

①. Array name [subscript]

②. Pointer variable name [subscript]

③. * (p+1)

(2) How much does +1 of the pointer variable actually add? This depends on the type of pointer, plus 1 bytes if it is a char type, or 4 bytes if it is of type int.

(3) The pointer is used to receive an array, and the pointer variable points to the first element of the array.

void change (int array[]) is equivalent to void change (int *array).

The former is stored as the first address of an array element, but it becomes a pointer when it is passed.

Example:

Void change (int *array)

{

Printf ("%d\n", array[2]);

Printf ("%d\n", * (array+2));

}

Int Main ()

{

Int ages[5]={1,2,3,4,5};

Change (ages);

}

The result of the call is: the third element of the array 3

If changing to change (&ages[2]), the result of the call is 5, because at this point the array points to ages[2] and ages[2] as the first element of the array.

Vii. Pointers and strings

(i) Basic knowledge

The following two lines of code are essentially different:

①. Char name[]= "it";

②. Char *name2= "it";//pointer variable name2 points to the first character of the string I

Char name[0]= ' y ';//change the value of the first element

Printf ("%s\n", name);//print result for YT

*name2= ' Y ';

Printf ("%s\n", name2);//This time the program crashes

This is because one of the two is a string variable, and the other is a string constant. The C-language array elements are stored on the stack, and the elements inside can be arbitrarily modified, called string variables. The string constants are stored in the constant area and are not changed by the cache.

Char *name1= "it";

Char *name2= "it";

Printf ("%p%p", name1,name2);//The address is the same, stating that name1 and Name2 point to the same string.

There are two ways to master the definition of a string:

①. Using arrays

Features: The characters inside the string can be modified, which is useful when the content needs to be modified frequently.

②. Using pointers

Features: In fact, is a constant string, the characters inside cannot be modified, applicable to the content of the string does not need to be modified, and this string is often used.

(ii) array of pointers

Integer array: This array is an array of integers

Pointer array: This array contains pointers.

Int Ages[5];

Char *name[5]={"Jack", "Rose", "Yang"};//a common notation for string arrays

Corresponds to:

Char name2[3][10]={"Jack", "Rose", "Yang"};

There are two ways to save an array of strings:

①. Array of pointers (array of strings)

②. Two-D character array (array of strings)

How do I enter a string? (using arrays-because they are mutable)

Int Main ()

{

Char NAME[20];

Printf ("Please enter name: \ n");

SCANF ("%s", name);

Printf ("%s", name);

}

Functions for returning pointers

Examples of programs:

#include <stdio.h>

Char *test ();

Int Main ()

{

Char *name=test ();

Printf ("name=%s\n", name);

Return 0;

}

Char *test ()//function to return pointer

{

Return "Rose";

}

Ix. Pointers to functions

The array name is the address of the array, and the function name is the address of the function.

Suppose there are functions:

Void Test ()

{

Printf ("Call the test function \ n");

}

Void (*p) (); The function that the void pointer variable points to does not have a return value, () indicates that the function P points to has no formal parameter

P=test; Pointer p, pointing pointer p to function

There are three ways to manipulate functions:

①. Call test directly ();

②. Call with pointer variable introduction (*P) ();

③. Simplified use P ()

Practice:

Assume that a function is declared as int sum (int a,int b)

The corresponding pointer to the function should be defined as: Int (*p) (int, int);

Point pointer variable p to function: P=sum;

There are three ways to call this function:

(1) int c=p (10,12);

(2) Int c=sum (10,12);

(3) Int c= (*p) (10,12);

Suppose the function is declared as: double haha (double a,char *b,int c);

Then define a pointer to the Haha function should be: double (*p) (Double,char *,int) =haha;

C Language Pointer Basics

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.