Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.

Similar presentations


Presentation on theme: "Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training."— Presentation transcript:

1 Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training

2 Training C/C++EcoSoftware 2 Pointer and Pointer with Arrays o Introduction Pointer. o Declaring pointer variable. o Pointer with operator o Pointer Assignment o Pointer as arguments o Pointers to Constants o Array and pointer o Function pointer.

3 Training C/C++EcoSoftware 3 Introduction Pointer o Variables that can store addresses are called pointers. o The address that’s stored in a pointer is usually that of another variable o int number = 5; o int *P = &number;

4 Training C/C++EcoSoftware 4 Declaring pointer variable o Using statement:  Datatype *namePointer; o Example :  int *p ; // pointer to only integers  double *p ; // pointer to only doubles  char *p ; // pointer to only characters

5 Training C/C++EcoSoftware 5 The address and indirection Operators o The address operators  Declaring a pointer variable set aside space for pointer. int *p;  Must be init p before we use it. p = &i;

6 Training C/C++EcoSoftware 6 The address and indirection Operators o The indirection Operators.  Use the *(indirection) operator to access what’s stored in the object.  Example. int i = 5; int *p = &i; printf(“%d”,*p); printf will be display value of i, not address of i.  As long as p point to i.  *p is an alias for i.

7 Training C/C++EcoSoftware 7 Pointer Assignment o Use of the assignment operator to copy pointers.  int i, *p, *q;  p = &i; The address of i is copied into p;  q = p; This statement copies the content of p (address of i) into q *p = 1; *q = 2;

8 Training C/C++EcoSoftware 8 Pointer Assignment o Example 1  p = &i;  q = &j;  i = 1; o Example 2 o *p = *q;  Copy value that p pointers to (the value i) into the object that q point to (the variable j)

9 Training C/C++EcoSoftware 9 Using a pointer with scanf() o Example.  int value = 0;  int *pvalue = NULL;  pvalue = &value; /* Set pointer to refer to value */  printf ("Input an integer: ");  scanf(" %d", pvalue); /* Read into value via the pointer */  printf("\nYou entered %d\n", value);

10 Training C/C++EcoSoftware 10 Pointer as arguments o Using a pointer use to argument for function.  Example.  Prototype of decompose void decompose(double, long *, double *)  Call decompose decompose(3.14159, &i, &d);

11 Training C/C++EcoSoftware 11 Pointers to Constants o Use the const keyword when you declare a pointer to indicate that the value pointed to must not be changed.  long value = 9999L;  const long *pvalue = &value; /* Defines a pointer to a constant */  *pvalue = 8888L; /* Error - attempt to change const location */ o You have only asserted that what pvalue points to must not be changed. o You are quite free to do what you want with value:  value = 7777L;  long number = 8888L;  pvalue = &number; /* OK - changing the address in pvalue */

12 Training C/C++EcoSoftware 12 Constant Pointers o The address stored in a pointer cannot be changed. o using the const keyword slightly differently in the declaration of the pointer  int count = 43;  int *const pcount = &count; /* Defines a constant */ o Can’t changed address of a pcount.  int item = 34;  pcount = &item; /* Error - attempt to change a constant pointer */ o Can be change the value that pcount points  *pcount = 345; /* OK - changes the value of count */

13 Training C/C++EcoSoftware 13 Arrays and Pointers o Pointer Arithmetic  Use pointer can point to array element. int a[10], *p; p = &a[0];  Use can store 5 into a[0] by writing. *p = 5;

14 Training C/C++EcoSoftware 14 Arrays and Pointers o Adding an integer value to a pointer  Example :  int a[10], *p; *q;  p = &a[2];  q = p + 3;  p += 6;

15 Training C/C++EcoSoftware 15 Arrays and Pointers o Subtructing an integer from a pointer.  If p pointer to the array element a[i], then p-j pointer to a[i-j];  Example. int *p = &a[8]; q = p-3; p -= 6;

16 Training C/C++EcoSoftware 16 Example Pointer.

17 Training C/C++EcoSoftware 17 Arrays and Pointers o Pointer to compound literals  A pointer to point to an element with in an array created by compound literals.  Example. int *p = (int []){3,0,3,4,1}; p point to first element of five element array {3,0,3,4,1}.  Other way: int a[] = {3,0,3,4,1}; p = &a[0];

