"C + + institute" 0726-cppide/level pointer/pointer array/function pointer/function pointer array/level two pointer

Source: Internet
Author: User

"To the programmer on the road."

For a developer, the ability to develop any module in a system is the embodiment of its core value.

For an architect, mastering the advantages of various languages and applying them to the system simplifies the development of the system and is the first step in its architectural career.

For a development team, in the short term to develop a user satisfaction of the software system is the core competitiveness of the embodiment.

Every programmer can not be complacent, to contact the new industry, new technology field, break through the ego.


Cppide

Use the MinGW compiler in MFC and Codeblocks. Executes the bat batch script in the system command.

First-level pointers

Pointer, struct struct, union union, enum enum

#include <stdio.h> #include <stdlib.h>void changenum (int num)//function parameters have a copy mechanism, create a new variable that holds the value passed over the parameter {num = 3; printf ("\nchangenum=%p", &num);} void Changepnum (int *p)//Create pointer to hold address {*p = 2;//* extract content based on address and type}void main2 () {int num = 10;printf ("\nmain num=%p", &num);//ch Angenum (num);//Pass Data changepnum (&num);//Pass Address printf ("\n%d", num); GetChar ();} void Main1 () {int num = 10;num = 5;//Direct int *p = #*p = 6;//indirectly modifies printf ("%d", num); GetChar ();}  void Test (int a[10])//array is an exception, rejecting the copy mechanism, when the array is treated as a parameter when the address {printf ("\ntest =%d", sizeof (a)); int b[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};p rintf ("\ntest b=%d", sizeof (b));//array size}void test1 (int a[10])//array is an exception, deny copy mechanism, array as parameter when the address {a[0] = 9;//array as parameters, Changing the array is equivalent to manipulating the outer array directly for (int i = 0; i < i++) {printf ("\ntest1= a[%d]=%d", I, A[i]);}} void test2 (int *p)//first-level pointer can accept the array's first address as a function parameter {*p = 9;for (int *px = p; px < p + Ten; px++) {printf ("\ntest2 =%d,%p", *PX, p x)///Print data also address}}void main3 () {int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};p rintf ("\nmain a=%d", sizeof (a));//array size printF ("\n%d", sizeof (a)/sizeof (int)); test2 (a);//array as parameters using for (int i = 0; i <; i++) {printf ("\ n main= a[%d]=%d", I, A[i]) ;} System ("Pause");} void Main4 () {int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};p rintf ("%p", a);//a is a constant pointer,//a = a;int *p = a;//int* type printf ("\ n p=%d,a=%d ", sizeof (p), sizeof (a));//4 byte, the array name compiler made a special handling//subscript loop for (int i = 0; i <; i++) {printf (" \n%d,%p,%d,%p ", P[i], &A mp;p[i],* (p+i), p+i);//p[i] equivalent to * (p+i) &p[i]=p+i}//pointer loop for (int *px = a + 9; px >= A; px--) {printf ("\n%d,%p", *PX, px);} GetChar ();} int Go ()//return also has a copy mechanism, register {int a = 3;//auto automatic variable printf ("%p", &a);p rintf ("\ n"); return A;} void Main5 () {//printf ("%d,%p", Go (), &go ()), return value of function can not take address int x = Go ();p rintf ("%d", x); int x1 = Go ();p rintf ("\n%d", X1) ; GetChar ();} int * GO1 ()//return also has a copy mechanism, register {int a = 3;//auto automatic variable printf ("%p", &a);p rintf ("\ n"); return &a;} void Main6 () {int *p = GO1 (); printf ("\n\n\n\n");//After the function is completed, the memory is recycled, not using or the original value, the value of the memory after use changed printf ("%d,%p", *p, p);// Print address and content GetChar ();} VoiD main7 () {char *p = "tasklist & Pause";//pointer storage address//*p = ' a ';//constant string cannot be modified//printf ("%p", p); char *px = P;while (*px! = ') () {Putchar (*px);p x++;//pointer move forward}system (p);}    void Main8 () {int num = 10; int *p = #//pointer type, type determines the stride size printf ("%d", *p); GetChar ();} void Main9 () {int num = -1;//1111 1111 1111 1111 1111 1111 1111 1111 1111 1111//unsigned All data 4294967295//signed is the first sign bit 1 Represents a negative number, all that remains is the data unsigned int *p1 = #//The type of pointer determines how int *P2 = #printf ("%u,%d", *p1, *P2); GetChar ();} struct info{int num;float score;}; void Main10 () {struct info info1;printf ("%d,%f", Info1.num = ten, info1.score = 29);//The value of an assignment expression is equal to the value of the assigned variable struct info *p1 = &am p;info1;printf ("\n%d,%f", (*P1). Num, (*P1). Score);p rintf ("\n%d,%f", P1->num, P1->score);//pointer access struct body two forms of struct Info *p2= (struct info*) malloc (sizeof (struct info));p 2->num = 18;p2->score = 19.8;printf ("\n%d,%f", (*P2). Num, (* p2). score);//Print Data printf ("\n%d,%f", P2->num, P2->score); GetChar ();} void Main11 () {struct info *p2 = (struct info*) malloc (sizeof (struct info)//build dynamic array int i = 0;for (struct info *px = p2; px < P2 + 5; px++)//pointer loop {px->num = I;px->score = i + 3.5;i++;p r intf ("\n%d,%f", Px->num, Px->score);//pointer access}for (int j = 0; J < 5; J + +) {P2[j].num = i + 3;p2[j].score = i + 8.5;i+ +;p rintf ("\n%d,%f", P2[j].num, P2[j].score);//subscript}getchar ();}  Union un{int num;//4 bytes of float sh; A common body wearing a pair of trousers};void Main12 () {union un uni1;uni1.num = 4;printf ("%d", uni1.num); uni1.sh = 3;printf ("\n%d", uni1.num); Union un *p = &uni1;p->num;p->sh;//moment only one variable exists getchar ();} Enum myenum{creeps = 0, monitor = 1, platoon leader = 2, Commander, Commander, Commander, Commandant =10};void main13 () {enum MyEnum myenum1 = Commander; printf ("%d", myenum1); enum M Yenum *p = &myenum1;printf ("\n%d", *p); GetChar ();} #define P (x) (x) * (x)//implement function, replace 1+3 *1+3#define getname (x) #x//main "main" void Main () {//Pass function name Main, "main" p rintf ("%p", Main);p rintf ("\n%d", p (1+3));p rintf ("\n%s", GetName (Main)); GetChar ();}
Array of pointers

