Data Types and Pointer Arithmetic's
Why we need Memory at Run Time?
Content to be covered
• Example program to understand Data types
• How data types works for Pointers
• Pointer Arithmetic's
• Pointer to an array
• Need of Dynamic Memory Allocation?
Twists & Turns
What output you expect?
• 2 2
• 4 4
• 1 1
?What you think is it right?
No, correct output is:
• 2 2
• 4 2
• 1 2
• The Reason: Because pointer is made to store addresses
so only 2 bytes are required because address of anything
(int, float, char) is integer number only.
• Then question should come in your mind what is the
significance of data types with respect to pointers?
Pointer Arithmetic's
• Consider the following array:
• int a[5] then compiler will provide following memory addresses.
• a
• Now if a=1000 then what is a+1=? Should it be 1001?
• No!!!, a+1 should be 1002, you might be surprised how it is 1002 it should be 1001
but consider we are talking about addresses so next address is 1002 similarly 1004,
1006 etc. are the addresses of next locations.
1000, 1001 1002, 1003 1004, 1005 1006, 1007 1008, 1009
Pointer Arithmetic's Cont.
• If previous array would have belonging to floats then
a=1000 and a+1 would have been 1004.
• Similarly for other data types.
Need of Dynamic Memory?
• int a[n], just imagine can you have such an array declaration ?
• So what you think YES or NO ?
• Answer is NO, because compiler cannot compute (2*n) bytes of memory to allocate.
• Even if you take a scanf(%d”,&n) statement before this statement still it is not possible because
scanf will take input at run time and compiler needs to allocate memory at compile time.
• So to solve this problem we had to declare int a[1000] or any big number and then we take n
inputs.
• But this way we were wasting memory, so to reduce this wastage of memory we need to learn
Dynamic Memory Allocation.
Pointer Size
Imp. Note: Every pointer variable always consumes 2 bytes in memory
no matter whatever is its data type.
Before learning why it happens let’s prove this statement with the help of a
small program. Consider the following C program.
/*Program for finding out size of pointers*/
#include<stdioh>
#include<conio.h>
void main()
{
int a, *b;
float c, *d;
char e, *f;
printf(“%d%d”,sizeof(a), sizeof(b));
printf(“%d%d”, sizeof(c), sizeof(d));
printf(“%d%d”, sizeof(e), sizeof(f));
getch();
}
You may think that its output may
be 22, 44, 11 but when you will
execute this code on C compiler
surprisingly you will see 22. 42, 12
on output screen, so it proves the
above statement.
Now let’s understand the reason:
Remember pointers are made to
address only and as explained in
introduction section address can
never be a float or character in itself
therefore pointers are never required
to store any float or character value
so it never needs a memory size of
other then 2 bytes. So size of pointer
is always 2 bytes irrespective of its
data type.

Pointers lesson 3 (data types and pointer arithmetics)

  • 1.
    Data Types andPointer Arithmetic's Why we need Memory at Run Time?
  • 2.
    Content to becovered • Example program to understand Data types • How data types works for Pointers • Pointer Arithmetic's • Pointer to an array • Need of Dynamic Memory Allocation?
  • 3.
  • 4.
    What output youexpect? • 2 2 • 4 4 • 1 1 ?What you think is it right?
  • 5.
    No, correct outputis: • 2 2 • 4 2 • 1 2 • The Reason: Because pointer is made to store addresses so only 2 bytes are required because address of anything (int, float, char) is integer number only. • Then question should come in your mind what is the significance of data types with respect to pointers?
  • 6.
    Pointer Arithmetic's • Considerthe following array: • int a[5] then compiler will provide following memory addresses. • a • Now if a=1000 then what is a+1=? Should it be 1001? • No!!!, a+1 should be 1002, you might be surprised how it is 1002 it should be 1001 but consider we are talking about addresses so next address is 1002 similarly 1004, 1006 etc. are the addresses of next locations. 1000, 1001 1002, 1003 1004, 1005 1006, 1007 1008, 1009
  • 7.
    Pointer Arithmetic's Cont. •If previous array would have belonging to floats then a=1000 and a+1 would have been 1004. • Similarly for other data types.
  • 8.
    Need of DynamicMemory? • int a[n], just imagine can you have such an array declaration ? • So what you think YES or NO ? • Answer is NO, because compiler cannot compute (2*n) bytes of memory to allocate. • Even if you take a scanf(%d”,&n) statement before this statement still it is not possible because scanf will take input at run time and compiler needs to allocate memory at compile time. • So to solve this problem we had to declare int a[1000] or any big number and then we take n inputs. • But this way we were wasting memory, so to reduce this wastage of memory we need to learn Dynamic Memory Allocation.
  • 9.
    Pointer Size Imp. Note:Every pointer variable always consumes 2 bytes in memory no matter whatever is its data type. Before learning why it happens let’s prove this statement with the help of a small program. Consider the following C program.
  • 10.
    /*Program for findingout size of pointers*/ #include<stdioh> #include<conio.h> void main() { int a, *b; float c, *d; char e, *f; printf(“%d%d”,sizeof(a), sizeof(b)); printf(“%d%d”, sizeof(c), sizeof(d)); printf(“%d%d”, sizeof(e), sizeof(f)); getch(); } You may think that its output may be 22, 44, 11 but when you will execute this code on C compiler surprisingly you will see 22. 42, 12 on output screen, so it proves the above statement. Now let’s understand the reason: Remember pointers are made to address only and as explained in introduction section address can never be a float or character in itself therefore pointers are never required to store any float or character value so it never needs a memory size of other then 2 bytes. So size of pointer is always 2 bytes irrespective of its data type.