18 Training C/C++EcoSoftware 18 Using pointers for array processing o Combining the * and ++ Operators  Using combine the * (inderection) and ++ operators in statement in process array elements. a[i++] = j;  Using p is pointing to an array element. *(p++) = j;  Notes :

19 Training C/C++EcoSoftware 19 Using pointers for array processing o Combining the * and ++ Operators  Example : Total array with using pointer. –int a[] = {3,5,7,10,23}; –int *p; »Using for for(p=&a[0]; p<&a[N]; p++) { sum += *p; } Using while p = &a[0]; while (p<&a[N]) sum += *p++;

20 Training C/C++EcoSoftware 20 Using array name as a pointer o The name of an array can be use as a pointer to the first element in the array.  Example : int a[10]; *a = 7; /* store 7 in a[0] */. *(a+1) = 12 /* store 12 in a[1]*/; *(a+n) = k /* store k in a[n] */ o Using for, while to view data with array.  Example : for(p = &a[0]; p<&a[n]; p++) { sum +=*p; }

21 Training C/C++EcoSoftware 21 Example Using pointer with array o Reversing a series of numbers.

22 Training C/C++EcoSoftware 22 Pointer with multidimensional array. o Processing the elements of multidimension arrays. #define NUM_ROWS 10; # define NUM_COLS 10; int a[NUM_ROWS][NUM_COLS]; int row, col; for(int row=0; row< NUM_ROWS ; row++) for(int col=0; col < NUM_ROWS ; col++) a[row][col] = 0;

23 Training C/C++EcoSoftware 23 Pointer with multidimensional array. o Using pointer with multidimensional array. #define NUM_ROWS 10; # define NUM_COLS 10; int a[NUM_ROWS][NUM_COLS]; int *p; for(p=&a[0][0]; p< &a[NUM_ROWS-1][NUM_COLS]; p++) *p= 0;

24 Training C/C++EcoSoftware 24 Processing the rows of a multidimensional array o Example. o Using pointer with multidimensional array. #define NUM_ROWS 10; # define NUM_COLS 10; int a[NUM_ROWS][NUM_COLS]; int *p, i; for(i = 0; i<NUM_ROWS; i++) for(p=a[i]; p<a[i] + NUM_COLS; p++) *p= 0;

25 Training C/C++EcoSoftware 25 Processing the colum of a multidimensional array o Example. #define NUM_ROWS 10; # define NUM_COLS 10; int a[NUM_ROWS][NUM_COLS]; int (*p)[NUM_COLS], i; for(i = 0; NUM_COLS; i++) for(p=&a[0]; p<&a[NUM_ROWS]; p++) (*p)[i] = 0

26 Training C/C++EcoSoftware 26 Using the name of multidimension array as a pointer o Example.  Not using array name. for(p=&a[0]; p<&a[NUM_ROWS]; p++) (*p)[i] = 0;  Using array name. for(p = a; p<a + NUM_ROWS; p++) –(*p)[i] = 0;

27 Training C/C++EcoSoftware 27 Example Multidimentional array with pointer

28 Training C/C++EcoSoftware 28 Function Pointer o Declaration function Pointer.  Datatype (*name function)();  Example : int(*cmp)(); // function pointer has integer type float(*fcmp)(); // function pointer has float type.

29 Training C/C++EcoSoftware 29 Example function pointer. #include intadd( int a, int b) { return(a+b);} intsubtruct( int a, int b) { return(a-b);} intmultiable( int a, int b) { return(a*b);} intmodul( int a, int b) { return(a%b);} void main(void) { int (*cmp)(); /* function pointer has int type*/ int a, b; printf(“\n Input a=”); scanf(“%d”, &a); printf(“\n Input b=”); scanf(“%d”, &b); cmp=add; // Using function pointer printf(“\n Addition a + b =%d”, cmp(a,b));

30 Training C/C++EcoSoftware 30 Example function pointer. cmp = subtruct; printf(“\n Subtruction a - b =%d”, cmp(a,b)); cmp = multiable; printf(“\n Multiable a * b =%d”, cmp(a,b)); cmp = modul; printf(“\n modul(a, b) =%d”, cmp(a,b)); getch(); }

31 Training C/C++EcoSoftware 31 Thank You End


Download ppt "Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training."

Similar presentations


Ads by Google