char *(*pargv)[]

R

Russell Shaw

Hi,
In gcc-3.4.4 i tried:

int main(int argc, char *argv[])
{
...
normalize_argv(&argc, &argv);
}


static void normalize_argv(int *pargc, char *(*pargv)[])
{
...
*pargv = calloc(1, sizeof(char*)); <--- error
}

I get an error:

error: invalid use of array with unspecified bounds
 
C

Chris Torek

int main(int argc, char *argv[])
{
...
normalize_argv(&argc, &argv);
}

The type of "argv" in main() is "char **". See
static void normalize_argv(int *pargc, char *(*pargv)[])
{
...
*pargv = calloc(1, sizeof(char*)); <--- error
}

I get an error:

error: invalid use of array with unspecified bounds

If you had declared the function earlier, you should have gotten
another error first, when calling it. The error you did get here
is correct. To fix it, realize that argv has type "char **",
and have normalize_argv() take a pointer to "char **", i.e., a
value of type "char ***".

You might then want to add a local variable of type "char **",
and use that everywhere inside the function, and only assign
to *pargv on the way out:

static void normalize_argv(int *pargc, char ***pargv) {
char **argv;

... code ...

*pargv = argv;
}

Any time you go beyond a few levels of pointer-ness, things can
get confusing, but you can always solve the problem by adding a
local variable that strips out one "pointer-ness" step.
 
T

Tim Rentsch

Russell Shaw said:
Hi,
In gcc-3.4.4 i tried:

int main(int argc, char *argv[])
{
...
normalize_argv(&argc, &argv);
}


static void normalize_argv(int *pargc, char *(*pargv)[])
{
...
*pargv = calloc(1, sizeof(char*)); <--- error
}

I get an error:

error: invalid use of array with unspecified bounds

A trap for the unwary. The declaration of 'argv' (in the function
header for 'main') makes it look like it's an array (of char *), but
it really isn't. On the other hand, the declaration of 'pargv' is
for a pointer to an (honest-to-goodness) array, and it really is.

The actual type of 'argv' is 'char **'. Declare 'char ** *pargv'
and you should be able to

*pargv = calloc( 1, sizeof **pargv );

if you really want to. Are you actually intent on discarding
the old value of 'argv' and assigning to it a new address (which
address might even be NULL)?

Incidentally, if the declaration had been 'char **pargv[]' it would
have worked, but for the wrong reasons.
 
R

Russell Shaw

Tim said:
Russell Shaw said:
Hi,
In gcc-3.4.4 i tried:

int main(int argc, char *argv[])
{
...
normalize_argv(&argc, &argv);
}

static void normalize_argv(int *pargc, char *(*pargv)[])
{
...
*pargv = calloc(1, sizeof(char*)); <--- error
}

I get an error:

error: invalid use of array with unspecified bounds

A trap for the unwary. The declaration of 'argv' (in the function
header for 'main') makes it look like it's an array (of char *), but
it really isn't. On the other hand, the declaration of 'pargv' is
for a pointer to an (honest-to-goodness) array, and it really is.

The actual type of 'argv' is 'char **'. Declare 'char ** *pargv'
and you should be able to

*pargv = calloc( 1, sizeof **pargv );

if you really want to.

Ok, that worked.
> Are you actually intent on discarding
the old value of 'argv' and assigning to it a new address (which
address might even be NULL)?

Yes, i expand options that have '=' into separate argv elements
which uses more space.
Incidentally, if the declaration had been 'char **pargv[]' it would
have worked, but for the wrong reasons.
 
R

Russell Shaw

Chris said:
Russell Shaw said:
int main(int argc, char *argv[])
{
...
normalize_argv(&argc, &argv);
}


The type of "argv" in main() is "char **". See
<http://web.torek.net/torek/c/expr.html#therule>.

Answers some questions i had a long time...
static void normalize_argv(int *pargc, char *(*pargv)[])
{
...
*pargv = calloc(1, sizeof(char*)); <--- error
}

I get an error:

error: invalid use of array with unspecified bounds


If you had declared the function earlier, you should have gotten
another error first, when calling it.

I did, but i just pasted it different into mozilla.
> The error you did get here
is correct. To fix it, realize that argv has type "char **",
and have normalize_argv() take a pointer to "char **", i.e., a
value of type "char ***".

You might then want to add a local variable of type "char **",
and use that everywhere inside the function, and only assign
to *pargv on the way out:

static void normalize_argv(int *pargc, char ***pargv) {
char **argv;

... code ...

*pargv = argv;
}

That fixed it:)
 

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

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top