473,503 Members | 3,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char *(*pargv)[]

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
Nov 14 '05 #1
4 1709
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
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.

Nov 14 '05 #2
In article <79************@main.anatron.com.au>,
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
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>.
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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3
Tim Rentsch wrote:
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
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.

Nov 14 '05 #4
Chris Torek wrote:
In article <79************@main.anatron.com.au>,
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> wrote:
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:)
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.

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2189
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
9715
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
2506
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
2
3386
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
5
3939
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
4
1042
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok...
12
10054
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
4
3187
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
29
9923
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5559
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.