Your SlideShare is downloading. ×
0
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Lecturer23 pointersin c.ppt

8,088

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
8,088
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
417
Comments
0
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. POINTERS IN ‘C’Prakash Khaire,LecturerB V Patel Inst. of BMC & IT, GopalVidyanagar
  • 2. 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 To allocate memory●Second reason is Instruct compiler how to treat this memory location ML? As int or float...
  • 3. What is a variable?int main() Three memory locations have{ been allocated. int x = 16; float y = 2.3; ●The first memory location char z = 70;. holds int value... ● The second memory} location holds floating-point number. ●The last memory location holds one byte integer.
  • 4. What is a variable?●A variable is a named memory location.●Variables provide direct access to its memorylocation.●Can you understand what will happen when youwrite: int x; X variable x = 44; 44 value 1638 address
  • 5. Memory AddressesEach 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_nameint i=15;●printf(“%d● %u”,i,&i);&i returns the address of i.●
  • 6. 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.
  • 7. Declaring Pointer VariablesLike 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;
  • 8. 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.
  • 9. Suspicious Pointer ConversionWhen you assign a particular type of pointer●variable with address of different type, such typeof automatic type conversion is known assuspicious type conversion.In turbo c compiler it is not cause of any●compilation error but compiler will send onewarning message: suspicious pointerconversion. So we should avoid suspiciouspointer conversion.
  • 10. Generic Pointer●void pointer in c is known as generic pointer. Literalmeaning of generic pointer is a pointer which can pointtype 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.has
  • 11. wild pointerA pointer in c which has not been initialized is●known as wild pointer.● Example:void main(){ int *ptr; Output: Any address printf("%un",ptr); Garbage value printf("%d",*ptr);}
  • 12. Dangling pointer●If any pointer is pointing the memory address ofany variable but after some time that variablehas been deleted from that memory locationwhile pointer is still pointing such memorylocation. Such pointer is known as danglingpointer and this problem is known as danglingpointer problem.
  • 13. Arithmetic Operations withPointerLike 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 anaddress
  • 14. Benefits of using pointer●Efficient in handling arrays and data tables.●Returns multiple values from a function.●Use of pointer arrays to character strings resultsin saving of data storage space in memory.●Supports dynamic memory management.●Can manipulate dynamic data structure.●Increases execution speed and reducesexecution time

×