• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Lecturer23 pointersin c.ppt
 

Lecturer23 pointersin c.ppt

on

  • 7,166 views

 

Statistics

Views

Total Views
7,166
Views on SlideShare
7,029
Embed Views
137

Actions

Likes
3
Downloads
290
Comments
0

3 Embeds 137

http://www.ustudy.in 135
http://localhost 1
http://ustudy.in 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Lecturer23 pointersin c.ppt Lecturer23 pointersin c.ppt Presentation Transcript

    • POINTERS IN ‘C’Prakash Khaire,LecturerB V Patel Inst. of BMC & IT, GopalVidyanagar
    • 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...
    • 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.
    • 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
    • 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.●
    • 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.
    • 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;
    • 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.
    • 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.
    • 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
    • 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);}
    • 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.
    • 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
    • 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