0% found this document useful (0 votes)
2 views

EE171_Lecture_9_Programming_in_C

Uploaded by

Tony Anhydrous
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

EE171_Lecture_9_Programming_in_C

Uploaded by

Tony Anhydrous
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

EE171

Introduction to Computers &


Programming for Engineers

Lecture 9:
Computer Programing with C
1
Pointers

2
3
Pointers
4

 Pointers are powerful features of C and C++ programming


 Before we learn pointers, let's learn about addresses in C
programming
 If you have a variable named var in your program, &var will give
you its address in the memory
 We have used address-of operator numerous times while
using the scanf() function:
#include <stdio.h>
int main(){
int var = 5;
printf("Value of var is: %d \n", var);
printf("Address of var is: %x \n", &var);
}
 The value entered by the user is stored in the address of var
variable.
 The actual location of a variable in the memory is system
dependent
5
Pointers
6

 A pointer is a variable whose value is the address of another


variable.
 Pointers are special variables that are used to store addresses
rather than values.
 A pointer “points” to the memory address of another variable.
 The actual value of all pointers, whether integer, float, character,
or otherwise, is the same, a long hexadecimal number that
represents a memory address.
How Pointers Work
7
Benefits of Using Pointers
8

 Pointers make it possible to return more than one value from


the function.
 Pointers enables us to access a variable that is defined outside a
function
 Pointers allow passing of arrays and strings to functions more
efficiently.
 Pointers increase the processing speed.
 Pointes save memory utilization.
How to Declare a Pointer
9

 Like any identifier, a pointer must be declared before using it to


store memory address of another variable.
 The general form of a pointer declaration is:

 datatype is the data type of the variable to which the pointer


variable is pointing. void type pointer works with all data types,
but is not often used.
 pointer-name is the name of the pointer variable.
How to Declare a Pointer…
10

 The asterisk * indicates that a variable is a pointer.


 The * operator allows us to get the actual value stored at the
address held by the pointer
How to Initialize a Pointer…
11

 Pointer Initialization is the process of assigning address of a


variable to a pointer variable.
 A pointer variable can only contain address of a variable of the
same data type.
 In C language address operator & is used to determine the
address of a variable.
How to Initialize a Pointer…
12

#include<stdio.h>
int main()
{
int a = 20;
int *ptr; //pointer declaration
ptr = &a; //pointer initialization
}
How to Initialize a Pointer…
13

 A pointer variable must always point to variables of the same


type.
How to Initialize a Pointer…
14

 If you are not sure about which variable's address to assign to a


pointer variable during declaration, it is recommended to assign
a NULL value.
#include<stdio.h>
int main()
{
int *ptr = NULL; //pointer declaration
}
How to Use a Pointer…
15

#include <stdio.h>
int main () {
int age = 20;
int *ptr;
ptr = &age;
printf("Address of age variable: %X\n", &age );
printf("Address stored in ip variable: %X\n", ptr );
printf("The actual value inside *ip: %d\n", *ptr );
}
Pointer and Arrays
16

 When an array in C language is declared, compiler allocates


contiguous amount of memory to contain all the elements of
the array.
 The name of the array points to the address of the first element
of the array.
 Suppose we declare an array:
int arr[5] = { 10, 20, 30, 40, 50 };
Pointer and Arrays…
17
Pointer and Arrays…
18

 Variable arr will give the base address of the array.


 Hence arr contains the address of arr[0] (that is 1000)
 In short arr has two purposes.
 It is the name of the array and it acts as a pointer pointing
towards the first element in the array
 arr is equal to &arr[0] by default
Pointer and Arrays…
19

 Using a pointer, we can move from one array element to


another.

int arr[5] = { 10, 20, 30, 40, 50 };


int *p;
p = arr;
// is equivalent to
p = &arr[0];
Accessing array values…1
20

#include <stdio.h>
int main () {
int arr[3] = { 10, 20, 30 };
printf("Value of first element is : %d \n", arr[0] );
printf("Value of second element is : %d \n", arr[1] );
printf("Value of third element is : %d \n", arr[2] );
}
Accessing array addresses…2
21

#include <stdio.h>
int main () {
int arr[3] = { 10, 20, 30 };
printf("Address of first element is : %d \n", &arr[0] );
printf("Address of second element is : %d \n", &arr[1] );
printf("Address of third element is : %d \n", &arr[2] );
}
Accessing array addresses…3
22

#include <stdio.h>
int main () {
int arr[3] = { 10, 20, 30 };
printf("Address of first element is : %d \n", arr );
printf("Address of second element is : %d \n", arr + 1 );
printf("Address of third element is : %d \n", arr + 2 );
}
Accessing array addresses…4
23

#include <stdio.h>
int main () {
int arr[3] = { 10, 20, 30 };
int *p = arr;
printf("Address of first element is : %d \n", p);
printf("Address of second element is : %d \n", p + 1 );
printf("Address of third element is : %d \n", p + 2 );
}
Accessing array values…5
24

#include <stdio.h>
int main () {
int arr[3] = { 10, 20, 30 };
int *p = arr;
printf("Value of first element is : %d \n", *p);
printf("Value of second element is : %d \n", *(p + 1) );
printf("Value of third element is : %d \n", *(p + 2) );
}
Exercise
25

Write a program to display all elements of the


following array, by using a for-loop

int number[5] = { 10, 20, 30, 40, 50 };


Approach 1…
26

#include <stdio.h>
int main () {
int arr[5] = { 10, 20, 30, 40, 50 };
int i;
for ( i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
}
Approach 2…
27

#include <stdio.h>
int main () {
int arr[5] = { 10, 20, 30, 40, 50 };
int i, *p = arr;
for ( i = 0; i < 5; i++)
{
printf("%d ", *(p+i));
}
}
Approach 3…
28

#include <stdio.h>
int main () {
int arr[5] = { 10, 20, 30, 40, 50 };
int i, *p = arr;
for ( i = 0; i < 5; i++)
{
printf("%d ", *p);
p++;
}
}
Pointer and Functions
29

 Recall that: A function is a piece of code that takes information


in, does some computation, and (usually) returns a new piece of
information based on the parameter information.
 We can call a function in two different ways, based on how we
specify the arguments:

 Call by Value
 Call by Reference
Call by Value
30

 Calling a function by value means, we pass copies the values


of the arguments to the function.
 Hence, the original values are unchanged only the parameters
inside the function changes.
 Changes made to the passed variable do not affect the original
variable value.
 This is the default calling mode in C.
#include <stdio.h>
void minusfive( int a) {
a = a – 5;
printf("The value of a in minusfive function is %d \n", a);
}
int main () {
int a = 20;
minusfive(a);
printf("The value of a in main function is %d \n", a);
31 }
Call by Reference
32

 Call by reference means we pass the address(reference) of


a variable as argument to the function.
 When we pass the address of a variable as argument, then the
function will have access to our variable, as it now knows where
it is stored and hence can easily update its value.
 Changes made to the passed value will affect the original data.
#include <stdio.h>
void minusfive( int *p) {
*p = *p – 5;
printf("The value of a in minusfive function is %d \n", *p);
}
int main () {
int a = 20;
minusfive(&a);
printf("The value of a in main function is %d \n", a);
33 }
Points to Remember when using Pointers
34

 A pointer variable stores the address of another


variable.
 While declaring a pointer variable, asterisk (*)

indicates that the variable is a pointer.


 The address of any variable is given by preceding the

variable name with Ampersand (&).


 To access the value of a certain address stored by a
pointer variable, * is used. The * can be read as 'value at'.
35

You might also like