Pointer data sorting

Source: Internet
Author: User

These days, the written examination often encountered pointer questions, so I made a summary.

 

Pointer types in applications:
(1) int * ip; // (char * cp, float * fp, double * dp) Level 1 pointer variable
(2) int ** ip; // (char ** cp, float ** fp, double ** dp) Level 2 pointer variable
(3) const int * ip; // constant pointer
(4) int * const ip; // pointer constant
(5) const int * const icp; // constant pointer constant
(6) int * ip [10]; // pointer Array
(7) int (* ip) [10]; // array pointer
(8) int * f (int a); // char * copy (char * s1, char * s2) pointer Function
(9) int (* gp) (int); // function pointer
(10) int (* gp [10]) (int); // array of function pointers

The following is a one-by-one explanation of the situation during the 11 period.
(1) int * ip; // (char * cp, float * fp, double * dp) Level 1 pointer variable
Level-1 pointers are the most commonly used and the simplest pointers.
# Include <iostream>
Using namespace std;

Int main (void)
{
Char * p = "hello world ";
Int I = 0;
While (p [I]! = '\ 0 ')
Cout <p [I ++];
Cout <'\ n ';

Int * ip, iq;
Iq = 5;
Ip = & iq;
}
The above program has two problems: 1) Increase or decrease of pointers 2) define a pointer * only one pointer can be modified
The increment or decrement of a pointer is measured in units of the object size of this type (but it does not make sense to multiply or divide the pointer)
Adding 1 to the char pointer actually adds 1 byte
Adding 1 to the float pointer actually adds 4 bytes.
Adding 1 to the int pointer actually adds 4 bytes.
Define a pointer. * only one pointer can be modified.
Ip in the program is a pointer, while iq is an integer variable.

In many c/c ++ programs, the pointer and array are replaced and used, so that we think the pointer and the value are equivalent, but the array and the pointer are quite different, see the following example.
# Include <iostream>
Using namespace std;

Void fun (char a [2, 100])
{
Cout <sizeof (a) <'\ n'; // 4
}

Int main (void)
{
Char a [] = "hello world ";
A [0] = 'y ';
Char * p = "welcome to c ++ ";
// P [0] = 'X'; // the compiler cannot find this error, but it has an error at runtime.
Cout <sizeof (a) <'\ n'; // 12
Cout <sizeof (p) <'\ n'; // 4
Fun ();
}

After reading the above example, let's talk about the difference between arrays and pointers:
An array is either created in a static storage area (such as a global array) or on a stack. The array name corresponds to (rather than pointing to) a piece of memory, and its address and capacity remain unchanged during the lifetime, only the content of the array can be changed.
A pointer can point to any type of memory block at any time, and its feature is "variable". Therefore, we often use pointers to operate dynamic memory. Pointers are far more flexible than arrays, but they are more dangerous.
The size of character array a is 12 characters and its content is hello world \ 0. The content of a can be changed, for example, a [0] = 'y '. The pointer p points to the constant string "welcome to c ++" (in the static storage area, the content is welcome to c ++ \ 0 ),
The content of a constant string cannot be modified. In terms of syntax, the compiler does not think that the statement p [0] = 'X' is inappropriate, but this statement attempts to modify the content of the constant string and causes a running error,
Therefore, we often write char * p = "welcome to c ++" as const char * p = "welcome to c ++ ", in this way, the p [0] = 'X' statement will report an error during compilation.
The size of sizeof (a) is 12 (do not forget \ 0) sizeof (p) is the size of a pointer. When an array is passed as a function parameter, the array is automatically degraded to a pointer.

How does the pointer Parameter Pass the memory?
# Include <iostream>
# Include <string. h>
Using namespace std;

Char * getStr1 ()
{
Char a [] = "hello world ";
Return;
}

Char * getStr2 ()
{
Char * a = new char [12];
Strcpy (a, "hello world ");
Return;
}

Char * getStr3 ()
{
Char * a = "hello world ";
Return;
}

Void getStr4 (char * p, int num)
{
P = new char [num];
Strcpy (p, "hello world ");
}

