You are on page 1of 37

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Deputy Chief Engineer, PIEAS
Arrays and Pointers

Dr. Yousaf, PIEAS


Arrays and Pointers
• A variable declared as an array represents a contiguous
region of memory in which the array elements are stored,
i.e. sequence of identical objects in memory
little endian byte ordering
int x[5]; // an array of 5 4-byte ints. 0
0 1 2 3

• int x[5]; means space for five integers 1


2

• All arrays begin with an index of 0


3
4

memory layout for array x

By itself, x is the address of the first integer


• An array identifier is equivalent to a pointer that
references the first element of the array
– int x[5];
– int *ptr;
ptr = &x[0] ;
– ptr = x; // same as ptr = &x[0];
Dr. Yousaf, PIEAS
The Relationship b/w Pointers and Arrays
• Pointers have close relationship with arrays and strings
– Array name is itself a pointer. It means the name of the
array is the address of the first element of the array.
– Pointers can do array subscripting operations
• Declare an array b[ 5 ] and a pointer ptrb
• b[5] = {4, 7, 9, 10, 56};
• int *ptrb
• Array name b is also the address of this array.
• ptrb = b; Most important to note that we have not used &
with b. WHY?
• Because actually b itself is the address of first element of
the array b[5].
i.e. ptrb = &b[ 0 ]
Explicitly assigns bptr to address of first element of b

Dr. Yousaf, PIEAS


If we define an array we can use pointers to
access it

int a[7]; /* An array of 7 ints */

int *ptra; /* A pointer to an int */

ptra = a;

*ptra = 3; /* Same as a[0]= 3 */

Dr. Yousaf, PIEAS


The Relationship b/w Pointers and Arrays
#include<stdio.h>
int main()
{
int a[5] = {4, 7, 9, 10, 56};
int *ptr1, *ptr2;
ptr1 = a;
ptr2 = &a[0];
// Are ptr1 and ptr2 equal?
printf("ptr1 = %d\n", ptr1);
printf("ptr2 = %d", ptr2);
getchar(); return 0;
}
Dr. Yousaf, PIEAS
The Relationship b/w Pointers and Arrays
#include<stdio.h>
int main()
{
int a[5] = {4, 7, 9, 10, 56};
int *ptr1, *ptr2;
ptr1 = a;
ptr2 = &a[0];
// Are ptr1 and ptr2 equal? Yes.
printf("ptr1 = %d\n", ptr1);
printf("ptr2 = %d", ptr2);
getchar(); return 0;
}
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
int a[5] = {4, 7, 9, 10, 56};
int *ptra;
ptra = a;
printf("%d\n", a[0]); //4
printf("%d\n", *ptra); //4
printf("%d\n", a[1]); //7
printf("%d\n", *(ptra+1) ); //7 a[3] is the same as *(a + 3)
printf("%d\n", a[3]); //10
or *(ptra+3) , the compiler
printf("%d\n", *(ptra+3) ); //10
printf("%d\n", *(a + 3 ) ); //10
will assume you mean 3
printf("%d\n", ptra[3] ); //10 objects beyond first
getchar(); return 0; element of a.
} Where 3 is the offset, called
pointer/offset notation
Dr. Yousaf, PIEAS
Pointer Expressions and Pointer
Arithmetic
• Arithmetic operations can be performed on
pointers
– Increment/decrement pointer (++ or --)
– Add an integer to a pointer( + or += , - or -=)

– Pointers may be subtracted from each other

– Operations meaningless unless performed on an


array

Dr. Yousaf, PIEAS


#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int a[5] = {4, 7, 9, 10, 56}; int a[5] = {4, 7, 9, 10, 56};
int *ptra; int *ptra;
ptra = a;
ptra = a; printf("%d\n", ptra);
printf("%d\n", ptra);
ptra = ptra+1;
printf("%d\n", a); //It moves ptra at a[1]

printf("%d\n", ptra+1); printf("%d\n", ptra);


getchar(); return 0; getchar(); return 0;
} }

Dr. Yousaf, PIEAS


#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int a[5] = {4, 7, 9, 10, 56}; int a[5] = {4, 7, 9, 10, 56};
int *ptra; int *ptra;
ptra = a;
ptra = a; printf("%d\n", ptra);
printf("%d\n", ptra);
ptra = ptra+1;
printf("%d\n", a); //It moves ptra at a[1]

printf("%d\n", ptra+1); printf("%d\n", ptra);


getchar(); return 0; getchar(); return 0;
} }

Dr. Yousaf, PIEAS


