Lecture 2 - Pointers
Lecture 2 - Pointers
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.
• 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.
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?
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.
*p = 5; //Same as a = 5;
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;
204 int a = 10
int a=10;
int b= 20;
int* p= &a; 148 int * p = 204
int* t= &b;
110 int * t = 64
204 int a = 10
int a=10;
int b= 20;
int* p= &a; 148 int * p = 204
int* t= &b;
110 int * t = 64
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 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.
• 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
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;
vPtr = v;
vPtr = &v[0];
would produce 3008 (3000 + 2 * 4), assuming an integer is stored in 4 bytes of memory
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
*(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.
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;