iOS Learning Journey---pointers are not difficult

Source: Internet
Author: User
Tags readfile

1. Understanding Pointers
#include <stdio.h>//basic data type is passed as a function parameter is a value passed//void movefront (int x, int y)//{//    x  = x + 2;//}void Test () {    / /  determines the current coordinate    int x =;    int y =.    printf ("%p\n", &x);    printf ("%lu\n", &x);    * ((int *) (0x7fff5fbff76c)) =;    printf ("(%d,%d) \ n", x, y);    Movefront (x, y);    printf ("(%d,%d) \ n", x, y);} If you want to access the storage space that the pointer points to, you must use the operator of the storage space pointed to by the Access pointer void Movefront (int *x, int *y) {//  x  = x + 2;//This is the point at which the pointer is changed, Instead of accessing the storage space pointed to by the pointer    *x  = *x + 2;} int main (int argc, const char * argv[]) {    //  determines the current coordinate    int x =;    int y =.    printf ("(%d,%d) \ n", x, y);    Movefront (&x, &y);    printf ("(%d,%d) \ n", x, y);    return 0;}


2, the definition and initialization of pointers (key to grasp)


The smallest storage unit in memory: bytes, each byte has a number in memory, this number is the pointer

Pointers: Memory Addresses
With the pointer, you'll have the memory key open and you can manipulate the memory.

Pointer variable: The variable that holds the memory address
Define pointer: Pointer to the data type * pointer variable name;
When defining a variable, * is a type specifier that defines that the variable is a pointer variable
When a variable is not defined, * is an operator that accesses (reads, writes) The block of storage that the pointer points to is empty
Initialization of the pointer:

Note the point:
1, only the definition did not initialize the pointer inside is a garbage value, this time we become this pointer for the wild pointer
2, if the operation of a wild pointer
2.1 Program crashes
2.2 Access should not be your access to storage, Operation potential logic error
3. You cannot assign a pointer variable with an integer constant
Because the memory is assigned to us by the operating system, not our random.
4, what type of pointer, only what type of variable
5. Multiple pointers can point to the same variable
6, the pointer can change the direction of


#include the definition of the <stdio.h>//pointer Void Test () {    int num = ten;  defines a pointer variable    int *p;    p = #        *p =;    printf ("num =%d\n", num);} int main (int argc, const char * argv[]) {//First defined in initialization    int num = 10;//  defines a pointer variable p    int * p;    *p = #//p also has an initial, cannot access it points to storage space    P = #//p points to num    *p =;  defines a pointer variable at the same time as the initial        int num2 =;    int *p2 = &num2;        *P2 = +;        printf ("%d,%d\n", NUM2,*P2);    You cannot assign a pointer variable with an integer constant//Because memory is assigned to us by the operating system, not by our random    /    int *p3 = 100000;//Here is the wrong///    /    *p3 = ten;        P2 = #        printf ("%p\n", p2);        char c = ' a ';        int *pc = &c;        *PC = ten;        printf ("%p\n", p2);            return 0;}

3. Multi-level pointer


Accessing variables through pointers is called indirect access. Because pointer variables point directly to variables, they are called "first-level pointers." and
If you access a variable by pointing to a pointer variable, it forms a level two pointer.

#include <stdio.h>void Test () {    int num = ten;        int *p = #        //  Define a pointer to point to the variable p    //  PP is a level two pointer    int **pp = &p;        **PP = +;    printf ("%d\n", num);        int ***PPP = &pp;    PPP = +;    printf ("%d\n", num);  level four pointer    int ****pppp = &ppp;    PPPP = +;    printf ("%d\n", num);   } void ReadFile (char **error) {    *error = "Read error";   } int main (int argc, const char * argv[]) {  //    char error[100];        char *error;        ReadFile (&error);        printf ("%s", error);        return 0;}

4, why the pointer to distinguish the type

1. The address of the variable is the first address of the storage space where the variable resides.
2, pointer variable can only store an address number, if there is no type, when through the pointer do not know how many bytes of storage space to access
3. The pointer distinguishes the type so that it can access the storage space pointed to by the pointer.
4. If a variable of int is manipulated by a pointer of type char, if the binary data of the value exceeds 1 bytes, then the data error is caused.
5. If you manipulate a char variable by a pointer of type int, you will modify the memory you should not modify, resulting in a program logic error
#include <stdio.h>/*   all pointer types are storage spaces that occupy eight bytes   */void testeverypointeris8b () {    printf ("%lu\n", sizeof ( int *));    printf ("%lu\n", sizeof (char *));    printf ("%lu\n", sizeof (double *));    printf ("%lu\n", sizeof (float *));    printf ("%lu\n", sizeof (float *));} int main (int argc, const char * argv[]) {        int num = ten;        char *CP = #        printf ("%d\n", num);        return 0;}