#include<stdio.h>
int main()
{
int a[5] = {4, 7, 9, 10, 56};
int *ptra;
ptra = a;
printf("%d\n", ptra);
ptra= ptra+1;
printf("%d\n", ptra);
ptra = a;
printf("%d\n", ptra);
ptra += 2;
printf("%d\n", ptra);
getchar(); return 0;
} Dr. Yousaf, PIEAS
Pointer Expressions and Pointer Arithmetic
5 element int array on machine with 4 byte
ints
– int a[5];
– int *ptra;
– ptra = a;
– ptra points to first element a[ 0 ]
• at location 3000 (ptra = 3000)
– ptra += 2; sets ptra to 3008
• ptra points to a[ 2 ] (incremented by 2), but the
machine has 4 bytes ints, so it points to address
3008 location
3000 3004 3008 3012 3016

a[0] a[1] a[2] a[3] a[4]

Dr. Yousaf, PIEAS


pointer variable ptra
Pointers Subtraction
#include<stdio.h>
int main()
{
int a[5] = {4, 7, 9, 10, 56};
int *ptr1, *ptr2;
ptr1 = a; // actually ptr1 = &a[0];

printf("%d\n\n", ptr1);

ptr2= &a[2]; // same as ptr2 = ptr1+2;


printf("%d\n\n", ptr2);

printf("%d\n", ptr2 - ptr1);


getchar(); return 0;
}
Dr. Yousaf, PIEAS
Pointers Subtraction
#include<stdio.h>
int main()
{
int a[5] = {4, 7, 9, 10, 56};
int *ptr1, *ptr2;
ptr1 = a; // actually vptr1 = v[0];

printf("%d\n\n", ptr1); ptr2 – ptr1 would


produce 2, i.e if two
ptr2= &a[2]; // same as ptr2 = ptr1+2; pointers are pointing to
printf("%d\n\n", ptr2); the same variable, the
difference between their
printf("%d\n", ptr2 - ptr1); values will be equal to
getchar(); return 0; the number of elements
} between them.
Dr. Yousaf, PIEAS
Pointer Expressions and Pointer Arithmetic

• Pointer comparison ( <, == , > )

– See which pointer points to the higher numbered

array element

– e.g if (ptr2 > ptr 1)

rest of the code here

Dr. Yousaf, PIEAS


// Printing the elements of an array without using
pointer
#include<stdio.h>
int main()
{
int i, x[5] ={10, 20, 30, 40, 50};

for (i = 0; i<5;i++)
printf("\n x = %d", x[i]);

getchar();
return 0;
}

Dr. Yousaf, PIEAS


// Printing the elements of an array using pointer
#include<stdio.h>
int main()
{
int i, x[5] ={10, 20, 30, 40, 50}, *ptrx;
ptrx = x; // important to observe that there is no & sign
// with x.
for (i = 0; i<5;i++)
{
printf("\n x = %d",*ptrx);
ptrx++; // see the increment in ptrx and its use
}
getchar(); return 0;
}

Dr. Yousaf, PIEAS


/* C program to determine and print the largest number in an
array (no use of pointers) */

#include<stdio.h>
int main()
{
int i, max, x[5] ={23, 78, 99, 2, 37};
max = x[0];
for (i = 0; i<5;i++)
{
if (x[i] > max)
max = x[i];
}
printf("\n max = %d",max);
getchar(); return 0;
}
Dr. Yousaf, PIEAS
/* C program to determine the largest number in an
array, and then print it using a pointer */
#include<stdio.h>
int main()
{
int i, max, x[5] ={23, 78, 99, 2, 37}, *ptrmax;
max = x[0];
for (i = 0; i<5;i++)
{
if (x[i] > max)
max = x[i];
}
ptrmax = &max;
printf("\n max = %d", *ptrmax);
getchar(); return 0;
} Dr. Yousaf, PIEAS
Solve a problem

Try to write a program that calculates average of

three numbers, you have done this program before,

here try to do it using pointers, i.e. use pointers only.

Dr. Yousaf, PIEAS


Solve a problem

Try to write a program that swaps the values of

two integer variables, you have to do it using

pointers i.e. any modifications in the values of

integer variables should be done using pointers.

Dr. Yousaf, PIEAS


Pass By Reference

Dr. Yousaf, PIEAS


Pass by Value/ Pass by Reference
• Normally when we send a variable to a
function we make a COPY of the variable.
• This is called pass by value. A value is passed
and this copy of the variable arrives at the
function. The original doesn’t change.
• The value of a variable can also be pass by
reference
• In this case, any change in the value of this
variable made within a function changes the
original value.

Dr. Yousaf, PIEAS


What's the point of pointers?
• When we use & to pass a pointer to a variable

into a function we CAN change the value of the

variable within the function.

• This is called pass by reference.

Dr. Yousaf, PIEAS


Calling Functions by Reference
Call by reference with pointer arguments
– Pass address of argument using & operator
– Allows you to change actual location in
memory

Dr. Yousaf, PIEAS