Void getStr5 (char ** p, int num)
{
* P = new char [num];
Strcpy (* p, "hello world ");
}
Int main (void)
{
Cout <"getStr1" <getStr1 () <'\ n ';

Char * str2 = NULL;
Str2 = getStr2 ();
Cout <"getStr2" <str2 <'\ n ';
Str2 [0] = 'X ';
Cout <"getStr2" <str2 <'\ n ';
Delete [] str2;
Char * str3 = NULL;
Str3 = getStr3 ();
Cout <"getStr3" <str3 <'\ n ';
// Str3 [0] = 'X'; // The code is compiled, but an error occurs.

Char * str4 = NULL;
Int num = 50;
GetStr4 (str4, num );
Cout <"getStr4" <(str4 = NULL? "NULL": str4) <'\ n ';

Char * str5 = NULL;
GetStr5 (& str5, num );
Cout <"getStr5" <str5 <'\ n ';
}

GetStr1 () Outputs garbled characters, because the stack memory is returned in getStr1 (), so after exiting the function, char a [] is also cleared.
GetStr2 () can run correctly.
Although getStr3 () can run correctly, it returns constant data no matter where it is returned.
After getStr4 () is run, str4 is still NULL, because the char * p parameter is cleared after the getStr4 () function is run.
GetStr5 () can run correctly.

(2) int ** ip; // (char ** cp, float ** fp, double ** dp) Level 2 pointer variable
The relationship between Level 2 pointer variables and Level 1 pointer variables is similar to that between two-dimensional arrays and one-dimensional arrays.


(3) const int * ip; // constant pointer
(4) int * const ip; // pointer constant
(5) const int * const icp; // constant pointer constant
The three have some similarities. See the following example.
# Include <iostream>
Using namespace std;

Int main (void)
{
Const int a = 10;
Int B = 5;
Int c = 13;
Const int * ip = &;
Int * const cp = & B;
Const int * const icp = & c;

* Ip = 30; // error. The constant pointer cannot be changed to the constant. * The ip address can only be used as the right value.
Ip = & c; // correct. The pointer itself can be changed.
* Cp = 50; // correct. The pointer constant. The value pointed to by the pointer can be modified.
Cp = & c; // the pointer constant cannot be changed.
* Icp = 50; // error. The constant pointer constant cannot be changed to a constant.
Icp = & c; // error. The constant pointer constant cannot change the pointer itself.
}

(6) int * ip [10]; // pointer Array
(7) int (* ip) [10]; // array pointer
Pointer array: As the name implies, it is an array first, and then the elements of the array are pointers.
Array pointer: pointer to an array
# Include <iostream>
Using namespace std;

Int main ()
{
Int a = 5, B = 1, c = 10;
Int * p1 = & a, * p2 = & B, * p3 = & c;
Int * p [3] = {p1, p2, p3}; // pointer array, each element in the array is a pointer
For (int I = 0; I <3; I ++)
Cout <* p [I] <'\ n ';

Int (* ap) [3]; // array pointer
Int arr [4] [3] = {1, 2}, {2, 3 }};
Ap = arr;
For (int I = 0; I <4; I ++)
{
For (int j = 0; j <sizeof (ap [I])/sizeof (int); j ++)
Cout <ap [I] [j] <"";
Cout <'\ n ';
}
}

(8) int * f (int a); // char * copy (char * s1, char * s2) pointer Function
A pointer function is a pointer type.
For example, strcpy is often used in C.
Char * strcpy (char * Dest, const char * Src)
{
Assert (Dest! = NULL & Src! = NULL );
Char * tmp = Dest;
While (* Dest ++ = * Src ++ )! = '\ 0 ');
Return tmp;
}
When we see this function, we may feel that there is a return value. Why? Didn't the Src content have been copied to Dest? This is mainly to complete the chain operation, in order that the strcpy function can be used as the right value.

(9) int (* gp) (int); // function pointer
(10) int (* gp [10]) (int); // array of function pointers
# Include <iostream>
Using namespace std;

Void fun1 () {cout <"good" <'\ n ';}
Void fun2 () {cout <"better" <'\ n ';}
Void fun3 () {cout <"best" <'\ n ';}


Int main ()
{
Void (* gp) () = fun1; // function pointer
Gp ();

Void (* gpa [3]) (); // function pointer Array
Gpa [0] = fun1;
Gpa [1] = fun2;
Gpa [2] = fun3;
For (int I = 0; I <3; I ++)
Gpa [I] ();
}

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.