Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

question on passing char** to function

5 views
Skip to first unread message

Eric

unread,
Dec 11, 2009, 3:45:31 PM12/11/09
to
Here is what i am trying to do:

void fn(char **a_list);
int main(void)
{
char list[6][32];

fn(list);
printf("list 0 %s\n", list[0]);
printf("list 1 %s\n", list[1]);
printf("list 2 %s\n", list[2]);
}
void fn(char **a_list)
{
strcpy(a_list[0], "one");
strcpy(a_list[1], "two");
strcpy(a_list[2], "three");
}

How do i do this correctly?
I'm not sure how to declare it in the fn prototype or how to
pass the list to fn.
Can someone set me straight here on how this should be done?
Thanks
Eric

--
Msg to ET:
If you get here and there's no planet -
dont ever build a Large Hadron Collider
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Hans-Bernhard Bröker

unread,
Dec 11, 2009, 6:03:49 PM12/11/09
to
Eric wrote:
> Here is what i am trying to do:
>
> void fn(char **a_list);
> int main(void)
> {
> char list[6][32];
>
> fn(list);

'list' used like this is equivalent to a pointer to list[0], i.e.
&(list[0]). But the type of that thing is "pointer to array of 32
chars", not "pointer to pointer to char", so you can't legally pass it
to fn().

What you need would be more like this:

char list[6][32];
char *(plist[6]) = {list[0], list[1], list[2],
list[3], list[4], list[5]};
fn(plist);

Either that, or your function has to take a pointer-to-array-of-32-chars
as its argument:

void fn(char (* alist)[32]);

Jasen Betts

unread,
Dec 12, 2009, 1:25:32 PM12/12/09
to
On 2009-12-11, Eric <apo...@ruler.of.the.night.org> wrote:
> Here is what i am trying to do:
>
> void fn(char **a_list);
> int main(void)
> {
> char list[6][32];
>
> fn(list);
> printf("list 0 %s\n", list[0]);
> printf("list 1 %s\n", list[1]);
> printf("list 2 %s\n", list[2]);
> }
> void fn(char **a_list)
> {
> strcpy(a_list[0], "one");
> strcpy(a_list[1], "two");
> strcpy(a_list[2], "three");
> }
>
> How do i do this correctly?

here's one way. may suit you may not.

void fn(char (*a_list)[32])

Barry Schwarz

unread,
Dec 14, 2009, 1:08:15 PM12/14/09
to
On Sat, 12 Dec 2009 12:25:32 -0600 (CST), Jasen Betts
<ja...@xnet.co.nz> wrote:

>On 2009-12-11, Eric <apo...@ruler.of.the.night.org> wrote:
>> Here is what i am trying to do:
>>
>> void fn(char **a_list);
>> int main(void)
>> {
>> char list[6][32];
>>
>> fn(list);
>> printf("list 0 %s\n", list[0]);
>> printf("list 1 %s\n", list[1]);
>> printf("list 2 %s\n", list[2]);
>> }
>> void fn(char **a_list)
>> {
>> strcpy(a_list[0], "one");
>> strcpy(a_list[1], "two");
>> strcpy(a_list[2], "three");
>> }
>>
>> How do i do this correctly?
>
>here's one way. may suit you may not.
>
>void fn(char (*a_list)[32])

I wonder why everyone seems to be avoiding the obvious

void fn(char a_list[][32])

or even the equally correct but slightly misleading

void fn(char a_list[6][32])

both of which mean exactly the same as the pointer notation but are
immediately recognizable from the definition of the array without
having to think about how to convert to/from the pointer to array
syntax which is frequently confusing for newcomers to C. It works
especially well if the array type is a typedef.

--
Remove del for email

John Bode

unread,
Dec 23, 2009, 1:58:07 PM12/23/09
to
On Dec 11, 2:45 pm, Eric <apop...@ruler.of.the.night.org> wrote:
> Here is what i am trying to do:
>
> void fn(char **a_list);
> int main(void)
> {
> char list[6][32];
>
>     fn(list);
>     printf("list 0 %s\n", list[0]);
>     printf("list 1 %s\n", list[1]);
>     printf("list 2 %s\n", list[2]);}
>
> void fn(char **a_list)

That should be

void fn (char (*a_list)[32])

> {
>     strcpy(a_list[0], "one");
>     strcpy(a_list[1], "two");
>     strcpy(a_list[2], "three");
>
> }
>
> How do i do this correctly?
> I'm not sure how to declare it in the fn prototype or how to
> pass the list to fn.
> Can someone set me straight here on how this should be done?
> Thanks
> Eric
>

When an expression of array type appears in most contexts, the type of
the expression is implicitly converted from "array of T" to "pointer
to T", and the value of the expression is set to point to the first
element of the array. The exceptions to this rule are when the array
expression is an operand of either the sizeof or address-of (&)
operators, or is a string literal being used as an initializer in a
declaration.

In the function call

fn(list);

the type of "list" is implicitly converted from "6-element array of 32-
element array of char" to "pointer to 32-element array of char", and
it's value is the the same as &list[0]. Note that the expressions
list, &list, &list[0], and &list[0][0] all result in the same *value*
(the address of the first element in the array), but the types will
all be different, as shown below:

Expression Type Implicitly converted to
---------- ---- -----------------------
list char [6][32] char (*)[32]
&list char (*)[6][32]
&list[0] char (*)[32]
&list[0][0] char *

And just for completeness:

list[0] char [32] char *
list[0][0] char

Note that the types of &list and &list[0] are pointer types, *not*
array types, so there's no implicit conversion.

0 new messages