Professional Documents
Culture Documents
Rohit Khokher
0x9FFF:000F
What does this number really mean?
0xFFFF:000F
0xFFFF0 + 0x 000F = 0xFFFFF
1 Mb = 1,048,576
1 to 1,048,576
0xFFFFF
456
ADDR1
ADDR2
ADDR3
ADDR4
ADDR5
ADDR6
*
*
*
Contents
.
*
*
1048576
Contents
POINTERS DECLARATION
Examples of pointer declarations:
int *a;
float *b; What does float *b;
char *c; What does char *c;
mean
mean
0xFFFFF
456
Conten
t/value
POINTERS
Consider the statements:
#include <stdio.h>
int main ( )
What is
{ the difference between int *aptr; and int a; declaratio
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 */
0xFFFFF
0xFFFFF
aptr = &a ;
bptr = &b ;
float x;
POINTERS
float
*px;
x=
px?
POINTERS
We must associate a pointer to a
particular data type. For example, we
can't assign the address of a short int
0xFFFFF
0xFFFFE
to a long int. Why?
Int x = 1, y = 2;
Int *ip;
ip= &x;
y = *ip;
*ip= 3;
1
3
0xFFFFE
2
1
POINTERS
When a pointer is
declared it does not
point anywhere. We
must set it to point
somewhere before its
use. The statements
int*p;
*p = 10;
will generate an error.
Why?
int*p, *q, x, y;
p = &x;
q = &y;
*p = 10;
*q = 20;
p = q;
*p = *q;
10
20
POINTERS
a=5;
b=6.75;
aptr = &a ;
bptr = &b ;
printf ("%d %d\n", aptr,
bptr ) ;
printf ("%d %f\n", *aptr, *bptr
);
printf ("%d %f\n",
a,
b);
printf ( "%d %d\n", &a , &b )
;
return 0 ;
}
}
1659178974 1659178976
5 6.750000
5 6.750000
1659178974 1659178976
POINTERS
double x = 3.14;
*p = &x;
3.14
Address of X
p will be 3.14
Set pointer value directly
double x; *p = &x;
*p = 3.14;
Make sure pointer has been set
to an
allocated memory region
Arithmetic Operations on
Pointers
When an integer is added to or subtracted
from a pointer, the new pointer value is
changed by the integer times the number
of bytes in the data variable the pointer is
pointing to. For example, if the pointer
valptr contains the address of a double
precision variable and that address is
8 bytes
234567870, then the statement:
234567870 + 2 x 8
valptr = valptr + 2;
would change valptr to 234567886
Review
nt x, *y, z, *q;
= 3;
= &x;
// y points to x
printf("%d\n", x);
// outputs 3
printf("%d\n", y);
printf("%d\n", *y);
printf("%d\n", *y+1);
printf("%d\n", *(y+1)); // this outputs the item after x in memory what is it?
= *(&x);
q = &*y;
Pointers to Arrays
int x = 1, y = 2, z[10];
int *ip;
// ip is a pointer to an int, so it can point to x, y, or an
element of z
ip = &x;
y = *ip;
*ip = 0;
=0
pj is a pointer, so it is an address
a is a pointer to the beginning of an array of n elements so a + n is the size of the array
pj++ increments the pointer to point at the next element in the array
The instruction (*pj)++ says take what pj points to and increment it
NOTE: (*pj)++; increments what pj points to, *(pj++); increments the pointer to
point at the next array element
what do each of these do?
*pj++;
++*pj;
Strings
There is no string type in C, strings are arrays of
chars
Implementing Some of
These
int strlen(char *s)
int strcmp(char *s, char *t) void strcpy(char *s, char
*t)
{
int n;
int i;
int i = 0;
for(n = 0; *s != \0; s++) for(i=0;s[i] = = t[i];i++)
while((s[i] = t[i]) !=
n++;
if(s[i] = = \0)
\0)
return n;
return 0;
i++;} strcpy(s1, s2)
}
return s[i] t[i];
strlen(s1)
}
strcmp(s1, s2) }
void strcpy(char *s, char *t)
int strcmp(char *s, char *t) {
while((*s = *t) != \0)
{
for( ; *s = = *t; s++, t++) {
Notice in the second
if(*s = = \0) return 0; s++; t++;
strcmp and second and return *s - *t;
}
third strcpy the
strcmp(s1, s2)}
strccpy(s1, s2)
}
use of pointers to iterate
void strcpy(char *s,
through the strings.
Check it
char *t)
The conciseness of the
copies t[i]
{
last strcmp and strcpy
into s[i] and
while((*s++
= *t+ s2)
strcpy(s1,
make them hard to
checks for
+) != \0);
understand
NULL
}
More On Pointer
Arithmetic
Got to the
element
int a[10] = {};
int *ip;
for(ip = &a[9]; ip >= a; ip--)
wepass to a function
Decrease
pointer by
1
Here,
the address of the
third element of an array (&a[2]) and use
pointer subtraction
get to a[0] and a[1])Functio
ip = to
&a[2];
int a[3] = {};
printf(%d, addem(&a[2]));
Recall:
a[0] = *a and
a[i] = *(a + i)
ns
addme
m
+ *(ip 2);
Multidimensional Arrays
C allows multidimensional arrays
Example: int matrix[5][10 ];
Some differences:
Because functions can be compiled separately, we must denote all but
one dimension of a multiple dimensional array in a functions parameter
list
void afunction (int amatrix[ ][10]);
Because arrays are referenced through pointers, there are multiple ways
to declare and access 2+ dimensional arrays
This will be more relevant when dealing with an array of strings (which is a 2D array)
int *a[3];
// array of 3 pointers
int x[2] = {1, 2};
int a[10][20];
int y[3] = {3, 4, 5};
int *a[10];
int z[4] = {6, 7, 8, 9};
int **a;
*a = &x[0]; // a[0] points to x[0]
*a[4] first element of 5th array element *(a+1) = &y[0];// a[1] points to y[0]
*a[9] first element of 10th array element*(a+2) = &z[0];// a[2] points to z[0]
// array a is a jagged array, it is not
**a first element of a[0]
// rectangular, or of equal dimensions
Pointers to Pointers
As indicated in the last slide, we can
have an array of arrays which is really
an array of pointers or pointers to
pointers
Arrays of Strings
Implementation
Example
y = x[i];
// x[i] is an array, x is really a pointer, so this sets y to xs starting ad
while(*y!='\0') // while the thing y points to is not the end of a string
{
printf("%c", *y);
// print whatX[0]
y pointshello
to
NUL
y++;
// and go on to the next char in x L
}
X[1]
goodby NUL
printf("\n");
// separate strings in output
with \n
e
L
X[2]
So long
NUL
L
X[3]
NUL the storage
Notice that if we had used char
x[ ][ ]thanks
= {}; then
L
space would have been 4 strings of length 23 (the
length of the
longest string) or 92 bytes instead of 42 bytes as it is above
Passing Arrays
Because you can
When an array is
passed to a function,
what is being passed
is a pointer to the
array
compile functions
separately, the compiler
must be able to know
about an array being
passed in to a function,
so you must specify all
(or most) of the
definition:
In the formal
parameter list, you
can either specify the
parameter as an array
The type and all
int array[100];
or a pointer
dimensions except for the
int array[5][10][15];
first
afunction(array);
afunction(array);
void afunction(int
void afunction(int
void afunction(int
void afunction(int
a[ ][10][15]) {}
*a[10][15]) {}
a[5][10][15]) {}
**a[15]) {}
or
or
or
etc
Some Additional
In functions, doComments
not return p; where p is a pointer
Recall local variables are deallocated when the function
ends
so whatever p is pointing to will no longer be available
but if you return the pointer, then you still are pointing at that
memory location even though you no longer know what is there
struct tag {
char lname[20]; /* last name */
char fname[20]; /* first name */
int age; /* age */
float rate; /* e.g. 12.75 per hour */
};
struct tag my_struct; /* declare the
structure my_struct */
It will copy the last
int main(void)
name and the first
name into the
{
structure
strcpy(my_struct.lname,"Jensen");
strcpy(my_struct.fname,"Ted");Passing structure takes
more memory (stack
printf("\n%s ",my_struct.fname);
space) , aThe pointer uses
printf("%s\n",my_struct.lname);
a minimum amount of
return 0;
stack space.
}
An instance of
tag in memory,
also called
instantiation
Address of my
struct
Lname
[20
Fname
[20]
age
rate
Pointers to Functions
A function that is capable of sorting
virtually any collection of data that
can be stored in an array.
The data might be an array of
strings, or integers, or floats, or even
structures. The sorting algorithm can
be the same for all.
Pointers to Functions
int arr[10] =
{ 3,6,1,2,3,8,4,1,7,2};
Pointers to Functions
void bubble(int *p, int N)
int arr[10] = { 3,6,1,2,3,8,4,1,7,2}; {
int i, j, t;
void bubble(int *p, int N);
for (i = N-1; i >= 0; i--)
int compare(int *m, int *n);
{
int main(void)
for (j = 1; j <= i; j++)
{ int i;
{ if (compare(&p[j-1],
for (i = 0; i < 10; i++)
&p[j]))
{
printf("%d ", arr[i]);
{
}
t = p[j-1];
bubble(arr,10);
p[j-1] = p[j];
for (i = 0; i < 10; i++)
p[j] = t;
{ printf("%d ", arr[i]);
}
}
m=&p[j-1],
}
return 0;
}
}
n=&p[j],
}
int compare(int *m, int *n)
SORTING AN ARRAY OF
{
INTEGERS BUT IN THIS
return (*m > *n);
COMPARISON Is DONE BY
}
A FUNCTION
Pointers to Functions
void bubble(int *p, int N)
int arr[10] =
{ int i, j, t;
{ 3,6,1,2,3,8,4,1,7,2};
void bubble(int *p, int N); for (i = N-1; i >= 0; i--)
{ for (j = 1; j <= i; j++)
int compare(void *m, void
{ if (compare((void *)&p[j-1],
*n);
(void *)&p[j]))
int main(void)
{
{
t = p[j-1];
int i;
p[j-1] = p[j];
for (i = 0; i < 10; i++)
p[j] = t;
{ printf("%d ", arr[i]); }
}
bubble(arr,10);
}
for (i = 0; i < 10; i++)
{ printf("%d
arr[i]);
}
Bubble
sort or",any
program
can }
return
0; general by identifying }
be
made
int compare(void *m, void *n)
}
the code that can be made
{
general. For example by making
int *m1, *n1;
the compare function or
m1 = (int *)m;
swapping operations general the
n1 = (int *)n;
same bubble sort can be used
return (*m1 > *n1);}
Pointers to a Function
Explain