Saket Kr. Pathak
SoItware Developer
3D Graphics

quick review oI Iundamentals oI C with an prospective view oI implementation in daily day to
day liIe.
Funuamentals 0f Piogiamming C

Saket Ki. Pathak Page Ŷ

HIII uII, Iere jusL u Iew duys buck we Iuve u IesLIvuI DIwuII.

As you, ¡ uIso LIougIL Lo buy someLIIng und wenL Lo sLoresJsIowrooms Lo by sIIrL. ¡
seIecLed one In bIue coIor (my IuvorILe) und jusL buy IL, ¡ uIso bougIL u Iunky (obvIousIy
wIILe wILI S murk) Irom LIere. UnIorLunuLeIy LIe puckeL oI sIIrL wus so compucL LIuL IL's
reuIIy u LuugI Lusk Lo udjusL Iunky's puckeL wILIIn LIe sIIrL's.

AII oI sudden, mulloc sLrIke my mInd ... yuuu :) ... pIeuse dn'L IuugI und ¡ know you
peopIe ure noL goIng Lo sLop IuugIIng.

Ok, now LIInk LIe puckeL oI sIIrL Is jusL sume us LIe memory cIunk gIven
by mulloc{size_t) I.e. Ior exucL duLu, LIuL's IL. As;

//Returns the base address/memory location of a memory-chunk
//having size specified within parameter.
char* what_is_malloc(void)
char *chptr_packet = malloc(sizeof(char)*13);
chptr_packet = "Get a shirt\n";

return chptr_packet;

SomeIow ¡ mude some udjusLmenL wILIIn LIe sIIrL's puckeL und mude some spuce Ior
Iunky's puckeL.

TIen IInuIIy ¡ reLurned buck Lo Iome. ¡ Iud Luken IL IIgILIy und move Iorwurd, LIen cume
DIunLerus (purL oI DIwuII LIuL comes u duy beIore IL). SInce ¡ um LIe IoneIy bIrd so LIere
wus scurcILy oI spoons In my pIuce. ¡ decIded Lo buy u spoon-seL (spoon-sLund) In LIIs
DIunLerus occusIon. ¡ bougIL IL.

¡ wus urrungIng LIe spoon-seL umong my kILcIen sLuII LIen,culloc{size_t,
size_t) LrIggered my mInd. Now Iow, culloc reLurns LIe exucL memory cIunk Ior LIe
number oI ILems pussed Lo IL IuvIng purLIcuIur sIze sume us spoon-seL wILI IIxed number
oI LIe pIuce IoIders Ior spoons.


//Returns the base address/memory-location of a memory-chunk
//for n number of objects specified as 1st parameter each
//having size specified within 2nd parameter.
Funuamentals 0f Piogiamming C

Saket Ki. Pathak Page ŷ

char** what_is_calloc(void)
char **chptr_stand = calloc(6, sizeof(char)*12);
chptr_stand [0] = "Tea Spoon";
chptr_stand [1] = "Tea Spoon";
chptr_stand [2] = "Fork";
chptr_stand [3] = "Fork";
chptr_stand [4] = "Butter Knife";
chptr_stand [5] = "Butter Knife";

return chptr_stand ;

AILer LIuL, ¡ wus seurcIIng Ior LIe exumpIe oI7eulloc{void¯,size_t) In LIe room und ¡
goL my Iugguge bug. Yes LIuL's LIe one, wIere I cun udjusL ubouL IuII oI exLru Iugguge
uILer IuII bug usIng cIuIns In IL, sume us 7eullocin, LIuL onIy cIunges LIe sIze oI
objecL und reLurns LIe buse uddress Lo LIe cuIIer. As;

//Returns the base-address/memory-location of a memory-chunk
//passed within the 1st parameter with the new memory-space
// defined as 2nd parameter.
char* what_is_realloc(char* bag)
bag = (char*)realloc(bag, sizeof(char) * 30);
bag = "Shirt, Paint, Tie, Coat, Jacket, Socks, Shoes";

return bag;

Now IeLs Iuve muIn IuncLIon us;

//Global Declaration with the File
char* what_is_malloc(void);
char** what_is_calloc(void);
char* what_is_realloc(char*);
Funuamentals 0f Piogiamming C

Saket Ki. Pathak Page Ÿ

int main()
printf("Let's %s",what_is_malloc());

char** my_place = what_is_calloc();
int number_of_items = 6;
int loopCount;
for (loopCount = 0;
loopCount < number_of_items;
printf("Please give me : %s\n",my_place[loopCount]);

printf("Please take %s in my bag.",

return 0;
Hmmm ... goL u prucLIcuI ImpIemenLuLIon oI LIese memory uIIocuLIon IuncLIons gIven by
DennIs M. RILcIIe, In duIIy IIIe. JusL u vIew :) uILIougI mulloc Is noL so sImpIe Lo creuLe.
¡L's busIcuIIy, u unIon IuvIng u sLrucLure und u Iong Lype vurIubIe us;

//Reference: The C Programming langugae by Kernighan/Ritchie -
//Chapter-8(8.7), Page-186. --- must read
union head
union head *ptr;
unsigned size;
} strct;
long lval;

Hmmm :) ... IL's LIe InsLunce I Iud, muIIocJcuIIocJreuIIoc In my duIIy IIIe ... Iuuu Iuuu
Iuuu :). Yup, u wuy Lo be cIose wILI C progrummIng ... LIuL's IL. Tuke cure ... In someone's
sLyIe ... noL mIne ... I used Lo suy ... cLcI u uguIn ... :)

Sign up to vote on this title
UsefulNot useful