Pointers and Arrays

Source: Internet
Author: User

Speaking of hands, it really makes my teeth itch, this let me love and hate goblin!

At the beginning of the study, how can not understand the pointer of this thing, the pointer is what is what ah .... Until I saw the Zhao Yan Teacher's book.

perhaps, in life we are lack of such a teacher, tell us this is this! Tell us clearly all the ins and outs of Him. If I still don't know, it's only strange that I didn't see the Book.

In fact, know the shaping variable bar, he used to save an integer. Know the character variable, he used to save a character, then know the pointer, he is used to save an address.

What is an address? The address is the number of an area in the Computer's memory. With the number, the whole world can be organized, fast and efficient!

The pointer is powerful, he can point to any address in memory, and can modify the value on that address with a pointer dereference.

You can define a pointer like this:int *p; here, p is the address, p = &a;*p is the value that the pointer points to, but I don't initialize the pointer here, it's not Appropriate.

When defining a pointer, specify the type of data he points to. Like the int i defined earlier, He just tells the compiler that this pointer is of type int. So the "saved addresses" and "types pointing to variables" are the two properties of Pointers.

When you define the pointer is not initialized, your pointer is a wild pointer, to avoid, or expose you are novice of the Fact. When you define a null pointer, make him equal to null ;

Pointer Assignment principle: an xx-type pointer holds an xx-type address (pointer truth).

int a[5]; defines an array of 5 elements a, and the array variable a itself represents an address! That is, the address of the first element in the array, equivalent to a &a[0];

So there is the array of truth :XX Type array variable represents an XX address. So: It can be said that P = a; They're all Addresses.

Pointers and arrays are two completely different things, but in some cases they are equivalent.

When using a[i] in an expression, the compiler automatically converts it to the pointer plus offset * (A+I) form. a[i] This form is intended for the program ape to write programs, but also to let others see convenience.

So in c, the variables and subscripts of the array are Interchangeable. a[3] = = 3[a];

We combine pointers and arrays to get four nouns: pointer pointer, pointer array, array pointer, array array.

1, pointer pointer int **pp;

Pointer to pointer, as the name Implies. int a;int *p; p = &a; This is true, the pointer variable p holds the address of constant a; so if we want to save the address of the pointer p, and so on, we define a pointer to the Pointer.

int **pp = &p; on Sauce.

2, pointer array int *pa[5];

For an array of pointers, the array holds pointers, and the array names are Pointer-type Addresses. It is wrong to execute pa+1 here, so the assignment is also wrong: pa=a; because PA is an unknowable representation,

Only pa[0], pa[1], pa[2]...p[n-1] are present, and they are pointer variables, which can be used to store variable Addresses. But can be so *pa=a; Here *pa represents the value of the first element of the pointer array, the value of the first address of A.

An example to indicate

Char*a[] = {"Zhao","Yan"," is","a","Good","Teacher", NULL};Char**p; for(p=a;*p!=null;p++) {printf ("%s\n",*p);}

3, array pointer int (*ap) [n];

At this point, (*ap) is a pointer to a pointer to an int [n], a one-dimensional array of integral types, the length of the one-dimensional array is n, or the step size of the AP.

That is to say, the AP crosses the length of n integer data when ap+1 is Executed.

To assign a two-dimensional array to a pointer, you should assign this value:
int a[3][4];
int (*p) [4]; The statement defines an array pointer to a one-dimensional array with 4 Elements.
p=a; Assign the first address of the two-dimensional array to p, i.e. a[0] or &a[0][0]
p++; After the statement executes, that is, P=p+1;p crosses line a[0][] points to the line a[1][]

So an array pointer is also called a pointer to a one-dimensional array, also known as a row pointer. A row pointer that points to the next line each time you add 1. but in actual engineering, it is very easy to access a two-dimensional array in the form of pointers or a[i][j].

The array pointer is just a pointer variable, which appears to be a C language specifically designed to point to a two-dimensional array, which occupies the memory of a pointer to the storage space.

Pointer arrays are multiple pointer variables that exist in the form of an array of memory, occupying multiple pointers of storage space.

