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

Lecture 2 - Pointers

asd

Uploaded by

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

Lecture 2 - Pointers

asd

Uploaded by

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

SE 2005

DATA
STRUCTURES

Pointers
The real power of C
lies in pointers!!
BEFORE START
Memory Organization in Computer Architecture

https://www.studytonight.com/computer-architecture/memory-organization
MEMORY ALLOCATION
• Programs manage their memory by partitioning or dividing it into
different units that perform specific tasks.

• Two of those units are the stack and the heap, which manage the
program's unused memory and allocate it for different kinds of data or
variables.

• When the program no longer needs the memory, it may deallocate it.
– Deallocated memory is returned to its source, either the stack or the heap,
and is made available for reuse.
memory allocation technique
segmented memory.
Controlled by
developers

Controlled by compiler

https://icarus.cs.weber.edu/~dab/cs1410/textbook/4.Pointers/memory.html
BEFORE START
• Let's first remember how memory is organized inside a computer.

Memory in a computer is made


up of bytes arranged in a
sequential manner.

Each byte has a number


associated with it, which is called
the address of the byte.

• The address of byte starts from 0 to one less than the size of memory.
– For example, say in a 64MB of RAM, there are 64 * 2^20 = 67108864 bytes.
Therefore the address of these bytes will start from 0 to 67108863
Note: The size of integer may vary from one system to another. On 32 bit systems, integer variable
is allocated 4 bytes while on 16 bit systems it is allocated 2 bytes
BEFORE START
• Every variable in C has a name and a value associated with it.
• When a variable is declared, a specific block of memory within the
computer is allocated to hold the value of that variable.
– The size of the allocated block depends on the data type.

When this statement executes,


int x = 10; the compiler allocates 4 bytes of
memory to hold the value 10.
BEFORE START
Think about what happens when we write

int marks = 42;

A block of memory (named as marks here) is


reserved by the compiler to hold an int value (42
here).

Now, to remember the block, it is assigned with an


address or a location number(say, 5004).
SHOULD WE REMEMBER THE
ADDRESSES OF ALL THE VARIABLES?
• No! We can access this address using & (ampersand) or address of operator
– Which can't be used with constants or expression, it can only be used with
a variable

printf("The address of marks = %p.", &marks);


/* prints "The address of marks = 5004. */

Hint: you must always use %p or %u instead of %d


CONVERSION SPECIFICATION FOR
MEMORY ADDRESSES
#include <stdio.h>

int main () {
int* val = 0xffffffff;
d = -1
printf("d = %d\n", val); u = 4294967295
printf("u = %u\n", val); p = FFFFFFFF
printf("p = %p\n", val);

return 0;
}
We have been using address
operator(&) in the function
scanf() without knowing why?

The address of a variable is provided to


scanf(), so that it knows where to write data.
INTRODUCTION TO POINTERS
• Pointers are variables whose values are
memory addresses.
– Normally, a variable directly contains a
specific value.
– A pointer contains an address of a
another variable that contains a
specific value

• Pointer enables programs to simulate pass-by-


reference.
INTRODUCTION TO POINTERS

A pointer, countPtr, is
the address of some
other variable, count.
WHY WE USE POINTERS IN C
• Pointers provide a powerful and flexible method for manipulating data in
programs.
– We can easily access the memory location of any variable directly and
manipulate it according to our convenience.
• It facilitates the concept of dynamic memory allocation.
• We prefer pointers in certain situations to improve the efficiency of
certain procedures
• Pointers are used to create complex data structures,
– such as trees, linked lists, linked stacks, linked queues, and graphs
POINTER OPERATORS
• The Address (&) Operator is a unary operator that returns the address of
its operand.

• The Indirection (*) Operator is the unary operator, commonly referred to


as the indirection operator or dereferencing operator, returns the value
of the object to which its operand (i.e., a pointer) points.
POINTERS DEFINITIONS AND
INITIALIZATION
Just like any other variables you need to first declare a pointer variable before you can
use it. The asterisk (*) notation used to declare pointer variables.

