array of pointer

H

herrcho

#include <stdio.h>

int main()
{
char *name[5];
int i;

name[0] = "Jung Jae Une";
name[1] = "Han Woo Ryong";
name[2] = "Byun Ji Ha";
name[3] = "Lee Do Geun";
name[4] = "Hong Jae Mok";

for (i=0;i<5 ;i++ )
{
puts(name);
}

for (i=0;i<5 ;i++ )
{
puts(name+i);
}
return 0;
}

i expected the two for statements result in same output.

but, the 'puts' statement in second one shows warning message

---> warning: passing arg 1 of `puts' from incompatible pointer type

and when i execute the file, the second one shows weird characters..

i don't know what is wrong..
 
L

Lew Pitcher

herrcho said:
#include <stdio.h>

int main()
{
char *name[5];
int i;

name[0] = "Jung Jae Une";
name[1] = "Han Woo Ryong";
name[2] = "Byun Ji Ha";
name[3] = "Lee Do Geun";
name[4] = "Hong Jae Mok";

for (i=0;i<5 ;i++ )
{
puts(name);
}

for (i=0;i<5 ;i++ )
{
puts(name+i);
}
return 0;
}

i expected the two for statements result in same output.

but, the 'puts' statement in second one shows warning message

---> warning: passing arg 1 of `puts' from incompatible pointer type

and when i execute the file, the second one shows weird characters..

i don't know what is wrong..


name is the same as *(name + i)

In the first puts() call, you provide
name
as the parameter, so you are in effect providing puts() with
*(name + i)

However, in the second puts() call, you provide
name + i
as the parameter.

You forgot to dereference the pointer-to-pointer






--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
 
C

CBFalconer

herrcho said:
#include <stdio.h>
int main()
{
char *name[5];
int i;

name[0] = "Jung Jae Une";
name[1] = "Han Woo Ryong";
name[2] = "Byun Ji Ha";
name[3] = "Lee Do Geun";
name[4] = "Hong Jae Mok";

for (i = 0; i < 5; i++) puts(name);
for (i = 0; i < 5; i++) puts(name+i);
return 0;
}


** edited for compactness/clarity **
i expected the two for statements result in same output. but,
the 'puts' statement in second one shows warning message

---> warning: passing arg 1 of `puts' from incompatible pointer type

and when i execute the file, the second one shows weird characters..

puts(*(name + i));
 
J

Joe Wright

herrcho said:
#include <stdio.h>

int main()
{
char *name[5];
int i;

name[0] = "Jung Jae Une";
name[1] = "Han Woo Ryong";
name[2] = "Byun Ji Ha";
name[3] = "Lee Do Geun";
name[4] = "Hong Jae Mok";

for (i=0;i<5 ;i++ )
{
puts(name);
}

for (i=0;i<5 ;i++ )
{
puts(name+i);
}
return 0;
}

i expected the two for statements result in same output.

but, the 'puts' statement in second one shows warning message

---> warning: passing arg 1 of `puts' from incompatible pointer type

and when i execute the file, the second one shows weird characters..

i don't know what is wrong..


Well, name is an array of pointers to char. name is one of those
pointers. The alternative to array notation would be *(name+i). Try ...

puts(*(name+i));
 
J

John Bode

herrcho said:
#include <stdio.h>

int main()
{
char *name[5];
int i;

name[0] = "Jung Jae Une";
name[1] = "Han Woo Ryong";
name[2] = "Byun Ji Ha";
name[3] = "Lee Do Geun";
name[4] = "Hong Jae Mok";

for (i=0;i<5 ;i++ )
{
puts(name);
}

for (i=0;i<5 ;i++ )
{
puts(name+i);


puts(*(name + i));
}
return 0;
}

i expected the two for statements result in same output.

but, the 'puts' statement in second one shows warning message

---> warning: passing arg 1 of `puts' from incompatible pointer type

puts() expects a pointer to char. The type of "name" is array[5] of
pointer to char.
and when i execute the file, the second one shows weird characters..

Because you are passing the wrong value to puts. The base address of
the name array is not the same as the address of the first string in
the array.

Remember that the syntax a is equivalent to *(a + i), *not* (a +
i).

Here's a fictional memory map that illustrates what's happening:

Item Address Contents 00 01 02 03 04 05 06 07
----- ------- --------
"str1" 0x000100 's' 't' 'r' '1' '\0'.. .. ..
"str2" 0x000108 's' 't' 'r' '2' '\0'.. .. ..
"str3" 0x000110 's' 't' 'r' '3' '\0'.. .. ..
....
name 0x008000 00 01 00 .. .. .. .. ..
0x008008 00 01 08 .. .. .. .. ..
0x008010 00 01 10 .. .. .. .. ..

Note the following:

name+0 = 0x008000
*(name+0) = 0x000100

name+1 = 0x008008
*(name+1) = 0x000108
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,082
Messages
2,570,588
Members
47,209
Latest member
Ingeborg61

Latest Threads

Top