[OCLUG-devel] invalid initializer?

John Heil johnhscs at scsoftware.sc-software.com
Mon Jul 5 20:35:39 PDT 2004


On Mon, 5 Jul 2004, James Colannino wrote:

> Date: Mon, 05 Jul 2004 19:57:57 -0700
> From: James Colannino <james at colannino.org>
> To: oclug-devel at oclug.org
> Subject: [OCLUG-devel] invalid initializer?
>
> Hey everyone.  I know that you can do, say, the following:
>
> char string[] = "Hello";
>
> But, I tried the following:
>
> char filename[] = argv[1];
>
> This resulted in the following compiler error:
>
> error: invalid initializer
>
> What does this mean?  I thought that you could assign a string to a
> variable-length character array at the time you initialize it.  Am I
> missing something?
>
> James

The char filename[] is an array of undefined length in units of characters.
argv[1] is a specific array entry which came from, IIRC, char **argv,
thus argv[1] is a char * ie a pointer to a parameter value which
is a 0x00 terminated string.

You'd need to size filename like char filename[256] then take
use something like strcpy to copy argv[1]'s contents into it.

You should probably do man strcpy or man strncpy just for the
experience of using man for function lookup, but any C text
ought to describe them.

Also you could do char *filename[];
Then filename[0] = argv[1]; ought to work just fine cuz your
placing a pointer into a specific location in an array of pointers
both of which point to chars.

johnh

-
-----------------------------------------------------------------
John Heil
South Coast Software
Custom systems software for UNIX and IBM MVS mainframes
1-714-774-6952
johnhscs at sc-software.com
http://www.sc-software.com
-----------------------------------------------------------------


More information about the OCLUG-devel mailing list