#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h>void main1 () {Time  _t ts;unsigned int randdata = time (&ts);//Gets the current transition to an unsigned data srand (randdata);//random number seed int a[10] = {0};int *p[10];for (int i = 0; I < 10; i++) {P[i] = &a[i];//pointer corresponds to the address of each element of the array}while (1) {for (int i = 0; i <; i++) {A[i] = rand ()% 1000;//take the random number printf ("\n%d" , A[i]);//print array each element}sleep;p rintf ("\ n"), for (int i = 0; i <; i++) {if (*p[i] <=) {*p[i] + = 100;//Add Blood}printf ("\n%d", *p[i]);} Sleep (5000); }}void main2 () {char * p[] = {"Calc", "notepad", "MSPaint", "Write", "Tasklist & Pause"};int n = sizeof (p)/sizeof (c Har *); for (int i = 0; i < n; i++) {printf ("\n%s", P[i]); system (P[i]);}}

function pointer/function pointer array

#include <stdio.h> #include <stdlib.h>static int jia (int a, int b)//qualification only this file uses {return a + B;} static  int Jian (int a, int b) {return a-A;} static int Cheng (int a, int b) {return a * b;} static  int chu (int a, int b) {return a/b;} void Main3 () {int (*p) (int a, int b) =jia;//the address of the stored function pointer//jia = Jia; The function name is a constant pointer to printf ("%d", p (1, 2));p = jian;printf ("\n%d", p (1, 2)); GetChar ();} void Main4 () {//int* a;//int* a[10];int (*p[4]) (int a, int b) = {Jia, jian, Cheng, chu};for (int i = 0; i < 4; i++) {pri NTF ("\n%d", P[i] (10, 2));} GetChar ();}

Second-level pointers
#include <stdio.h> #include <stdlib.h>//change a variable, you need the address of the variable, the address of the pointer variable//double **P1 = &p; The secondary pointer stores the address of the first-level pointer double  db = 3.5;double  bd = 4.5;void change (double *p)//Create a new pointer to store the value you passed {p = &bd;printf ("\ Nchange=%p ", &p);p rintf (" \nchange=%f ", *p);} void  CHANGEP (double **pp) {*pp = &bd;//change pointer}void main1 () {Double *p = &db;printf ("\nmain=%p", &p);p rintf ("\n%f", *p),//p = &bd;changep (&p);p rintf ("\n%f", *p); GetChar ();}

#include <stdio.h> #include <stdlib.h>void  run (char *p[5])//array does not have a copy mechanism, delivery is address {printf ("\nrun=%d", sizeof (P));//4 bytes for (char **pp = p; pp < p + 5; pp++) {System (*PP);}} void  run1 (char **px)//array does not have a copy mechanism, pass is address {printf ("\nrun=%d", sizeof (px)),//4 bytes for (char **pp = px; pp < px + 5; pp++) { System (*PP);}} void Main () {char *p[5] = {"Calc", "notepad", "MSPaint", "Tasklist &pause", "write"};p rintf ("\nmain=%d", sizeof (p)); r UN1 (P); GetChar ();} void Mainx () {char *p[5] = {"Calc", "notepad", "MSPaint", "Tasklist &pause", "Write"};//for (int i = 0; i < 5; i++ )//{//system (P[i]);//Subscript mode//}//for (int i = 0; i < 5; i++)//{//system (* (P + i)); subscript//}//Polls An array, requires a pointer, polls an array of pointers, Requires a level two pointer for (char **pp = p; pp < p + 5; pp++) {System (*PP);}}


|=========== Wu Yingjiang csdn Blog Column ==============|

|== the content of the A/C + + College column (not updated regularly) ===|

|== Linux-Driven development explores the secrets of Linux's underlying ==========|

|== basic knowledge of Java Basic Learning =====|

| ====== Daily Progress a little, healthy and happy every day ========|


Copyright notice: This blog all articles are original, welcome to Exchange, welcome reprint, reprint do not tamper with the content, and indicate the source, thank you!

"C + + institute" 0726-cppide/level pointer/pointer array/function pointer/function pointer array/level two pointer

Related Article

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.