POINTERS IN ‘C’
Prakash Khaire,Lecturer
B 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 memory
location.
●Can you understand what will happen when you
write:
             int x;
                             X     variable
       x = 44;
                          44    value

                         1638       address
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.
●
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 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;
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 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.
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
wild pointer
A 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 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.
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
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

Lecturer23 pointersin c.ppt

  • 1.
    POINTERS IN ‘C’ PrakashKhaire,Lecturer B V Patel Inst. of BMC & IT, GopalVidyanagar
  • 2.
    What is avariable? 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 avariable? 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 avariable? ●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 variable x = 44; 44 value 1638 address
  • 5.
    Memory Addresses Each MemoryLocation 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. ●
  • 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 Variables Likeordinary 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 Conversion Whenyou 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.
  • 10.
    Generic Pointer ●void pointerin 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
  • 11.
    wild pointer A pointerin 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 anypointer 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.
  • 13.
    Arithmetic Operations with Pointer Likeordinary 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
  • 14.
    Benefits of usingpointer ●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