(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
Pointers Notes.pptx
1. Outline
1
• Derived Types
• Introduction to Pointers
• Pointer Declaration and Initialization
• Pointer Types
• Pointer pointing to one or more variables
• Pointer Usage and Arithmetic
• Pointers in 1D Array
3. Introduction to Pointers
• Pointers contain address of a variable that has a specific value
(indirect reference)
• Indirection – referencing a pointer value
• Contain memory addresses as their values
• Normal variables contain a specific value (direct reference)
Normal_variable
9
Normal_variable
9
Pointer_variable
3
4. Pointer Declaration and Initialization
4
• * used with pointer variables
int *myPtr;
• Declares a pointer to an int (pointer of type int *)
• Multiple pointers require using a * before each variable
declaration
int *myPtr1, *myPtr2;
• Can declare pointers to any data type
• Initialize pointers to 0, NULL, or an address
• 0 or NULL – points to nothing (NULL preferred)
7. Store address of variable to another
variable
7
int n;
int *q;
q = &n;
// Declares pointer to int
// address of n is stored in q
char a;
char *p; // Declares pointer to char
p = &a; // address of a is stored in p
float x;
float *r; // Declares pointer to float
r = &x; // address of x is stored in r
10. Demonstrate Pointer Usages
10
#include <stdio.h>
int main (void)
{
int a;
int *p;
a = 15;
p= &a;
printf(“%d %un”, a , &a);
printf(“%u %d %d”, p, *p,
a);
return 0;
}
Output:
15 13579
13579 15 15
11. Program
#include <stdio.h>
int main (void)
{
int a, b, c;
int *p, *q, *r;
a = 6;
b = 2;
p = &b;
q = p;
r = &c;
p = &a;
*q = 8;
*r = *p;
*r = a + *q + *&c;
printf(“%d %d %dn”, a, b, c);
printf(“%d %d %d”, *p, *q, *r);
return 0;
}
Output:
6 8 20
6 8 20
11
12. Arrays
When an array is declared, compiler allocates sufficient amount of memory to contain
all the elements of the array.
Base address which gives location of the first element is also allocated by the
compiler. Suppose, we declare an array arr.
int arr[5] = {1, 2, 3, 4, 5};
Assuming that the base address of arr is 1000 and each integer requires 2 bytes.
Then 5 elements are stored as follows.
arr[0] arr[1] arr[2] arr[3] arr[4]
1000 1002 1004 1006 1008
Here, arr will give base address, which is a constant pointer pointing to the element
arr[0]. Therefore, arr contains the address of arr[0] i.e. 1000
arr is equal to &arr[0] //by default
12
13. Pointer
13
We can declare a pointer of type int to point to the array arr.
int *p;
p = arr;
or p = &arr[0]; //both statements are equivalent
Now, we can access every element of array arr using p++ to move from one
element to another.
Note: We cannot decrement a pointer once incremented p- - won’t work.
14. Pointer to Array
14
We can use a pointer to point to an Array. Then use that pointer to access that array.
int i;
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
for (i=0; i<5; i++)
{
printf(“%d”, *p);
p++;
}
Note: pointer *p prints all the values that are stored in the array arr one by
one.
15. Replacing printf(“%d”, *p); statement in the previous program, with below
statements. Let’s observe the result.
15
printf(“%d”, arr[i]); prints array by incrementing index
printf(“%d”, i[arr]); prints array by incrementing index
printf(“%d”, arr + i); prints address of all the array elements
printf(“%d”, *(arr + i)); prints array by incrementing index
printf(“%d”, *arr); prints value of arr[0] only
arr++; compile time error, can’t change base address of array
21. Pointer to an array
• We can also declare a pointer that can point to the whole
array instead of only one element of array.
• Useful in multidimensional array.
• Syntax: data_type (*var_name)[size_of_array];
Declaration: int (*ptr)[10]; // ptr is a pointer that
can point to an array of 10 integers.
p = 9584, ptr = 9584, arr= 9584
*p = 3, *ptr = 9584
p = 9588, ptr = 9604
*p = 5, *ptr = 9604
21
23. •p: is pointer to 0th element of the array arr, while ptr is a pointer that points to the
whole array arr.
•The base type of p is int while base type of ptr is ‘an array of 5 integers’.
•We know that the pointer arithmetic is performed relative to the base size, so if we
write ptr++, then the pointer ptr will be shifted forward by 20 bytes.
•On dereferencing a pointer expression we get a value pointed to by that pointer expressio
• Pointer to an array points to an array, so on dereferencing it, we should get the array, and
the name of array denotes the base address.
•So whenever a pointer to an array is dereferenced, we get the base address of the array to
which it points.
23
24. Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) ?
a[3] float float array element (variable) ?
ptr float * float pointer variable
*ptr float de-reference of float pointer
variable
?
24
25. Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) ?
a[3] float float array element (variable) ?
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
?
25
26. Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) ?
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
3.14
26
27. Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) ?
ptr float * float pointer variable address of a[3]
*ptr float de-reference of float pointer
variable
?
27
28. Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[3]
*ptr float de-reference of float pointer
variable
9.0
28
29. Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[0]
*ptr float de-reference of float pointer
variable
?
29
30. Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) 6.0
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[0]
*ptr float de-reference of float pointer
variable
6.0
30
31. Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) 6.0
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
3.14
31
32. Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) 6.0
a[1] float float array element (variable) ?
a[2] float float array element (variable) 7.0
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
7.0
32
33. #include <stdio.h>
void disp( int *num)
{
printf("%d ", *num);
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
for (int i=0; i<10; i++)
{
/* Passing addresses of array
elements*/
disp (&arr[i]);
}
return 0;
}
33
Passing 1-D array to a function
a single array element or an entire array can be passed to
a function. This can be done for both one-dimensional array or a
multi-dimensional array.
Using call by reference:
O/p 1 2 3 4 5 6 7 8 9 0
34. Entire array to a function as an argument
34
#include <stdio.h>
void myfuncn( int *var1, int var2)
{
for(int x=0; x<var2; x++)
{ printf("Value of var_arr[%d] is: %d n", x, *var1);
/*increment pointer for next element fetch*/
var1++;
}
}
int main() {
int var_arr[] = {11, 22, 33, 44, 55, 66, 77};
myfuncn(var_arr, 7);
return 0;
}
35. Passing 2-D array in function
# include <stdio.h>
void show(int (*)[3],int,int);
int main()
{
int a[3][3]={{1,2,3},{4,5,6},7,8,9};
show(a,3,3);
return(0);
}
Output: 1 2 3
4 5 6
7 8 9
void show(int (*p)[3],int row,int col)
{
int i,j;
printf("nnn");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("t %d",p[i][j]);
printf("n");
}
}
Pointer to an array
of 3 integers(size
of 1-D)
35
37. Array of pointers
We can declare an array that contains pointers as its element. Every element of this
array is a pointer variable that can hold the address of any variable of appropriate
type.
Syntax: datatype *arrayname[size];
Example:
37
int *arr[10];
# include <stdio.h>
int main()
{
int *pa[3];
int i, a=5, b=10, c=15;
pa[0]= &a;
pa[1]=&b;
pa[2]=&c;
for(i=0;i<3;i++)
{
printf("pa[%d] = %ut",i,pa[i]);
printf("*pa[%d] = %dn",i,*pa[i]);
}
}
pa[0] = 2293516 *pa[0] = 5
pa[1] = 2293512 *pa[1] = 10
pa[2] = 2293508 *pa[2] = 15
pa[0] pa[1] pa[2]
2012 2560 3020
5000 5008 5012
5 1
0
1
5
a b c
5 10 15
2012 2560 3020
38. Example2: Array of pointers can also contain address of
elements of another array
# include <stdio.h>
int main()
{
int i, arr[4] = {5, 10, 15, 20};
int *pa[4];
for(i=0;i<4;i++)
pa[i] = &arr[i];
for(i=0;i<4;i++)
{
printf("pa[%d] = %ut",i,pa[i]);
printf("*pa[%d] = %dn",i,*pa[i]);
}
} pa[0] = 8992 *pa[0] = 5
pa[1] = 8996 *pa[1] = 10
pa[2] = 9000 *pa[2] = 15
pa[3] = 9004 *pa[3] = 20
pa[0] pa[1] pa[2] pa[3]
1000 1002 1004 1006
5000 5008 5012 5018
arr[0] arr[1] arr[2] arr[3]
5 10 15 20
1000 1002 1004 1006
38
39. Void pointers
39
A pointer to void is a generic pointer that can point to any data type.
If we have a pointer to int, then it would be incorrect to assign the address of a
float variable to it but using void pointer we can store.
Void pointer is:
int i=2, *ip = &i;
float f=2.3, *fp=&f;
double d;
void *vp;
ip=fp;
vp=ip;
vp=fp;
vp=&d
40. Dynamic memory allocation
40
Dynamic memory allocation is the allocation of memory storage
for use in a computer program during the runtime of that
program.
Four Dynamic Memory Allocation Functions:
• Allocate memory - malloc(), calloc(), and realloc()
• Free memory - free()
41. malloc()
41
To allocate memory use
void *malloc(size_t size);
• Takes number of bytes to allocate as argument.
• Use sizeof to determine the size of a type.
• Returns pointer of type void *. A void pointer may
be assigned to any pointer.
•If no memory available, returns NULL.
e.g.
char *line;
int linelength = 100;
line = (char*)malloc(linelength);
42. malloc() example
42
To allocate space for 100 integers:
int *ip;
if ((ip = (int*)malloc(100 * sizeof(int))) == NULL){
printf("out of memoryn");
exit();
}
• Note we cast the return value to int*.
• On successfull malloc return a pointer to the newly allocated
memory.
• On error malloc return null;
43. Allocating memory for a struct
43
You can also allocate memory for a struct.
Example:
struct node *newPtr;
newPtr = (struct node *)malloc(sizeof(struct node));
• Memory allocated with malloc() lasts as long as you want it to.
• It does not automatically disappear when a function returns, as local
variables do
• To free the allocated memory there is a mechanism to free allocated
memory.
44. free()
44
To release allocated memory use
free()
• Deallocates memory allocated by malloc().
• Takes a pointer as an argument.
e.g.
free(newPtr);
• Freeing unused memory is a good idea, but it's not mandatory.
When your program exits, any memory which it has allocated but
not freed will be automatically released.
45. calloc()
45
• Similar to malloc(), the main difference is that the values stored in the
allocated memory space are zero by default. With malloc(), the allocated
memory could have any value.
• calloc() requires two arguments - the number of variables you'd like to
allocate memory for and the size of each variable. While in malloc() only
one arguments.
void *calloc(size_t nitem, size_t size);
Example: ptr =(int *)calloc(5,sizeof(int));
• This allocate a 5 blocks memory, each block contains 2 byte and starting
address is stored in a pointer variable ptr.
• Like malloc(), calloc() will return a void pointer if the memory allocation
was successful, else it'll return a NULL pointer.
46. realloc()
46
• When you want to increase or decrese memory allocated by malloc()
and calloc() use realloc().
• The function ralloc() is used to change the size of memory block.
• You give realloc() a pointer (such as you received from an initial call to
malloc()) and a new size, and realloc does what it can to give you a
block of memory big enough to hold the new size.
int *ip;
ip = (int*)malloc(100 * sizeof(int));
...
/* need twice as much space */
ip = (int*)realloc(ip, 200 * sizeof(int));
47. #include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) malloc(num * sizeof(int)); //memory allocated using malloc
if(ptr == NULL)
{
printf("Error! memory not allocated."); exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr); return 0;
}
Output:
Enter number of elements:5
Enter elements of array: 2 5 7 8 9
Sum = 31
Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using malloc() function.
47
48. #include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
//memory allocated using malloc
ptr = (int*) malloc(num * sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated."); exit(0);
}
printf("Enter elements of array: ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr); return 0;
}
Output:
Enter number of elements:5
Enter elements of array: 2 5 7 8 9
Sum = 31
Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using malloc() function.
ptr = (int*) calloc(num,sizeof(int));
48
49. Pointers and Structures
• Structure pointers are just like pointers to a variable of any data-type.
Declaring a structure pointer
struct <struct-name> *<pointer name>;
For eg.,
struct dob
{
int day;
int month;
int year;
};
struct dob *person1;
This declaration for a pointer to structure does not
allocate any memory for a structure but allocates
only for a pointer, so that to access structure’s
members through pointer person1, we must
allocate the memory using malloc() function.
49
50. Access Structure Members - Pointers
• Just like we use “.” operator to access members of a structure using a
normal variable, while for a structure pointer we use “ ->” notation.
• For eg.,
person1->date;
person1->month;
person1->year;
• Other method to access structure members through structure pointers.
(*person1).date;
(*person1).month;
(*person1).year;
*person1.date;
*person1.month;
*person1.year;
50
51. Access Structure Members - Pointers
51
• When using structure pointers, we should take care of operator
precedence.
• Member operator “.” has higher precedence than “*”.
• person1 –> date and (*person1).date mean the same thing.
• *person.date will lead to error.
• The operator “–>” enjoys the highest priority among operators.
• ++person1 –> date will increment date, not person1.
• (++person1) –> date will do the intended thing.
• When pointer is incremented by one i.e., ++person1, it increases by
sizeof(dob), and points the next record, if any.
52. Example Program
#include<stdio.h>
struct point
{
int x, y;
};
int main()
{
struct point p1 = {1, 2};
// p2 is a pointer to structure p1
struct point *p2 = &p1;
// Accessing structure members using structure pointer
printf("%d %d", p2->x, p2->y);
return 0;
}
The address of a structure type variable can be stored in a
structure type pointer variable
52
53. Example Program using malloc()
#include<stdio.h>
struct point
{
int x, y;
};
int main()
{
struct point *p1;
p1=(struct point *)malloc(sizeof(struct point)); //dynamic memory allocation
printf("Enter x,y coordinates");
scanf("%d%d",&p2->x,&p2->y);
printf("%d %d", p2->x, p2->y); // Accessing structure members
return 0;
}
53
54. Subtracting two pointers of same type
54
#include<stdio.h>
void main ()
{
int i = 100;
int *p = &i;
int *q;
q = p;
p = p + 3;
printf("Pointer Subtraction: %d - %d = %d",p, q, p-q);
}
Output: (addresses may vary)
Pointer Subtraction: 1982076 - 1982064 = 3
55. Precedence of dereferencing operator and increment
decrement operator
The precedence level of * operator and increment/decrement operators
is same and their associativity is from right to left.
Suppose ptr is an integer pointer and x is an integer variable
ptr
x = *ptr++ = *ptr++ ==> *(ptr++) o/p x= 25
o/p x=38
o/p x = 26
x = *++ptr = *++ptr ==> *(++ptr)
x= ++ *ptr = ++*ptr ==> ++(*ptr)
x = (*ptr)++ o/p x= 25
25 38
55
56. Pointer to pointer
We can store the address of a pointer variable in some other variable, which
is known as a pointer to pointer variable.
The syntax of declaring a pointer to pointer is as-
data_type **pptr;
double asterisk used in the declaration of pointer to pointer.
int a= 5 ;
int *pa = &a;
int **ppa = &pa;
4000 3000 2000
ppa pa a
3000 2000 5
56
57. Output Questions
57
Q1.
#include "stdio.h"
int main()
{
char a[] = { 'A', 'B', 'C', 'D' };
char* ppp = &a[0];
*ppp++; // Line 1
printf("%c %c ", *++ppp, --*ppp);
// Line 2
}
C A
Q2. #include<stdio.h>
#include <stdio.h>
int main()
{
int i = 25;
int *j;
int **k;
j = &i;
k = &j;
printf("%u %u %u ", k, *k, **k);
return 0;
}
6684168 6684180 25