12438_Lect 26-28
-
Upload
rounit-roy -
Category
Documents
-
view
214 -
download
0
Transcript of 12438_Lect 26-28
-
8/12/2019 12438_Lect 26-28
1/17
Pointers
-
8/12/2019 12438_Lect 26-28
2/17
POINTERS
Pointers are variables that contain memory
addressesas their values.
A variable name directlyreferences a value.
A pointer indirectlyreferences a value.
Referencing a value through a pointer is
called indirection.
A pointer variable must be declared before it
can be used.
-
8/12/2019 12438_Lect 26-28
3/17
Concept of Address and
Pointers Memory can be
conceptualized as
a linear set of data
locations. Variables reference
the contents of a
locations
Pointers have a
value of the
address of a given
location
Contents1
Contents11
Contents16
ADDR1ADDR2ADDR3ADDR4ADDR5
ADDR6***
ADDR11
**
ADDR16
-
8/12/2019 12438_Lect 26-28
4/17
POINTERS
Examples of pointer declarations:
FILE *fptr;
int *a;
float *b;
char *c;
The asterisk, when used as above in thedeclaration, tells the compiler that the variable
is to be a pointer, and the type of data that the
pointer points to, but NOT the name of the
variable pointed to.
-
8/12/2019 12438_Lect 26-28
5/17
POINTERS Consider the statements:
#include
int main ( )
{
FILE *fptr1 , *fptr2 ; /* Declare two file pointers */
int *aptr ; /* Declare a pointer to an int */
float *bptr ; /* Declare a pointer to a float */
int a ; /* Declare an int variable */
float b ; /* Declare a float variable */
-
8/12/2019 12438_Lect 26-28
6/17
POINTERS
/* Then consider the statements: */
aptr = &a ;
bptr = &b ;fptr2 = fopen ( "my_out_file.dat" , "w" ) ;
fptr1 = fopen ( "my_in_file.dat" , "r" ) ;
if ( fptr1 != NULL )
{
fscanf ( fptr1, "%d%f" , aptr , bptr ) ;
-
8/12/2019 12438_Lect 26-28
7/17
POINTERS
fprintf ( fptr2, "%d %d\n" , aptr , bptr ) ;
fprintf ( fptr2, "%d %f\n" , *aptr , *bptr ) ;
fprintf ( fptr2, "%d %f\n" , a , b ) ;
fprintf ( fptr2, "%d %d\n" , &a , &b ) ;return 0 ;
}
Assuming that the above is part of a program that runswithout error and the input file does open, what wouldbe printed to the file
By the first fprintf? By the second fprintf?
By the third fprintf? By the fourth fprintf?
-
8/12/2019 12438_Lect 26-28
8/17
Use of & and *
When is & used?
When is * used?
& is "address operator" which gives or produces
the memory address of a data variable
* is "dereferencing operator" which provides the
contents in the memory location specified by a
pointer
-
8/12/2019 12438_Lect 26-28
9/17
POINTERSaptr = &a ;
bptr = &b ;
fptr2 = fopen ( "my_out.dat" , "w" ) ;
fptr1 = fopen ( "my_in.dat" , "r" ) ;
if ( fptr1 != NULL )
{
fscanf (fptr1, "%d%f", aptr, bptr);
fprintf (fptr2, "%d %d\n", aptr, bptr ) ;
fprintf (fptr2, "%d %f\n", *aptr, *bptr ) ;
fprintf (fptr2, "%d %f\n", a , b ) ;
fprintf (fptr2, "%d %d\n", &a , &b ) ;
return 0 ;
}
}
/* input file */
5 6.75
/* output file */
1659178974 1659178976
5 6.750000
5 6.750000
1659178974 1659178976
-
8/12/2019 12438_Lect 26-28
10/17
Pointer Arithmeticpointer+ number pointernumber
E.g.,pointer+ 1 adds 1 something to a pointer
char *p;char a;
char b;
p = &a;
p += 1;
int *p;int a;
int b;
p = &a;
p += 1;
In each, p now points to b(Assuming compiler doesnt
reorder variables in memory)
Adds 1*sizeof(char) tothe memory address
Adds 1*sizeof(int) tothe memory address
Pointer arithmetic should be used cautiously
-
8/12/2019 12438_Lect 26-28
11/17
Arrays and Pointers
Dirty secret:Array pointer to the initial (0th) array
element
a[i] *(a+i)
An array is passed to a function as a
pointer
The array size is lost!
Usually bad style to interchange arrays
and pointers
Avoid pointer arithmetic!
Really int *array
int
foo(int array[],
unsigned int size)
{
array[size - 1]
}
int
main(void){
int a[10], b[5];
foo(a, 10) foo(b, 5)
}
Must explicitlypass the size
Passing arrays:
-
8/12/2019 12438_Lect 26-28
12/17
Arrays and Pointersintfoo(int array[],
unsigned int size)
{
printf(%d\n, sizeof(array));
}
int
main(void)
{
int a[10], b[5]; foo(a, 10) foo(b, 5)
printf(%d\n, sizeof(a));
}
What does this print?
What does this print?
8
40
... because arrayis reallya pointer
-
8/12/2019 12438_Lect 26-28
13/17
Arrays and Pointers
int i;
int array[10];
for (i = 0; i < 10; i++)
{
array[i] = ;
}
int *p;
int array[10];
for (p = array; p < &array[10]; p++)
{
*p = ;
}
These two blocks of code are functionally equivalent
-
8/12/2019 12438_Lect 26-28
14/17
Pointers and Functions
Pointers can be used to pass addresses of variables to
called functions, thus allowing the called function to
alter the values stored there.
We looked earlier at a swap function that did not
change the values stored in the main program because
only the values were passed to the function swap.
This is known as "call by value".
-
8/12/2019 12438_Lect 26-28
15/17
Pointers and Functions
If instead of passing the values of thevariables to the called function, we pass theiraddresses, so that the called function can
change the values stored in the callingroutine. This is known as "call by reference"since we are referencingthe variables.
The following shows the swap functionmodified from a "call by value" to a "call byreference". Note that the values are now
actually swapped when the control isreturned to main function.
-
8/12/2019 12438_Lect 26-28
16/17
Pointers with Functions
(example)
#include
void swap ( int *a, int *b ) ;
int main ( )
{int a = 5, b = 6;
printf("a=%d b=%d\n",a,b) ;
swap (&a, &b) ;
printf("a=%d b=%d\n",a,b) ;
return 0 ;
}
void swap( int *a, int *b )
{
int temp;
temp= *a; *a= *b; *b = temp ;printf ("a=%d b=%d\n", *a, *b);
}
Results:
a=5 b=6a=6 b=5
a=6 b=5
-
8/12/2019 12438_Lect 26-28
17/17