// Passing arguments by reference void myfunc(int *a, int *b,
#include <stdio.h> int *c)
void myfunc(int *a, int *b, int *c); {
int main() *a = 5;
{ *b = 7;
int x = 2, y = 4, z = 6; *c = 9;
printf("%d, %d, %d\n", printf("%d, %d, %d\n", *a,
x, y, z); // 2, 4, 6 *b, *c); // 5, 7, 9
}
my_func(&x, &y, &z);

printf("%d, %d, %d\n",


x, y, z); //5, 7, 9

getchar(); return(0);
}
Dr. Yousaf, PIEAS
Calling Functions by Reference
// Passing arguments by reference void twice( int *number )
#include <stdio.h> {
void twice(int *number ); *number = 2 * ( *number );
int main() }
{
int number;
printf(“Enter an integer number”);
scanf(“%d”, &number);
twice(&number) ;
printf(“%d”, number);
getchar(); getchar();
return 0;
} Dr. Yousaf, PIEAS
Calling Functions by Reference
#include<stdio.h>
void square_num (int *);

int main()
{
int p= 5;
square_num (&p);
printf("P is now %d\n",p); //P is now 25
return 0;
}

void square_num (int *num)


{
(*num)= (*num) * (*num);
}
Dr. Yousaf, PIEAS
Passing Arrays to Functions

Dr. Yousaf, PIEAS


Calling Functions by Reference
Arrays are not passed with & because
the array name is already a pointer
(we have already studied)

Dr. Yousaf, PIEAS


Pass by Reference
/*Pass by reference (Page 365) void add_num_inArray(int x[])
Pass by reference Example Page 366 {
If names of variables in main and
the calling function are similar */ int i;
#include<stdio.h>
void add_num_inArray(int x[]); for (i = 0; i<5; i++)
int main() {
{
int i, x[5] ={23, 78, 90, 2, 37}; x[i] = x[i] + 5;
}
add_num_inArray(x); }
for (i = 0; i<5;i++)
{
printf("\n %d",x[i]);
}
getchar(); return 0;
}
Dr. Yousaf, PIEAS
Pass by Reference
/* Pass by reference, If names of void add_num_inArray(int k[])
variables in main and the calling {
function are different */ int i;
#include<stdio.h>
void add_num_inArray(int k[]); for (i = 0; i<5; i++)
int main() {
{ k[i] = k[i] + 5;
int i, x[5] ={23, 78, 99, 2, 37};
add_num_inArray(x); }
for (i = 0; i<5;i++) }
{
printf("\n %d",x[i]);
}
getchar(); return 0;
}
Dr. Yousaf, PIEAS
/* Define an array in main. Pass int find_max(int k[])
that array to a function that should
determine the maximum number.
{
// Return the maximum number
from that function to main and then int i, max = k[0];
print it in main. for (i = 0; i<5;i++)
No use of pointers */ {
#include<stdio.h> if (k[i] > max)
int find_max(int k[]);
max = k[i];
int main()
{ }
int max, x[5] ={23, 78, 99, 2, 37}; return max;
max = find_max(x); }
printf("\n max = %d",max);
getchar();
return 0; }
Dr. Yousaf, PIEAS
/* Define an array in main. Pass int* find_max(int k[])
that array to a function that should
determine the maximum number {
// Return the address of that max int *ptrmax;
number (using pointer) from the int i;
function to main and then print it in int max = k[0];
main. */ for (i = 0; i<5;i++)
#include<stdio.h> {
int* find_max(int k[]); if (k[i] > max)
int main() max = k[i];
{ int *ptrmax, x[5] ={23, 78, 99, 2, }
37}; ptrmax = &max;
ptrmax = find_max(x); return ptrmax;
printf("\n max = %d",*ptrmax); }
getchar();
return 0; }
Dr. Yousaf, PIEAS
/* Define an array in main. Pass that array to a function that
should add an integer to each element of that array. Return the
address of the new array (using pointer) from the function to
main and then print it in main. */ int*
#include<stdio.h> add_num_inArray(int
int* add_num_inArray(int k[]); k[])
int main() {
{ int i;
int i, *ptr_added_array, x[5] ={23, 78, 99, int new_array[5];
2, 37};
ptr_added_array = add_num_inArray(x); for (i = 0; i<5; i++)
for (i = 0; i<5;i++) {
{ new_array[i] = k[i] + 5;
printf("\n %d",*ptr_added_array); }
ptr_added_array++; return new_array;
} getchar(); return 0; } }
Dr. Yousaf, PIEAS
/* Define an array in main. Pass that array to a function that should
add an integer number to each of the element of that array.
Also correspond the added_array of main function and new_array
of main function in the calling statement of the main function.
In fact, here array names are being used as pointers. */
#include<stdio.h> void add_num_inArray(int
void add_num_inArray(int k[], int k[], int new_array[])
new_array[]);
int main() {
{ int i;
int i, x[5] ={23, 78, 99, 2, 37}; for (i = 0; i<5; i++)
int added_array[5]; {
add_num_inArray(x, added_array);
new_array[i] = k[i] + 5;
for (i = 0; i<5;i++) }
{ }
printf("\n %d", added_array[i]);
}
getchar();
return 0; }
Dr. Yousaf, PIEAS

You might also like