pointer level 2

19
Prepared by Mohammed Sikander Technical Lead Cranes Software International Limited

Upload: mohammed-sikander

Post on 13-Jan-2017

38 views

Category:

Software


0 download

TRANSCRIPT

Prepared by Mohammed SikanderTechnical LeadCranes Software International Limited

[email protected] 2

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

• Can we make a pointer point to string literal?

[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.

[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));

[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));

We 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?

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?

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};

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

Bangalore

Chennai

Guwahati

Amravathi

****

char *

char *

char *

char *

char *str = “VxWorks”;

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.

[email protected] 14

char **

*Bangalore

Chennai

Guwahati

Amravathi

NULL

****

char *

char *

char *

char *

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

Dereferencing on void pointers are not allowed.

Incrementing void pointers are not allowed.