pointers

advertisement
C/C++ Training
Programming C/C++ on Eclipe
Trình bày : Ths HungNM
Pointer and Pointer with Arrays








Introduction Pointer.
Declaring pointer variable.
Pointer with operator
Pointer Assignment
Pointer as arguments
Pointers to Constants
Array and pointer
Function pointer.
EcoSoftware
Training C/C++
2
Introduction Pointer




Variables that can store addresses are called pointers.
The address that’s stored in a pointer is usually that of
another variable
int number = 5;
int *P = &number;
EcoSoftware
Training C/C++
3
Declaring pointer variable

Using statement:


Datatype *namePointer;
Example :
int *p ; // pointer to only integers
 double *p ; // pointer to only doubles
 char *p ; // pointer to only characters

EcoSoftware
Training C/C++
4
The address and indirection Operators

The address operators

Declaring a pointer variable set aside space for pointer.
• int *p;

Must be init p before we use it.
• p = &i;
EcoSoftware
Training C/C++
5
The address and indirection Operators

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.

EcoSoftware
Training C/C++
6
Pointer Assignment

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;
EcoSoftware
Training C/C++
7
Pointer Assignment

Example 1
p = &i;
 q = &j;
 i = 1;



Example 2
*p = *q;

Copy value that p pointers to (the value i) into the
object that q point to (the variable j)
EcoSoftware
Training C/C++
8
Using a pointer with scanf()

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);

EcoSoftware
Training C/C++
9
Pointer as arguments

Using a pointer use to argument for function.

Example.

Prototype of decompose
• void decompose(double, long *, double *)

Call decompose
• decompose(3.14159, &i, &d);
EcoSoftware
Training C/C++
10
Pointers to Constants

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 */
You have only asserted that what pvalue points to must
not be changed.
You are quite free to do what you want with value:



value = 7777L;
long number = 8888L;
pvalue = &number; /* OK - changing the address in pvalue */
EcoSoftware
Training C/C++
11
Constant Pointers


The address stored in a pointer cannot be changed.
using the const keyword slightly differently in the
declaration of the pointer
int count = 43;
 int *const pcount = &count; /* Defines a constant */


Can’t changed address of a pcount.



int item = 34;
pcount = &item; /* Error - attempt to change a constant pointer */
Can be change the value that pcount points

*pcount = 345; /* OK - changes the value of count */
EcoSoftware
Training C/C++
12
Arrays and Pointers

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;
EcoSoftware
Training C/C++
13
Arrays and Pointers

Adding an integer value to a pointer
Example :
 int a[10], *p; *q;
 p = &a[2];
 q = p + 3;


p += 6;
EcoSoftware
Training C/C++
14
Arrays and Pointers

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;
EcoSoftware
Training C/C++
15
Example Pointer.
EcoSoftware
Training C/C++
16
Arrays and Pointers

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];
EcoSoftware
Training C/C++
17
Using pointers for array processing

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 :
EcoSoftware
Training C/C++
18
Using pointers for array processing

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++;
EcoSoftware
Training C/C++
19
Using array name as a pointer

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] */

Using for, while to view data with array.

Example :
• for(p = &a[0]; p<&a[n]; p++)
{
sum +=*p;
}
EcoSoftware
Training C/C++
20
Example Using pointer with array

Reversing a series of numbers.
EcoSoftware
Training C/C++
21
Pointer with multidimensional array.

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;
EcoSoftware
Training C/C++
22
Pointer with multidimensional array.

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;
EcoSoftware
Training C/C++
23
Processing the rows of a multidimensional array


Example.
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;
EcoSoftware
Training C/C++
24
Processing the colum of a multidimensional array

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
EcoSoftware
Training C/C++
25
Using the name of multidimension array as a pointer

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;
EcoSoftware
Training C/C++
26
Example Multidimentional array with pointer
EcoSoftware
Training C/C++
27
Function Pointer

Declaration function Pointer.
Datatype (*name function)();
 Example :

• int
(*cmp)(); // function pointer has integer type
• float (*fcmp)(); // function pointer has float type.
EcoSoftware
Training C/C++
28
Example function pointer.
#include <stdio.h>
int add( int a, int b) { return(a+b);}
int subtruct( int a, int b) { return(a-b);}
int multiable( int a, int b) { return(a*b);}
int modul( 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));
EcoSoftware
Training C/C++
29
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();
}
EcoSoftware
Training C/C++
30
End
• Thank You
EcoSoftware
Training C/C++
31
Download