5. Overview of pointer operations
Pointer variable: The address number in which the memory byte is stored (unsigned number of shaping)
Pointer: is an unsigned number of operations constrained
Arithmetic operation:
Pointer + shape Number = = = Pointer variable median value + sizeof (the data type it points to)
Pointer-integer Number = = = Pointer variable median-sizeof (the data type it points to)
Pointer1-pointer2 = (pointer1 median-pointer2 median)/sizeof (which points to the data type)
Assignment operation:
=
+ = must be an integer number
-= must be an integer number
Comparison operation
==
!=
>
<
>=
<=
Self-increment self-reduction
p++; p = p + 1;
++p; p = p + 1;
--p;
p--;
#include <stdio.h>//arithmetic operation void Test () {    int a = ten;        int *p = &a;  pointer +1    p = p + 1;            int nums[5] = {1,2,3,4,5};        int * Pointer1 = nums;        int * Pointer2 = &nums[4];        size_t size  = pointer2-pointer1;        printf ("%lu\n", size);  pointer1 + pointer2;    pointer2 * POINTER1;    Pointer1/pointer2;    POINTER1/2;} Assignment operation void Test1 () {    int a = ten;    int *p = &a;        int nums[] = {1,2,3,4,5};        int *p = nums;    int *p2 = nums;    p + = 2;    p = p + 2;        P-= 1;            printf ("%d\n", *p);   } Relational operation int main (int argc, const char * argv[]) {      int nums[] = {1,2,3,4,5};        int *p = nums;    p++;    int result =  Nums = = p;    result = p > nums;    p--;    result = P < nums;       result = P >= nums;    result = P <= nums;       printf ("%d\n", result);       return 0;}
6. Pointers and one-dimensional arrays (understanding)

An array is like a pointer: Accessing an array of elements, using an array and using pointers to this array is equivalent

NUMS[1] = = P[1]
nums+1 = = = p + 1;

The nature of Nums[1] * (nums + 1)
Pointer + integer ===== value in pointer + sizeof (data type pointed to) * integer
int nums[] = {1,2,3,4,5};
//
int *p = Nums;
Double nums[] = {1.0,2.0,3,4,5};
Double * p = nums;
printf ("%d,%d,%d,%d,%d,%d\n", nums[1],p[1],* (Nums + 1), * (P + 1), * (++p),. );
printf ("%p\n", nums);
printf ("%p\n", nums+2);
printf ("%p\n", p);
printf ("%p\n", p+2);
Array is not a pointer
1. sizeof (array)! = sizeof (pointer): When an array is assigned a pointer variable, some of the information in the array is lost, such as the array length, which is missing from the pointer information
2, pointers can be changed, the direction of the array can not be changed
3, Array = = &array Array name is the array address, pointer! = &pointer: The pointer is pointing to the address is not the pointer itself address

#include <stdio.h>int main (int argc, const char * argv[]) {    int nums[] = {1,2,3,4,5};    int *p = nums;    p = nums;//    nums = nums + 1;         printf ("%lu,%lu\n", sizeof (Nums), sizeof (p));           printf ("%p\n", nums);    printf ("%p\n", &nums);          printf ("%p\n", p);    printf ("%p\n", &p);            return 0;}

7. Pointers and two-dimensional arrays
The difference between a pointer array and a two-dimensional array pointer variable
You should be aware of the difference between pointer arrays and two-dimensional array pointer variables. Both of these can be used to represent a two-dimensional array, but the presentation method and meaning are
Different.
A two-dimensional array pointer variable is a single variable, in its general form "(* pointer variable name)" On both sides of the parentheses are not few. And the pointer array type represents the
is multiple pointers (a set of ordered pointers) in the general form "* Pointer array name" cannot have parentheses on either side. For example:
int (*p) [3];
Represents a pointer variable that points to a two-dimensional array. The number of columns in the two-dimensional array is 3 or the length of the decomposition into a one-dimensional array is 3.
int *p[3]
Indicates that P is an array of pointers, and that there are three subscript variables p[0],p[1],p[2] are pointer variables.

#include <stdio.h>void test () {int nums[3][2] = {{1,2},{3,4},{5,6}};        int *p = nums[0];    printf ("%p\n", p);        printf ("%p\n", nums);    for (int i = 1; i < 6; i++) {printf ("%d", * (P + i));        }/* defines the format of the pointer array: data type * pointer variable name [number of pointers] */void test2 () {int nums[3][2] = {{1,2},{3,4},{5,6}};    int * P[2] = {nums[0],nums[1]};    p = nums;        printf ("%d\n", p[0][1]);    int a = 10;    int B = 20;        int c = 30;        int *p = &a; *p = = = P[1];        Not so written int *ps[3] = {&a,&b,&c};    printf ("%d,%d,%d", *ps[0],*ps[1],*ps[2]); }/* defines a pointer data type that points to a one-dimensional array (* pointer name) [the number of elements of a one-dimensional array to point] pointer + integer = = value in the pointer + the length of the data type that is pointed to * integer */int main (int argc, const char * AR        Gv[]) {int nums[3][2] = {{1,2},{3,4},{5,6}};        int (*PS) [2];    PS = nums;//can be considered PS and nums are equivalent int num = ps[0][1];       printf ("%d\n", num);    printf ("%p\n", nums);        printf ("%p\n", nums+1);    printf ("%p\n", PS); PriNTF ("%p\n", ps+1);        for (int i =0; i < 3; i++) {for (int j = 0; J < 2; J + +) {printf ("%d", ps[i][j]);    } printf ("\ n"); }//Nums nums[0]///Same point: the corresponding address is the same//different points: pointer type is different//nums + 1 = nums + sizeof (Nums[0])//nums[0] + 1  = nums + sizeof (int)//sizeof (nums) Two-dimensional array takes up storage space Bytes//sizeof (Nums)/sizeof (int) in a two-dimensional array of how many int data int *p =    Nums[0];    for (int i = 0; i < sizeof (nums)/sizeof (int); i++) {printf ("%d", p[i]); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS Learning Journey---pointers are not difficult

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.