Академический Документы
Профессиональный Документы
Культура Документы
Free eBook
Contact
About
Start Here
C Pointer to Pointer, Pointer to Functions,
Array of Pointers Explained with Examples
by Himanshu Arora on January 27, 2012
In C programming anguage, the concept o! pointers is the most po"er!u concept that makes
C stan# apart !rom other programming anguages$ In the part%I o! this series "e #iscusse# the
!un#amenta concepts aroun# C pointers$
In this artice, "e "i try to #e&eop un#erstan#ing o! some o! the reati&ey compe' concepts$
(he !oo"ing are e'paine# in this artice "ith e'ampes)
1$ Constant pointer an# pointer to constant$
2$ *ointer to pointer "ith an e'ampe
+$ Array o! pointers "ith an e'ampe
,$ *ointer to !unctions "ith an e'ampe
1. C Constant Pointer and Pointer to Constant
As a #e&eoper, you shou# un#erstan# the #i!!erence bet"een constant pointer an# pointer to
constant$
C Constant pointer
A pointer is sai# to be constant pointer "hen the a##ress its pointing to cannot be change#$
-ets take an e'ampe )
char ch, c;
char *ptr = &ch
ptr = &c
In the abo&e e'ampe "e #e!ine# t"o characters ./ch0 an# /c01 an# a character pointer /ptr0$ First,
the pointer /ptr0 containe# the a##ress o! /ch0 an# in the ne't ine it containe# the a##ress o! /c0$
In other "or#s, "e can say that Initiay /ptr0 pointe# to /ch0 an# then it pointe# to /c0$
But in case o! a constant pointer, once a pointer ho#s an a##ress, it cannot change it$ (his
means a constant pointer, i! area#y pointing to an a##ress, cannot point to a ne" a##ress$
I! "e see the e'ampe abo&e, then i! /ptr0 "ou# ha&e been a constant pointer, then the thir# ine
"ou# ha&e not been &ai#$
A constant pointer is #ecare# as )
<type-of-pointer> *const <name-of-pointer>
For e'ampe )
#include<stdio.h>
int main(void)
char ch = !c!;
char c = !a!;
char *const ptr = &ch; "" # constant pointer
ptr = &c; "" $ryin% to assi%n ne& address to a constant pointer. '()*+,,,,
return -;
.
2hen the co#e abo&e is compie#, compier gi&es the !oo"ing error )
/ %cc -'all constptr.c -o constptr
constptr.c0 1n function 2main30
constptr.c040 error0 assi%nment of read-only varia5le 2ptr3
So "e see that, as e'pecte#, compier thro"s an error since "e trie# to change the a##ress he#
by constant pointer$
3o", "e shou# be cear "ith this concept$ -ets mo&e on$
C Pointer to Constant
(his concept is easy to un#erstan# as the name simpi!ies the concept$ 4es, as the name itse!
suggests, this type o! pointer cannot change the &aue at the a##ress pointe# by it$
-ets un#erstan# this through an e'ampe )
char ch = !c!;
char *ptr = &ch
*ptr = !a!;
In the abo&e e'ampe, "e use# a character pointer /ptr0 that points to character /ch0$ In the ast
ine, "e change the &aue at a##ress pointer by /ptr0$ But i! this "ou# ha&e been a pointer to a
constant, then the ast ine "ou# ha&e been in&ai# because a pointer to a constant cannot change
the &aue at the a##ress its pointing to$
A pointer to a constant is #ecare# as )
const <type-of-pointer> *<name-of-pointer>;
For e'ampe )
#include<stdio.h>
int main(void)
char ch = !c!;
const char *ptr = &ch; "" # constant pointer !ptr! pointin% to !ch!
*ptr = !a!;"" '()*+,,, 6annot chan%e the value at address pointed 5y
!ptr!.
return -;
.
2hen the abo&e co#e "as compie#, compier ga&e the !oo"ing error )
/ %cc -'all ptr7const.c -o ptr7const
ptr7const.c0 1n function 2main30
ptr7const.c080 error0 assi%nment of read-only location 2*ptr3
So no" "e kno" the reason behin# the error abo&e ie "e cannot change the &aue pointe# to by
a constant pointer$
2. C Pointer to Pointer
(i no" "e ha&e use# or earne# pointer to a #ata type ike character, integer etc$ But in this
section "e "i earn about pointers pointing to pointers$
As the #e!inition o! pointer says that its a specia &ariabe that can store the a##ress o! an other
&ariabe$ (hen the other &ariabe can &ery "e be a pointer$ (his means that its per!ecty ega !or
a pointer to be pointing to another pointer$
-ets suppose "e ha&e a pointer /p15 that points to yet another pointer /p25 that points to a
character /ch0$ In memory, the three &ariabes can be &isuai6e# as )
So "e can see that in memory, pointer p1 ho#s the a##ress o! pointer p2$ *ointer p2 ho#s the
a##ress o! character /ch0$
So /p25 is pointer to character /ch0, "hie /p15 is pointer to /p25 or "e can aso say that /p25 is a
pointer to pointer to character /ch0$
3o", in co#e /p25 can be #ecare# as )
char *p7 = &ch;
But /p15 is #ecare# as )
char **p9 = &p7;
So "e see that /p15 is a #oube pointer .ie pointer to a pointer to a character1 an# hence the t"o
7s in #ecaration$
3o",
/p15 is the a##ress o! /p25 ie 8000
/7p15 is the &aue he# by /p25 ie 9000
/77p15 is the &aue at 9000 ie /c0
I think that shou# pretty much cear the concept, ets take a sma e'ampe )
#include<stdio.h>
int main(void)
printf(<=n a = ?d=n<,a);
printf(<=n 5 = ?d=n<,5);
return -;
.
int main(void)
C$%&'E
o -inu' Sysa#min CentES J Course % =aster the (oos, Con!igure it Dight, an# be
-a6y
E($$)'
o -inu' 101 Hacks 2n# @#ition eBook % *ractica @'ampes to Bui# a
Strong Foun#ation in -inu'
o Bash 101 Hacks eBook % (ake Contro o! 4our Bash Comman# -ine an# She
Scripting
o Se# an# A"k 101 Hacks eBook % @nhance 4our ?3I` Z -inu' -i!e "ith Se# an#
A"k
o Cim 101 Hacks eBook % *ractica @'ampes !or Becoming Fast an# *ro#ucti&e in
Cim @#itor
o 3agios Core + eBook % =onitor @&erything, Be *roacti&e, an# Seep 2e
P$P%*A& P$'+'
o 12 Ama6ing an# @ssentia -inu' Books (o @nrich 4our Brain an# -ibrary
o 80 ?3I` Z -inu' Sysa#min (utorias
o 80 =ost Fre>uenty ?se# ?3I` Z -inu' Comman#s .2ith @'ampes1
o Ho" (o Be *ro#ucti&e an# ;et (hings Aone ?sing ;(A
o +0 (hings (o Ao 2hen you are Bore# an# ha&e a Computer
o -inu' Airectory Structure .Fie System Structure1 @'paine# "ith @'ampes
o -inu' Crontab) 18 A"esome Cron Job @'ampes
o ;et a ;rip on the ;rep< B 18 *ractica ;rep Comman# @'ampes
o ?ni' -S Comman#) 18 *ractica @'ampes
o 18 @'ampes (o =aster -inu' Comman# -ine History
o (op 10 Epen Source Bug (racking System
o Ci an# Cim =acro (utoria) Ho" (o Decor# an# *ay
o =ommy, I !oun# it< %% 18 *ractica -inu' Fin# Comman# @'ampes
o 18 A"esome ;mai (ips an# (ricks
o 18 A"esome ;ooge Search (ips an# (ricks
o DAIA 0, DAIA 1, DAIA 8, DAIA 10 @'paine# "ith Aiagrams
o Can 4ou (op (hisO 18 *ractica -inu' (op Comman# @'ampes
o (op 8 Best System =onitoring (oos
o (op 8 Best -inu' ES Aistributions
o Ho" (o =onitor Demote -inu' Host using 3agios +$0
o A"k Intro#uction (utoria B 7 A"k *rint @'ampes
o Ho" to Backup -inu'O 18 rsync Comman# @'ampes
o (he ?timate 2get Ao"noa# ;ui#e 2ith 18 A"esome @'ampes
o (op 8 Best -inu' (e't @#itors
o *acket Anay6er) 18 (C*A?=* Comman# @'ampes
o (he ?timate Bash Array (utoria "ith 18 @'ampes
o + Steps to *er!orm SSH -ogin 2ithout *ass"or# ?sing ssh%keygen T ssh%copy%
i#
o ?ni' Se# (utoria) A#&ance# Se# Substitution @'ampes
o ?3I` Z -inu') 10 3etstat Comman# @'ampes
o (he ?timate ;ui#e !or Creating Strong *ass"or#s
o J Steps to Secure 4our Home 2ireess 3et"ork
o (urbocharge *u((4 "ith 12 *o"er!u A##%Ens
CA+E,$&E'
o -inu' (utorias
o Cim @#itor
o Se# Scripting
o A"k Scripting
o Bash She Scripting
o 3agios =onitoring
o EpenSSH
o I*(abes Fire"a
o Apache 2eb Ser&er
o =ySa- Aatabase
o *er *rogramming
o ;ooge (utorias
o ?buntu (utorias
o *ostgreSa- AB
o Heo 2or# @'ampes
o C *rogramming
o CKK *rogramming
o A@-- Ser&er (utorias
o Erace Aatabase
o C="are (utorias