Syntax: <data type> *<pointer variable>


When we declare a Pointer, we
have several option when
Example: typing the asterisk ( * ). White
int *countPtr;
space surrounding it does not
“countPtr is a pointer to matter.
countPtr = &x;
int” or “countPtr points
to an object of type int.”

In C, an “object” is a region of memory that can hold a value.


DEREFERENCE
İnt a, *p; //1 int, 1 pointer to int

p = &a; //Assign p the address of a

*p = 5; //Same as a = 5;

IMPORTANT – The two different stars

int* P → declares an integer pointer p


*p = 5 → uses the * operator to dereference p
ADDRESSES, &, AND POINTERS
EXAMPLE
int a = 5;
int* p = &a;

printf("value of a = %d \n",a); // 5
printf("address of a = %p \n", &a); // 204
printf("value of p = %p \n", p); // 204
printf("dereferencing p = %d \n",*p); // 5
int marks = 100, *p1, *p2;

p1 = &marks;

p2 = p1;
char ch = 'a';
int i = 10;
double d = 100.21;

char *cp = &ch;


int *ip = &i;
double *ip = &d;
The printf conversion specifier %p
outputs the memory location as a
hexadecimal integer on most
platforms

The address of a is 0028FEC0


The value of aPtr is 0028FEC0
The value of a is 7
The value of *aPtr is 7
Showing that * and & are complements of each other
&*aPtr = 0028FEC0
*&aPtr = 0028FEC0
SMALLISH QUESTION -1 int a = 20

204 int a = 10
int a=10;
int b= 20;
int* p= &a; 148 int * p = 204
int* t= &b;
110 int * t = 64

After performing a=b the content of the memory at


64 İnt b = 20
A: 204 will change
B: 148 will change
C: both A and B
D: none of the above
Note: If we perform *p = *t, again the same
location will change
SMALLISH QUESTION -2 int a = 12

204 int a = 10
int a=10;
int b= 20;
int* p= &a; 148 int * p = 204
int* t= &b;
110 int * t = 64

After performing *p = 12 the content of the memory at


64 İnt b = 20
A: 204 will change
B: 148 will change
C: both A and B
D: none of the above
SMALLISH QUESTION -3
204 int a = 10
int a=10;
int b= 20;
int* p= &a; 148 int * p = 204
int * p = 64
int* t= &b;
110 int * t = 64

After performing p = &b


64 İnt b = 20
A: The value of p changes
B: The value of *p changes
C: both A and B
D: none of the above
SMALLISH QUESTION -4
*p=b p=&b

int a=10; 204 int a = 10 204 int a = 10


int a = 20
int b= 20;
int* p= &a;
148 int * p = 204 148 int * p = 204
int* t= &b; int * p = 64
110 int * t = 64 110 int * t = 64
*p=b and p=&b
A: Are literally the same; 64 İnt b = 20 64 İnt b = 20

B: both change the value of *p to 20


C: both end up making p be equal to t
D: Both B and C
NULL POINTER
• The word "NULL" is a constant in C language and its value is 0

• A null pointer is a pointer which points nothing (any memory location).

Syntax int *pointer_var = NULL;

OR
int *pointer_var = 0
USAGE OF NULL POINTER
• To initialize a pointer variable when that pointer variable isn’t assigned any
valid memory address yet.

• To pass a null pointer to a function argument when we don’t want to pass


any valid memory address.

• To check for null pointer before accessing any pointer variable. So that, we
can perform error handling in pointer related code e.g. dereference
pointer variable only if it’s not NULL.
PASSING ARGUMENTS TO FUNCTIONS
BY REFERENCE
• There are two ways to pass arguments to a function—pass-by-value and pass-by-
reference. However, all arguments in C are passed by value.

• In C, you use pointers and the indirection operator to accomplish pass-by-reference.


