Size of Pointers | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to nourish technologies this is inverse in the last session we discussed the basic introduction concept of a pointers now the very important discussion in pointers concept what is the size of pointer and why the pointer size varies from compiler to compiler we already know that integer size in C language is not constant that will change from one compiler to another compiler in the same way the pointer size also will change how it will change observe suppose here we are taking compiler type compiler type and here it is the size of integer and here it is size of pointer suppose if you are using a 16 bit compiler integer occupies a 2 by its memory integer occupies 2 bytes memory because 8 bits is equals to 1 byte so here it is a 16 bit is equals to 2 bytes pointer size also 2 bytes and suppose if you are using 32-bit compiler 32 bit C compiler and here it is a 4 into 8 is a 32 so here it will occupy 4 bytes integer size and here it is a pointer size is also 4 bytes depends on the compiler type so what compiler we are using depends on the compiler type the size of integer and the size of pointer varies if it is a 16 bit it occupies a 2 bytes if it is a 32 bit it occupies a 4 bytes integer and pointer both are same the reasons are why pointer size varies why the pointer size where is observed here suppose if it is a 16 bit compiler if it is 16-bit compiler exactly 16-bit compiler contains to power 16 memory locations in the machine to power 16 memory addresses present to power 16 suppose I am declaring one variable int I equals 200 some of the memory locations we are giving suppose 128 256 here it is a 512 7:05 wait some addresses now observe we are declaring one pointer variable integer pointer and into PTR we are storing address of i first i gets memory allocation and initializes with a value hundred somewhere in a 128 I gets memory allocation address is 128 that 128 will go on store into PTR PTR can pointing to this location because the address is a 128 only imagine if PTR occupies one byte if PTR occupies one byte what is the one-bite range 256 to power eight value 256 nothing but it can points to only these many locations whatever the data which is stored inside 256 locations we can point by a pointer which is occupying one byte memory for example imagine whenever you declare this variable suppose I not getting memory allocation here I memory allocation is here I now this is the memory location 100 using this pointer it is impossible to pointing to this location because the pointer size is only one byte pointer size only one bite one bite means it should points to only 256 memory locations only these are the memory locations it should point then who will point remaining memory locations what about the data which has stored inside remaining locations how to access so that is why how many memory locations are present the pointer should point into any one of all the available memory locations okay only some of the memory locations it is allowed to point and remaining memory locations not allowed no such type of restriction pointer means right it should access any data which is stored inside the memory anywhere in the memory location anywhere in the memory location so when it can points to this data when the pointer occupies two bytes memory then it can points to any location any location from 0 to 2 power 16 so by that time only it is allowed by the time only it is allowed so suppose if it is a 32-bit compiler then we have total 2 power 32 memory locations total 2 power 32 some more memory locations will be present 2 power 32 memory locations by that time the pointer also should occupy 4 bytes in that case suppose pointer occupies only 2 bytes memory pointer occupies only 2 bytes memory it can points to 2 power 16 memory locations it cannot point in 2 remaining locations then again problem will come suppose I am the faculty I am taking class in this classroom suppose 500 students are there 500 chairs are there just consider I am the pointer as a pointer I should point to all the 500 chairs nothing but all the 500 students right as a faculty right if I see always I always pointing to only hundred students aside what about remaining 400 students the remaining 400 students will effect that is a problem so as a pointer you should be in your position to point all the memory locations in the computer all the chairs as a faculty all the chairs available in this room that you have to point out you should maintain eye contact with all the 500 students that's it okay so this is why the pointer size varies from compiler to compile early we will see a simple program how to find out the size of a pointer practically I will show you okay see how to find the size of a pointer any pointer type you can take observe suppose I am declaring one structure struct employee employee is having employee number next employee name the size 20 we are taking and next employee cell employee cell now main method we are writing wide main integer main or simply main also you can write I am declaring one character pointer variable C P we are taking character pointer variable and next second one we are taking integer pointer variable integer next directly we are printing or struct EMP pointer variable we are declaring instructor EMP pointer here it is s e P we are taking struct employee pointer type now we are printing printf size of character pointer is percentage D size always represents in a integers so integer format specifier it will send the control to next line we know the function we are using sizeof function only CP CP is the character pointer variable CP is a character pointer available next printf size of sizeof integer pointer variable is percentage D sizeof integer pointer in the same way printf size of employee pointer variable is percentage D here it is size of s EP s EP we are writing so whenever we are executing this program what will be the output means generally the people thinking that it is a character type so this pointer occupies one byte and it is a integer and it varies either two bytes or four bytes imagine integer sizes or two bytes so pointer size is also two bytes so here it is a one byte and it is a two bytes and it is a 26 bytes because twenty plus four plus 226 the people thinking like that output should be like this so like that people will think character pointer size is a one byte integer pointer size is a two bytes and here it is employee pointer size is a 26 bytes but this is exact output this is wrong here it is it will give the output like this two bytes employee pointer also two bytes any pointer you can take character pointer float pointer double pointer employee pointer function pointer array pointer string pointer any type of pointer no matter it occupies either two bytes or 4 bytes okay why see the explanation so why every pointer occupies two bytes you will get more clarity right of the previous example here suppose I am declaring one character CH I am storing one element G how many bytes it will occupy one byte memory it will occupy it holds the G at some location to 0 for 6 the size is a one bite one bite memory and suppose I am declaring one short variable short s is equals to some 10 we are storing it holds 10 at some location 5 0 4 8 how many bytes it will occupy 2 bytes memory it will occupy suppose I am declaring one double type variable double D equals 2 3 4 point 5 6 it will store 3 4.56 at some location it occupies how many bytes 8 bytes memory it will occupy next I am declaring one employee point employee variable struct employee so it holds 3 employee number employee named employee cell at some location suppose 7 0 5 2 how many bytes it will occupy 26 bytes observe right ordinary variables whenever you declare ordinary variable the size varies from one data type to another data type because the type of data you are storing is different here you are storing character here you are storing short here you are storing double here you are storing employee depends on the size of the data the size will vary but suppose I am declaring character pointer CP what you are holding address of CH address of CH means you are storing 2 0 4 6 and here it is using short short pointer SP you are storing address of yes and here it is it holds a Phi 0 4 8 and next suppose double double pointer double pointer right here it is a DP equals to address of D and here it is it holds 9 double 0 to a next struct pointer struct employee pointer employee pointer SCP is equals to address of something e here it is holding 7 0 5 to using pointer variables always we are storing same type of data it is pointing to this one it is pointing to this one it is pointing to this one it is pointing to this one it is not holding the data so what it is holding it is holding addresses 2 0 4 6 5 0 4 8 9 double zero two seven zero five two pointer variables always store address address means the value between 0 to 2 power 16 so it always occupies how many bytes - by its memory always occupies how many bytes - bytes memory it will occupy you are not storing information so that is why the pointer size will not vary from one type to another type just like a primitive variables or derived or user-defined data types pointer size always fixed because it is holding address instead of data this is the reason why pointer size fixed either two bytes or four bytes this is all about the size of pointer hope you people enjoy this session in the coming sessions we will see some more examples on pointers thank you for watching thank you and for more videos please subscribe to nourish IP thank you
Info
Channel: Naresh i Technologies
Views: 335,559
Rating: 4.9047618 out of 5
Keywords: C Language, Pointers in C, Srinivas, Naresh IT, Hands on C Language Training, C Language Demo, Online C Language Training, C Language Tutorial Videos, C Language Overview, C Language Interview Questions
Id: Q_NQ9nikUOc
Channel Id: undefined
Length: 17min 11sec (1031 seconds)
Published: Mon Sep 12 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.