4, array array (two-dimensional array) int aa[][];

In fact, there is no two-dimensional array in c, just so called comparative image, easy to Understand. Two-dimensional arrays are stored in memory in the same way as one-dimensional arrays, except that each row becomes a one-dimensional array.

is a one-dimensional array of one-dimensional arrays.

II. dynamic memory allocation malloc and CALLOC

Use both of these required references <stdlib.h>

int main () {    char  "123456";     char *t = (char*)malloc(strlen (s) +1); // dynamic memory allocation, without wasting any memory       strcpy (t,s);    printf ("%s\n", t);
Free (t);//develop good habits and release space }

When I read the book, I feel that only this one program is still more useful, the other for the heap and the operation of the stack let me a little overwhelmed, may not have to a certain extent, not very understand, do not write that.

three, string

When I saw the name, I always thought the string was easy, not just a single letter.

Wrong.

Now we know that the string can still be like this ah.

In fact, C does not have a string of this type of data, he is using a character array to Simulate.

*GP = "hello_gp", When the string is defined, GP points to the first address of the string,

Char ga[] = "hello_ga", GA is also the first address that points to a string.

The following is a program that describes the character of a string when it is Stored.

1#include <stdio.h>2 3 Char*GP ="HELLO_GP";//stored in the constant store, the east of this area, cannot manipulate it, cannot change4 Charga[] ="Hello_ga";//Static storage area, He has his own independent storage space, content allowed to modify5 6 Char*F ()7 {8     Char*p ="hello_p";//Constant Storage Area9     Chara[] ="hello_a";//save on the stack, he has his own separate storage space, content allows to modifyTenp[0] ='Z';//error, This error can cause the program to run to Terminate.  onegp[0] ='Z';//Error aGP = a;//the content stored in the constant store cannot be changed, but the pointer to him can change direction, allowing him to point to A's address -gp[0] ='Z';//then It's ready, -     returna; the      - } -  -  + intMain () - { +     Char*str = f ();//STR Gets the address of array a, but a is stored on the stack, a     //when the F function ends, all local variables are popped out of the stack, so a does not exist, and Str gets an empty address.  at      -str[0] ='Z';//Error -Hat0] ='Z'; -      -}

four, pointer function

First it is a function, except that the return value of this function is an address Value. The function return value must be accepted with a pointer variable of the same type, that is, the pointer function must have a function return value, and, in the keynote function,

The function return value must be assigned to a pointer variable of the same type. ,int *f (x,y); defines a pointer function f (y).

Float *fun ();

Float *p;

p = Fun (a);

In the book, it's just that using pointer functions avoids a certain memory Leak.

(memory Leak: a situation in which a program fails to release memory that is no longer in use because of negligence or error.) A memory leak does not mean that there is physical disappearance, but that the application allocates a certain amount of memory

Due to design errors, loss of control over this segment of memory, resulting in a waste of memory. )

 int  *f () { int  *ptr = (int  *) malloc  (sizeof  (int      *ptr = 999  ;  return   ptr;}  int   main () { int  *p =     f (); printf (  " %d      ", *p);  free  (p); // } 

five, function pointers

A function pointer is a pointer variable to a function, which is essentially a pointer variable.

 int (*f) (int x); /* declare a function pointer * /

 f=func; /* assigns the first address of the Func function to the pointer f * /

Because calling functions with function pointers is cumbersome, it is better to call them directly, so more often, function pointers are used in scenarios where callback functions are Needed.

The so-called callback function, there are many excellent online posts, read some, It is difficult to understand the feeling. Let's just say it, it feels like it's rarely used in hardware development, and it's mostly used in software development to provide an interface to the Program. Easy for others to Use.

the so-called callback is that programmer a wrote a program (program a), which reserved a callback function interface, and encapsulated the Program. Programmer B wants A to invoke a method in its own program b, so he callbacks his method in B through the interface in A.

About the callback function, Here's a post. very good: http://blog.csdn.net/callmeback/article/details/4242260/

The definition of complex statements is well written in this blog: http://www.cnblogs.com/afarmer/archive/2011/05/05/2038201.html

Pointers and Arrays

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.