Dark Horse programmer _ios Development _objective-c Learning Notes _ pointer review

Source: Internet
Author: User

1. Pointers

I was in the analysis of my own programming errors found in the pointer problem, I think it is necessary to review the pointer, after all, the pointer is the C language series of difficulties.

Pointers are the essence of C language, but many beginners are often not very deep in the concept of pointers, so that after learning the concept and use of pointers over time more and more blurred, feel the pointer is difficult to grasp, today I will review the concept and use of pointers.

2. What is a pointer

The essence of a pointer is a variable that holds the address of the variable, simply saying that the variable p stores the address of the variable A, then p can be called a pointer to the variable A, or P points to a. When we visit the A variable is actually the program according to a to obtain a corresponding address, and then to the address of the corresponding storage space to get a value, this way we call "direct reference", and when we get a through p, we first have to convert p to p corresponding storage address, Then according to the address to its corresponding storage space to get the storage content, its content is actually a address, and then according to this address to the corresponding storage space to obtain the corresponding content, this content is a value, this through P find a corresponding address and then the way to become "indirect reference."

The 2.1 sample code is as follows:

int a =ten; // the address of a is ffeedd01 int *p; *p=a; // The value of p at this point is the address of a ffeedd01

2.2 Assignment of pointers
#include <stdio.h>intMainintargcConst Char*argv[]) {        intA=Ten; int*p; P=&a;//You can also assign values directly to pointer variables: int *p=&a;printf"a=%d,p=%d\n", a,*p);//Results: a=10,p=10

*p= -; printf ("a=%d,*p=%d\n", a,*p);//results: a=20,p=20 intb=8; CharC=1; int*q=&C;printf"c=%d,q=%d\n", C, *q);//Result: c=1,q=2049, why is the value of Q not 1? return 0;}

Attention:

*int *p; * Just means the p variable is a pointer variable; while printing *p, the * is an operator in *p, which represents the storage space of the variable that the P pointer points to (the current storage is 10), and we also see the *p==a; Modified * P also modifies the contents of the storage space that P points to, and modifies a so that the second print a=20;

* The type pointed to by the pointer must be the same type that was declared when the pointer was defined, the pointer q is defined as an int and pointed to the char type, and the result output *q prints 2049, (assuming that the pointer length is 2 bytes under the 16-bit compiler)

     int b=8;    // address ffeedd0c                  // address ffeedd0d    Char 1;   // address ffeedd0b    int *q=&c;      // ffeedd0b

Because the local variables are stored in the stack stacks, so store B and then store C, Q, when printing *q, in fact, the address corresponding to the Q point of the space to start taking two bytes of data (because the definition of Q when it points to an int, in the 16-bit compiler, the length of the int type is 2 bytes) , just defined B and C space continuous, so take to one of B's bytes, the last *p binary storage as "0000100000000001", the decimal means is 2049;

* The pointer variable occupies space independent of the type of variable it points to.

3. Arrays and pointers

The storage of the array is contiguous, and the array name is the starting address of the array, so that the array and the pointer have a relationship:

#include <stdio.h>voidChangeValue (inta[]) {a[0]=2;}voidChangeValue2 (int*p) {p[0]=3;}intMainintargcConst Char*argv[]) {    inta[]={1,2,3}; int*p=&a[0];//equivalent to: *p=a;//p The starting address of the array//p points to a[0],p+1 pointing to a[1], and so on, so we can also remove the array element by pointer     for(intI=0;i<3;++i) {        //printf ("a[%d]=%d\n", I,a[i]);printf"a[%d]=%d\n", i,* (p+i));    }    /*output Result: a[0]=1 a[1]=2 a[2]=3*/ChangeValue (p);//equivalent to: ChangeValue (a)     for(intI=0;i<3;++i) {printf ("a[%d]=%d\n", I,a[i]); }    /*output Result: a[0]=2 a[1]=2 a[2]=3*/ChangeValue2 (a);//equivalent to: ChangeValue2 (p)     for(intI=0;i<3;++i) {printf ("a[%d]=%d\n", I,a[i]); }    /*output Result: a[0]=3 a[1]=2 a[2]=3*/        return 0;}

From the above example we can draw the following conclusions:

* The array name a equals &a[0] equals p;

* If P points to an array, then p+1 points to the next element of the array, noting that the length of the p+1 movement is not fixed, depending on the type of data the P points to;

* The argument can use the array name or pointer, regardless of the function's parameter array or pointer;

4. Strings and pointers

In the C language, where strings are character arrays, the strings and arrays are as follows:

#include <stdio.h>intMainintargcConst Char*argv[]) {    Chara[]="Itheima"; printf ("%s\n", a);//results: Itheima
Charb[]="Itheima"; Char*p=b; printf ("b=%s,p=%s\n", b,p);//results: B=itheima,p=itheima//The pointer stores the address, and the array name stores the address, since a character array can represent a string, and a pointer to a character can also be used to define a string more easily, as in the following way Char*c="Itheima";//equivalent to char c[]= "Itheima";printf"c=%s\n", c);//results: C=itheima return 0;}

5. Function pointers

Let's look at the function that returns the pointer type data, the pointer type is also the C language data type, and the following is an example of a program that converts a string to uppercase characters:

#include <stdio.h>Char* ToUpper (Char*a) {    Char*b=a;//The original address is retained because the subsequent loop changes the string's original address    intlen='a'-'A';//uppercase and lowercase ASCII code differences equal     while(*a!=' /') {//whether the character ends        if(*a>'a'&&*a<'Z'){//if it is a lowercase character* (a++)-= Len;//*a represents the corresponding character of the array (-32 to lowercase), and the a++ represents the move to the next character        }    }       returnb;}intMainintargcConst Char*argv[]) {    Chara[]="Itheima"; Char*p=ToUpper (a); printf ("%s\n", p);//results: Itheima    return 0;}

Note: Everyone knows that a function can have only one return value, and if you need to return multiple values, then what do we do, we just need to pass the pointer as a function parameter:

#include <stdio.h>intOperateintAintBint*c) {    *c=a-b; returnA +b;}intMainintargcConst Char*argv[]) {    intA=1, b=2, C,d; D=operate (A, B, &c); printf ("a+b=%d,a-b=%d\n", d,c);//results: A+b=3,a-b=-1    return 0;}

The difficulty: functions also need to be stored in memory, functions also have a starting address, function name is the starting address of the function. function pointers are defined as: return value type (* pointer variable name) (parameter 1, parameter 2), function pointer is actually equivalent to this function, function of the operation can be done by pointers. Since pointers are the data types of the C language and can be used as parameters and as return values, then of course function pointers can also be used as parameters and return values for functions:

#include <stdio.h>
intSumintAintb) { returnA +b;}
intSubintAintb) { returnA-b;}
//function pointers are passed as parametersintOperateintAintBint(*p) (int,int)){ returnP (A, b);}intMainintargcConst Char*argv[]) { intA=1, b=2; int(*p) (int,int) =sum;//The function name is the first address of the letter . intC=P (A, b); printf ("a+b=%d\n", c);//results: a+b=3//function as a parameter passprintf"%d\n", Operate (A, B, sum));//Results: 3printf"%d\n", Operate (A, B, sub));//results: -1 return 0;}

Although I know the concept of this function pointer, I have not yet encountered an occasion where I need to use a function pointer. But a function pointer can be passed as a function parameter, which is too powerful.

Dark Horse programmer _ios Development _objective-c Learning Notes _ pointer review

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.