C Pointer Summary 2

Source: Internet
Author: User

C Pointer Summary 2

In the last article on the pointer is only one-sided explanation of the general situation of the pointer, and the basic meaning, in fact, when it comes to the pointer must refer to the array, arrays and pointers are inextricably linked. But remember that arrays and pointers are two things at all, pointers cannot and paintings equals.

Array:

For example int a[10]; this declares an array of 10 shapes, containing 10 shaping elements, and the value of the array name is a pointer constant, which is the address of the first element of the array. If the other type is declared earlier, then the type of the array name is "point to a constant pointer of another type." Before I said that arrays and pointers are not a thing, then where are their differences?

1. The array has a determined number of elements, and the pointer is a scalar, and the compiler generates a pointer constant for them only when the array name is used in an expression.

2. The array name is a pointer constant, cannot be modified, only in two cases, the array name is not represented by the pointer constant, that is, when using the sizeof keyword and using the & operator, return the length of the entire array and a pointer to the array, respectively.

3. And generally, arrays are created to open up the actual space.

References to array contents:

There are generally two ways to refer to the contents of an array:

1. Subscript Reference

2. Indirect references

There is no difference except that the priority of [] is higher than *.

For example:

A[3] or * (A+3);

The question of which is more efficient with reference to pointers and subscript references:

There is no doubt that the pointer reference speed is definitely faster than the subscript reference, because the compiler is more time-consuming in subscript processing, of course, the pointer reference is more efficient, and in front of real-world problems, pointers are not necessarily faster. Because code that uses pointers frequently must look more complex, the readability is worse than the subscript method.

Here are a few tips:

1. Pointers are more convenient when you move in an array based on a fixed number of increments.

2. Declaring a register variable is more efficient than a static storage variable.

3. Use as few counters as possible

4. & and sizeof will spend a lot of time and resources at runtime

Array of pointers:

is an array of pointers, that is, every element within an array is a pointer, and this application is especially useful in string applications.

Char *p[3]={

{"Hello"},

{"Linux"},

{"DDD"},

};

Summary and tips:

1. Do not perform pointer operations on a pointer to an array that does not specify a length.

2. Whenever possible, pointer parameters for a function should be declared as const

A discussion of character pointer variables and character arrays:

For example:

char *cp; with Char str[20];

STR consists of several elements, one character per element, and the first address of the string in the CP

How to assign a value:

Str= "I love Linux" error, arrays can only assign values to elements.

Cp= "I love Linux" right

Arrays can also be assigned at the beginning of char str[30]= "I love Linux";

now summarize the statements of those headache pointers:  

⑴ int *p[3]; Array of pointers

⑵ int             (*P) [3]; Pointer to a one-dimensional array

 ⑶  int   *p ( int );               functions that return pointers

 ⑷  int   (*p) ( int );             Pointer to function (takes a shape as a parameter) returns an integer variable

 ⑸  int   * (*p) ( int );           Returns a pointer to a pointer to a function

 ⑹  int (*p[3]) ( int );         function pointer array, function returns integer variable

⑺ int     * (*p[3]) (int); function pointer array, function returns integer pointer


Finally, a small example:

To implement matrix multiplication by pointer:

For example

2-6 4-2-4-5 50 14-44-52

3 5 * =-23-21 18 20

1-1-7-3 6 7 11 1-10-12

int matrix_multiply (int *m1, int *m2,register int *r, int x, int y,int z)

{

register int *m1p;

register int *m2p;

Register int k;

int row;

int column;

for (row=0;row<x;row++)

{

for (column=0;column<z;column++)

{

M1p=m1+row*y;

M2p=m2+column;

*r=0;

for (k=0;k<y;k+=1);

 {

*r+=*mlp* (*M2P);

m1p++;

M2p+=z;

}

r++;

}

}

}

C Pointer Summary 2

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.