Professional Documents
Culture Documents
Slot11 12 Pointers
Slot11 12 Pointers
Slots 11-12
Pointers
Programming Fundamentals using C
Objectives
After studying this chapter, you should be able to:
Contents
• Review the memory structure of a program
• Where can we put program’s data?
• What are pointers?
• Pointer Declarations
• Where are pointers used?
• Pointer operators
– Assign values to pointers
– Access data through pointer
– Explain pointer arithmetic
– Explain pointer comparisons
• Pointers as parameters of a function
• Dynamic Allocated Data
Pointers 3
Programming Fundamentals using C
Memory Map of a program
Data segment 4202496 MAXN=10
Code of
Code 4199199 main()
segment
Code of
4199056 average()
HEAP
2293620 a=5
2293616 b =5
2293612 c =8
STACK
segment
2293576 c=8
2293572 b =5
2293568 a =5
2293552 t = 6.0000
Pointers 4
Programming Fundamentals using C
Questions
Data segment 4202496 MAXN=10
Address of a variable is a number.
Code of
Can we assign this number to main()
4199199
another variable then access data Code segment
Code of
through the new variable? 4199056 average()
Data Dynamic
Dynamic allocated data through
Heap explicitly statements for memory
allocation
Local
Variables are defined in functions.
They will exist only when the
Stack
function is executed and they will
Segment
be removed when the function
Completed execution
Pointers 6
Programming Fundamentals using C
3- What is a Pointer?
address value
A pointer is a variable, which contains the
80000 32
address of a memory location of another
variable p2: 80000
If one variable contains the address of
another variable, the first variable is said to 400 7
point to the second variable
p1: 400
A pointer provides an indirect method of accessing
the value of a data item
Pointers can point to variables of other fundamental data types
like int, char, or double or data aggregates like arrays or
structures
Pointers 8
Programming Fundamentals using C
4- Pointer Variables
double* pD;
char *pC;
Pointers 9
Programming Fundamentals using C
6- Pointer Operators
How to Operator Example
Get address of a variable & int n= 7;
and assign it to a pointer int *pn = &n;
Access indirectly value of * *pn =100;
a data through it’s pointer
Pointer Operators…
2293620 n=7
2293616 pn= 2293620
2293612 ppn= 2293616
Pointers 12
Programming Fundamentals using C
Pointers 13
Programming Fundamentals using C
Walkthroughs: Do yourself
Pointers 15
Programming Fundamentals using C
0000 0000
0000 0000 *pp =0
0000 0001
500/ n 0000 0100 0000 0000
p 500 500 pp
Manipulate Manipulate
on 4 bytes on 1 byte only
Pointers 16
Programming Fundamentals using C
2293632
2293624
x
2293616
2293612
If access data
2293608 2293608
using pD (bytes)
can cause a harm n 2293604
on the variable n 2293600 2293600
2293596
Pointers 17
Programming Fundamentals using C
Pointer Arithmetic Operators :
Accessing the neighbor
/* file pointer_demo4.c */
#include <stdio.h>
int main()
{ int n2= 10;
Copy and paste, run the int n1= 6;
program, explain the int n0= 5;
printf("n2=%d, n1=%d, n0=%d\n", n2, n1, n0);
result. int* p = &n1;
*p=9;
p++;
*p=15;
p--;
p--;
*p=-3;
printf("n2=%d, n1=%d, n0=%d\n", n2, n1, n0);
getchar();
return 0;
}
Pointers 18
Programming Fundamentals using C
Exercises
double* p;
Suppose that a double occupies the memory block of 8 bytes and
p stores the value of 1200.
What are the result of the following expression? p+8 p-3 p++
long*p;
Suppose that a long number occupies the memory block of 4 bytes
And p stores the value of 1000.
What are the result of the following expression? p+8 p-3 p++
char*p;
Suppose that a character occupies the memory block of 1 byte
and p stores the value of 207000.
What are the result of the following expression? p+8 p-3 p++
Pointers 19
Programming Fundamentals using C
Pointers 20
Programming Fundamentals using C
Pointer Comparisons
Two pointers can be compared in a relational expression
provided both the pointers are pointing to variables of the
same type.
Consider that ptr_a and ptr_b are 2 pointer variables, which
point to data elements a and b. In this case the following
comparisons are possible:
Pointers 21
Programming Fundamentals using C
y = 7 5
x=57
t =5
a =5
b =7
Pointers 22
Programming Fundamentals using C
py: 996
px : 1000
t =5
1000 a =57
996 b =75
Pointers 23
Programming Fundamentals using C
/* file pointer_demo5.c */
Demo: Dynamic Allocated Data
#include <stdio.h>
const int MAXN =100;
int main()
{ int n; int *p1; int *p2; int *p3;
(1) Copy, past, compile
printf("Address of MAXN: %u\n", &MAXN); and run the program.
printf("Main function ia allocated at: %u\n", &main); (2) Draw the memory
printf("Address of n : %u\n", &n);
printf("Address of p1: %u\n", &p1);
map.
printf("Address of p2: %u\n", &p2); (3) Show that where is
printf("Address of p3: %u\n", &p3); data segment, code
p1 = (int*)malloc(sizeof(int));
p2 = (int*)malloc(sizeof(int));
segment, stack segment
p3 = (int*)malloc(sizeof(int)); and heap of the program.
printf("Dynamic allocation (p1) at: %u\n", p1); (4) Give comment about
printf("Dynamic allocation (p2) at: %u\n", p2);
printf("Dynamic allocation (p3) at: %u\n", p3);
the direction of dynamic
getchar(); memory allocation.
free(p1);
free(p2);
return 0;
}
Pointers 25
Programming Fundamentals using C
Pointers 27
Programming Fundamentals using C
Summary
• Review the memory structure of a program
• Where can we put program’s data?
• What are pointers?
• Pointer Declarations
• Where are pointers used?
• Pointer operators
– Assign values to pointers Q&A
– Access data through pointer
– Explain pointer arithmetic
– Explain pointer comparisons
• Pointers as parameters of a function
• Dynamic Allocated Data
Pointers 28
Programming Fundamentals using C
Pointers 29
Programming Fundamentals using C
Pointers 30
Programming Fundamentals using C
Pointers 31
Programming Fundamentals using C
Thank you!
Pointers 32