– When calling a function with arguments that should be modified, the addresses of the
arguments are passed.
– Arrays are not passed using operator & because C automatically passes the starting location
in memory of the array (the name of an array is equivalent to &arrayName[0]).
– When the address of a variable is passed to a function, the indirection operator (*) may be
used in the function to modify the value at that location in the caller’s memory.
A function receiving an address as an
argument must define a pointer
parameter to receive the address
USING THE CONST QUALIFIER WITH
POINTERS
• The const qualifier enables you to inform the compiler that the value of a particular variable
should not be modified.

• Six possibilities exist for using (or not using) const with function parameters
– two with pass-by-value parameter passing
– four with pass-by-reference parameter passing

• There are four ways to pass a pointer to a function:


– a non-constant pointer to non-constant data.
– a constant pointer to nonconstant data.
– a non-constant pointer to constant data.
– a constant pointer to constant data.
NON-CONSTANT POINTER TO NON-CONSTANT DATA
In this case, the data can
be modified through the
dereferenced pointer, and
the pointer
can be modified to point
to other data items
NON-CONSTANT POINTER TO CONSTANT DATA
In this case, the pointer
can be modified to point
to any data item of the
appropriate type, but the
data to which it points
cannot be modified

Tip: If memory is low and


execution efficiency is a
concern, use pointers. If
memory is in abundance and
efficiency is not a major
concern, pass data by value to
enforce the principle of least
privilege. Remember that some
systems do not enforce const
well, so pass by- value is still
the best way to prevent data
from being modified
CONSTANT POINTER TO NON-CONSTANT DATA
In this case, the pointer
points to the same
memory location, and
the data at that location
Pointers that are declared const must
can be modified through
be initialized when they’re defined
the pointer

This is the default for an


array name. An array
name is a constant
pointer to the beginning
of the array. All data in
the array can be accessed
and changed by using the
array name and array
indexing.
CONSTANT POINTER TO CONSTANT DATA
In this case, the pointer
always points to the same
memory location, and the
data at that memory
location cannot
be modified

This is how an array


