🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #5037

Re: Q: char **foo, char *foo[], and char foo[][] ?

#5037
From: Larry Jones
Date: Fri, 19 Apr 1996 20:20
37 lines
1683 bytes
In article <KANZE.96Apr18105949@gabi.gabi-soft.fr>, kanze@gabi-soft.fr (J. Kanze) writes:
> In article <4l33ok$oo2@Sherlock.lectra.fr> phil@rd.lectra.fr (Philippe
> Maurisset) writes:
> |> myexample()
> |> {
> |> 	char foo[MAX_X][MAX_Y];
> |> 	...
> |> 	myfunc( (char **)foo );
> |> }
> 
> |> void myfunc( char *foo[MAX_X] )
>                 ^^^^^^^^^^^^^^^^
> 
> Because it appears as a function parameter, this is actually a
> declaration of a char**, and not a char*[].
> 
> So in fact, you have no type incompatibility to deal with.

Yes, you do.  While the cast in the call to myfunc matches the
prototype, the cast itself is erroneous.  The type of foo is actually
char (*)[MAX_Y]: a pointer to an array of characters.  Converting a
pointer to an array into a pointer to a pointer is essentially
meaningless and will not generally work.  The corrected example is:

	myexample()
	{
		char foo[MAX_X][MAX_Y];
		...
		myfunc( foo );
	}
	void myfunc( char (*foo)[MAX_X] )

Even K&R I discusses this in 5.7 Multi-Dimensional Arrays.
----
Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070
larry.jones@sdrc.com
In a minute, you and I are going to settle this out of doors. -- Calvin
Technical Details
Message-ID: <4l8siu$6r3@info1.sdrc.com>
Path: arc1-ca.pugleaf.net!sdrc.com!thor!scjones
References: <4l33ok$oo2@Sherlock.lectra.fr> <KANZE.96Apr18105949@gabi.gabi-soft.fr>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sdrc.com!thor!scjones
From: larry.jones@sdrc.com (Larry Jones)
Newsgroups: comp.std.c
Subject: Re: Q: char **foo, char *foo[], and char foo[][] ?
Date: 19 Apr 1996 20:20:46 GMT
Organization: SDRC Engineering Services
Distribution: world
Message-ID: <4l8siu$6r3@info1.sdrc.com>
References: <4l33ok$oo2@Sherlock.lectra.fr> <KANZE.96Apr18105949@gabi.gabi-soft.fr>
NNTP-Posting-Host: thor.sdrc.com
Originator: scjones@thor