A string in C is an array of characters treated
as a single unit. For example:
You can initialize the 2D string following way:
char str[6] =”hello”;
i.e. a string variable
can store one string. If you need to handle array of strings, what is the
solution?
You may use 2D array of
characters to handle list of strings like 2D array. For example, you want to store
month names of a year i.e. list of 12 months with max length 9
character(“September”). So the declaration will be:
char months [12] [10];
Where, 1st subscript
defines how many strings and 2nd subscript defines how many
characters in a string.
You can initialize the 2D string following way:
char months[12][10]={
“January”, “February”, “March”, “April”, “May”, “June”, “July”,
“August”, “September”, “October”, “November”, “December”}
And this list of string
can be represented in memory as follows:
J
|
a
|
n
|
u
|
a
|
r
|
y
|
‘\0’
|
||
F
|
e
|
b
|
r
|
u
|
a
|
r
|
y
|
‘\0’
|
|
M
|
a
|
r
|
c
|
h
|
‘\0’
|
||||
A
|
p
|
r
|
i
|
l
|
‘\0’
|
||||
M
|
a
|
y
|
‘\0’
|
||||||
J
|
u
|
n
|
e
|
‘\0’
|
|||||
J
|
u
|
l
|
y
|
‘\0’
|
|||||
A
|
u
|
g
|
u
|
s
|
t
|
‘\0’
|
|||
S
|
e
|
p
|
t
|
e
|
m
|
b
|
e
|
r
|
‘\0’
|
O
|
c
|
t
|
o
|
b
|
e
|
r
|
‘\0’
|
||
N
|
o
|
v
|
e
|
m
|
b
|
e
|
r
|
‘\0’
|
|
D
|
e
|
c
|
e
|
m
|
b
|
e
|
r
|
‘\0’
|
If we look at the memory
representation table, total 120 bytes used to store this list of strings. But
if we count the total characters in a list, only 86 bytes requires to store the
list. So 34 (120-86) bytes wastage.
That’s why most
efficient way of storing list of strings is array of pointers to strings and
declared as follows:
char *months [12] = {
“January”, “February”, “March”, “April”, “May”, “June”, “July”,
“August”, “September”, “October”, “November”, “December”}
And the memory
representation will be:
J
|
a
|
n
|
u
|
a
|
r
|
y
|
‘\0’
|
||
F
|
e
|
b
|
r
|
u
|
a
|
r
|
y
|
‘\0’
|
|
M
|
a
|
r
|
c
|
h
|
‘\0’
|
||||
A
|
p
|
r
|
i
|
l
|
‘\0’
|
||||
M
|
a
|
y
|
‘\0’
|
||||||
J
|
u
|
n
|
e
|
‘\0’
|
|||||
J
|
u
|
l
|
y
|
‘\0’
|
|||||
A
|
u
|
g
|
u
|
s
|
t
|
‘\0’
|
|||
S
|
e
|
p
|
t
|
e
|
m
|
b
|
e
|
r
|
‘\0’
|
O
|
c
|
t
|
o
|
b
|
e
|
r
|
‘\0’
|
||
N
|
o
|
v
|
e
|
m
|
b
|
e
|
r
|
‘\0’
|
|
D
|
e
|
c
|
e
|
m
|
b
|
e
|
r
|
‘\0’
|
Now 86 bytes required to
store the list, so no memory wastage. But there is one limitation in array of
pointers to strings. We cannot receive strings from keyboard using scanf() function. So the following code segment will not work during
execution:
int
main()
{
char *names[5];
int count;
for(count=0;count<=5;count++)
scanf("%s",names[count]);
return 0;
}
We can solve this
problem by allocating space using malloc()
following way:
int main()
{
char *names[5], str[20];
int count;
for(count=0;count<=5;count++)
{
scanf("%s",str);
names[count] = (char*)malloc(sizeof(str));
strcpy(names[count],str);
}
for(count=0;count<=5;count++)
printf("%s\n",names[count]);
return 0;
}
{
char *names[5], str[20];
int count;
for(count=0;count<=5;count++)
{
scanf("%s",str);
names[count] = (char*)malloc(sizeof(str));
strcpy(names[count],str);
}
for(count=0;count<=5;count++)
printf("%s\n",names[count]);
return 0;
}
5 comments:
Thank's sir.It's help to get clear concept about array of string in C.But if we use string type array in c++,then number of m*n string will contain .Is not it?
Thanks anup, it is same in both C & C++.
nice article, thanks.
Thnx Sir !
I'm inspired
Post a Comment