Lecturer23 pointersin c.ppt

14
POINTERS IN ‘C’ Prakash Khaire,Lecturer B V Patel Inst. of BMC & IT, GopalVidyanagar

description

 

Transcript of Lecturer23 pointersin c.ppt

Page 1: Lecturer23 pointersin c.ppt

POINTERS IN ‘C’Prakash Khaire,LecturerB V Patel Inst. of BMC & IT, GopalVidyanagar

Page 2: Lecturer23 pointersin c.ppt

What is a variable?

●Each variable must be defined before you can use it inside your program.●Did you ask yourself why we have to declare variables?●First reason is●Second reason is Instruct compiler how

to treat this memory location ML? As int or float...

To allocate memory

Page 3: Lecturer23 pointersin c.ppt

What is a variable?

int main(){

int x = 16;float y = 2.3;char z = 70;

.

.

.}

Three memory locations have been allocated. ● The first memory location holds int value. ● The second memory location holds floating-point number. ● The last memory location holds one byte integer.

Page 4: Lecturer23 pointersin c.ppt

What is a variable?

●A variable is a named memory location.●Variables provide direct access to its memory location.●Can you understand what will happen when you write: int x;

x = 44;

X variable

44 value

1638 address

Page 5: Lecturer23 pointersin c.ppt

Memory Addresses

●Each Memory Location has an address ➔ each variable has an address.●Memory address in 32-bit Machines is 32-bit unsigned integer number.●How to get the address of a variable? &variable_name●int i=15;●printf(“%d %u”,i,&i);●&i returns the address of i.

Page 6: Lecturer23 pointersin c.ppt

Pointer Variables

●Ask yourself:●What is an integer variable?●What is a floating-point variable?●What is a character variable?●Now, what is a pointer variable?●It is a variable that stores the memory address.OR●It points to a storage locations of memory.

OR●Pointers contain memory addresses as their values.

Page 7: Lecturer23 pointersin c.ppt

Declaring Pointer Variables

●Like ordinary variables, pointer variable must be declared before they are used.●Declaration form●datatype *pointer_variable;●The * tells the compiler that this is pointer variable●Pointer_variable will point to the given datatype●Declaration Style●int* iptr;●int *iptr;●int * iptr;

Page 8: Lecturer23 pointersin c.ppt

Example

●int *iptr;●iptr is pointer variable of integer data type, it will store the memory address of int data type.

●float *fptr;●fptr is pointer variable of float data type, it will store memory address of float data type.

●char *cptr;●cptr is pointer variable of character data type, it will store memory address of character data type.

Page 9: Lecturer23 pointersin c.ppt

Suspicious Pointer Conversion

●When you assign a particular type of pointer variable with address of different type, such type of automatic type conversion is known as suspicious type conversion. ●In turbo c compiler it is not cause of any compilation error but compiler will send one warning message: suspicious pointer conversion. So we should avoid suspicious pointer conversion.

Page 10: Lecturer23 pointersin c.ppt

Generic Pointer

●void pointer in c is known as generic pointer. Literal meaning of generic pointer is a pointer which can point type of data.●void *ptr;●Here ptr is generic pointer.●NULL pointer is a pointer which is pointing to nothing. NULL pointer points the base address of segment.●int *ptr=(char *)0;●float *ptr=(float *)0;●char *ptr=(char *)0;●double *ptr=(double *)0;●char *ptr=’\0’;●int *ptr=NULL;●NULL is macro constant which has been defined in the heard file stdio.h, alloc.h, mem.h, stddef.h and stdlib.h as●

Page 11: Lecturer23 pointersin c.ppt

wild pointer

●A pointer in c which has not been initialized is known as wild pointer.

●Example:void main(){ int *ptr; printf("%u\n",ptr); printf("%d",*ptr);}

Output: Any address

Garbage value

Page 12: Lecturer23 pointersin c.ppt

Dangling pointer

●If any pointer is pointing the memory address of any variable but after some time that variable has been deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.

Page 13: Lecturer23 pointersin c.ppt

Arithmetic Operations with Pointer●Like ordinary variables, pointer variables can also be used as a part of expression.●The following rules are applied when we perform arithmetic operations on pointer variables.●Address + Number= Address●Address - Number= Address●Address++ = Address●Address-- = Address●++Address = Address●--Address = Address●If we will add or subtract a number from an address result will also be an address

Page 14: Lecturer23 pointersin c.ppt

Benefits of using pointer

●Efficient in handling arrays and data tables.●Returns multiple values from a function.●Use of pointer arrays to character strings results in saving of data storage space in memory.●Supports dynamic memory management.●Can manipulate dynamic data structure.●Increases execution speed and reduces execution time