30
C Programming Pointers – Level-II Prepared by Mohammed Sikander Technical Lead Cranes Software International Limited

Pointer level 2

Embed Size (px)

Citation preview

Page 1: Pointer   level 2

C ProgrammingPointers – Level-II

Prepared by Mohammed SikanderTechnical LeadCranes Software International Limited

Page 2: Pointer   level 2

[email protected] 2

char *ptr_name = “SIKANDER”;char str_name[ ] = “SIKANDER”;

• Can we make a pointer point to string literal?

Page 3: Pointer   level 2

[email protected] 3

char *ptr_name = “SIKANDER”;char str_name[ ] = “SIKANDER”;

printf(“ %s \n“ , ptr_name);printf(“ %s \n“ , str_name);

• Can we print the string through pointer.

Page 4: Pointer   level 2

[email protected] 4

char *ptr_name = “SIKANDER”;char str_name[ ] = “SIKANDER”;

printf(“Length of %s = %d\n“ , ptr_name , strlen(ptr_name));

printf(“Length of %s = %d\n“ , str_name , strlen(str_name));

Page 5: Pointer   level 2

[email protected] 5

char *ptr_name = “SIKANDER”;char str_name[ ] = “MUQADDAR”;

printf(“Size of ptr_name = %d\n“ , sizeof(ptr_name));

printf(“Size of str_name = %d\n“ , sizeof(str_name));

Page 8: Pointer   level 2

[email protected] 8

Array of Pointers

Page 9: Pointer   level 2

Array of PointersWe can have arrays where each element can be a pointer

int *

char **

int *

int *

int *

int *

int *arr_ptr[5];

int

int

int

int

int

int arr[5];

What is size of arr?What is size of arr_ptr?

Page 10: Pointer   level 2

Array of Pointers

char **

Char *arr_ptr[5];

char *

char *

char *

char *

char *

char

char

char

char

char

Char arr[5];

What is size of arr?What is size of arr_ptr?

Page 11: Pointer   level 2

int x = 10;int *ptr =

&x;

int arr[5] int *ptr =

arr;

int a = 10;int b = 23;int arr[ ] = {12,34,54};int *ptr[ ] = {&a , &b ,

arr};

Page 12: Pointer   level 2

Array of Pointers

char *capitalCities[] = { “Bangalore”,“Chennai”, “Guwahati”, “Amravathi”};

Bangalore

Chennai

Guwahati

Amravathi

****

char *

char *

char *

char *

char *str = “VxWorks”;

Page 14: Pointer   level 2

[email protected] 14

Array of Pointers

names++ tries to change the base address of array. We cannot change the base address of array. Str was a pointer, names is an array.

Page 15: Pointer   level 2

[email protected] 15

Sorting of Strings stored in Array of Pointers

Page 16: Pointer   level 2

[email protected] 16

Passing Array of pointers to function.

Page 17: Pointer   level 2

Array of Pointers

char **

*Bangalor

eChennai

Guwahati

Amravathi NULL

****

char *

char *

char *

char *

Page 18: Pointer   level 2

void pointers

Void pointers can hold the address of any type of object.

Dereferencing on void pointers are not allowed.

Incrementing void pointers are not allowed.

Page 19: Pointer   level 2

[email protected] 19

Void pointer

Page 20: Pointer   level 2

[email protected] 20

Program to demonstrate double free corruption.

Page 21: Pointer   level 2

[email protected] 21

Program to solve double free corruption.

Page 23: Pointer   level 2

[email protected] 23

Trying to delete memory of stack segment using free.

Page 24: Pointer   level 2

[email protected] 24

Memory allocated by calloc is set to zero.

Page 25: Pointer   level 2

[email protected] 25

Understanding Realloc How many bytes of memory is leaked

Page 26: Pointer   level 2

[email protected] 26

Program to understand realloc.If realloc allocates new block in different location, it copies data from old block to new block

Page 27: Pointer   level 2

[email protected] 27

Identify the problem

Page 28: Pointer   level 2

[email protected] 28

By making ptr as constant pointer, any modification to ptr can be caught at compile time.

Page 29: Pointer   level 2

[email protected] 29

Write the output : DMA