should be passed to a
function that only looks at
the array using array index
notation and does not
modify the array.
SIZEOF OPERATOR
• C provides the special unary
operator sizeof to determine
the size in bytes of an array
(or any other data type).
– This operator is applied at
compilation time, unless its
operand is a variable-length
array
POINTER EXPRESSIONS AND POINTER
ARITHMETIC
• Pointers are valid operands in arithmetic expressions, assignment expressions
and comparison expressions. However, not all the operators normally used in
these expressions are valid in conjunction with pointer variables.
UNARY OPERATORS
// Program showing pointer expressions // Value before decrement
// during Unary Operations printf("\n\nDecrement:\n");
#include <stdio.h> printf("Before decrement a = %d\n", *ptr_a);
int main()
{ // unary decrement operation
int a = 34; (*ptr_a)--;

int* ptr_a; // Declaring pointer variable // Value after decrement


printf("After decrement a=%d", *ptr_a);
ptr_a = &a; // Initializing pointer variable
return 0;
// Value of a before increment }
printf("Increment:\n");
printf( "Before increment a = %d\n", *ptr_a);
Increment:
// Unary increment operation Before increment a = 34
(*ptr_a)++; After increment a = 35

// Value of a after increment Decrement:


printf( "After increment a = %d", *ptr_a); Before decrement a = 35
After decrement a=34
#include <stdio.h>
Program showing int main()
pointer {
int a = 20, b = 10;
expressions int add, sub, div, mul, mod; // Variables for storing arithmetic operations solution
during Arithmetic
Operations int *ptr_a, *ptr_b; // Pointer variables for variables a and b

ptr_a = &a;
ptr_b = &b; Note:While performing division,
make sure you put a blank space
add = *ptr_a + *ptr_b;
between ‘/’ and ‘*’ of the pointer as
sub = *ptr_a - *ptr_b;
mul = *ptr_a * *ptr_b; together it would make a multi-line
div = *ptr_a / *ptr_b; comment(‘/*’).
mod = *ptr_a % *ptr_b;

printf("Addition = %d\n", add);


printf("Subtraction = %d\n", sub); Addition = 30
printf("Multiplication = %d\n", mul); Subtraction = 10
printf("Division = %d\n", div); Multiplication = 200
printf("Modulo = %d\n", mod); Division = 2
return 0; Modulo = 0
}
AIMING A POINTER AT AN ARRAY
Assume that array int v[5] has been
defined and its first element is at
location 3000 in memory

vPtr = v;
vPtr = &v[0];

Assume pointer vPtr has been


initialized to point to v[0]—i.e., the
value of vPtr is 3000
ADDING AN INTEGER TO A POINTER
• When an integer is added to or subtracted from a pointer, the pointer is not incremented or
decremented simply by that integer, but the amount by which you do that is multiplied by the
size of the type the pointer points to.
– The number of bytes depends on the object’s data type.

• For example, the statement


vPtr += 2;

would produce 3008 (3000 + 2 * 4), assuming an integer is stored in 4 bytes of memory

• In the array v, vPtr would now point to v[2]

• If vPtr had been incremented to 3016, which points to v[4]


RELATIONSHIP BETWEEN POINTERS
AND ARRAYS
• An array name can be thought of as a constant pointer.

int b[5]; Because the array name b (without an index) is a pointer


int *bPtr; to the array’s first element, we can set bPtr equal to the
bPtr = b; address of the array b’s first element with the statement

bPtr = &b[0]; This statement is equivalent to taking the address of


array b’s first element
POINTER/OFFSET NOTATION
• Array element b[3] can alternatively be referenced with the pointer expression

Offset
The parentheses are Hint: Without the parentheses, the
*(bPtr + 3) necessary because the above expression would add 3 to
precedence of * is higher the value of the expression *bPtr
than the precedence of +. (i.e., 3 would be added to b[0],
assuming bPtr points to the
OR beginning of the array)

*(b + 3)
Array b printed with:
Array index notation
b[0] = 10
b[1] = 20
b[2] = 30
b[3] = 40

Pointer/offset notation where


the pointer is the array name
*(b + 0) = 10
*(b + 1) = 20
*(b + 2) = 30
*(b + 3) = 40

Pointer index notation


bPtr[0] = 10
bPtr[1] = 20
bPtr[2] = 30
bPtr[3] = 40

Pointer/offset notation
*(bPtr + 0) = 10
*(bPtr + 1) = 20
*(bPtr + 2) = 30
*(bPtr + 3) = 40
ARRAYS OF POINTERS
• Arrays may contain pointers. A common use of an array of pointers is to
form an array of strings, referred to simply as a string array.

Although it appears these strings are being


placed in the suit array, only pointers are
indicates that the strings pointed to by actually stored in the array
each element will not be modified.

const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};

indicates that each element indicates an array of 4 elements


of array suit is of type
“pointer to char
Each pointer points to the first character of its corresponding
string. Thus, even though the suit array is fixed in size, it
provides access to character strings of any length.

This flexibility is one example of C’s powerful data-structuring


capabilities.
POINTER TO POINTER
• A pointer to a pointer is a form of
multiple indirection, or a chain of
pointers

• When a target value is indirectly pointed


to by a pointer to a pointer, accessing
that value requires that the asterisk
operator be applied twice, as is shown
below

int **ptr; // declaring double pointers


#include <stdio.h>

int main () {

int var;
int *ptr;
int **pptr;

var = 3000;

/* take the address of var */


ptr = &var;

/* take the address of ptr using address of operator & */


pptr = &ptr;

/* take the value using pptr */


printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
Value of var = 3000
return 0; Value available at *ptr = 3000
} Value available at **pptr = 3000
https://www.youtube.com/watch?v=IkyWCOUY8V4

You might also like