473,671 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why can't we declare an argv variable?

CJ
Functions can accept "argv like" variable definitions, i.e. foo(int
argc, char *argv[]), which was defined/initialized in the C start up
stubs, but we can't declare one for our own use.

Example:

foo(char *args[]) { /* implementation excluded }; // ok

int main(int argc,char *argv[])
{
char *myarr[] = calloc(10,sizeo f(char *)); // compiler error;

foo(myarr);
}

This once was allowed, but removed and I don't understand why? If a
function receiving an argv-like pointer is not problematic, why
declaration of these a problem?

cj

Jun 9 '06 #1
6 3330
CJ wrote:
Functions can accept "argv like" variable definitions, i.e. foo(int
argc, char *argv[]), which was defined/initialized in the C start up
stubs, but we can't declare one for our own use.

Example:

foo(char *args[]) { /* implementation excluded }; // ok Equivalent to foo(char **args);
(Function should have a type though,
void foo (char **args);)
Since the array decomposes to a pointer on calling the function.
Fine.


int main(int argc,char *argv[])
{
char *myarr[] = calloc(10,sizeo f(char *)); // compiler error; Declaring an array of automatic length, with an initialiser which the
compiler can't determine the length from.
char **mayarr = calloc(10,sizeo f(char *));

or

char **myarr = calloc(10,sizeo f(*myarr));
Doing calloc on a pointer is a bad idea though.

foo(myarr);
}

This once was allowed, but removed and I don't understand why? If a
function receiving an argv-like pointer is not problematic, why
declaration of these a problem?


--
imalone
Jun 9 '06 #2
CJ wrote:
Functions can accept "argv like" variable definitions, i.e. foo(int
argc, char *argv[]), which was defined/initialized in the C start up
stubs, but we can't declare one for our own use.
I'm afraid you're wrong.
Example:

foo(char *args[]) { /* implementation excluded }; // ok
`args` is declared as `char *[]`, and rewritten (because it's an array
argument to a function) as `char **`.
int main(int argc,char *argv[])
{
char *myarr[] = calloc(10,sizeo f(char *)); // compiler error;
Yes, because `myarr` is an array (not a pointer) and you can't
assign to arrays in C.

Declare `myarr` as `char **` and you'll be fine. Urm, better make sure
you `#include <stdlib.h>`. Also better would be

char **myarr = calloc( 10, sizeof (*myarr) );

as part of the general mallocating idiom. And, since calloc fills the
/bytes/ with 0s, but this isn't specified or guaranteed to make the
/char */ values null, use:

malloc( 10 * sizeof (*myarr) );

and initialise the store explicitly.

Then:
foo(myarr);
`foo` has no way to tell how big `myarr` is; better make sure to fix that.
}

This once was allowed,
Was it?
but removed and I don't understand why? If a
function receiving an argv-like pointer is not problematic, why
declaration of these a problem?


It isn't. You just have to remember that parameters that look like
they have type Spoo[] are rewritten to be type Spoo*. This is not news.

--
Chris "seeker" Dollin
"Who do you serve, and who do you trust?" /Crusade/

Jun 9 '06 #3

CJ wrote:
Functions can accept "argv like" variable definitions, i.e. foo(int
argc, char *argv[]), which was defined/initialized in the C start up
stubs, but we can't declare one for our own use.

Example:
You'll need:

#include <stdlib.h>

here, for `calloc()`.
foo(char *args[]) { /* implementation excluded }; // ok
Don't use C++ style comments, at least not on Usenet.
You're missing a `return` here, and don't rely on implicit `int`.
int main(int argc,char *argv[])
{
char *myarr[] = calloc(10,sizeo f(char *)); // compiler error;
What were you trying to achieve here? Did you mean:

char **myarr = calloc(10,sizeo f(char *));
foo(myarr);
}

This once was allowed, but removed and I don't understand why? If a
function receiving an argv-like pointer is not problematic, why
declaration of these a problem?


I don't undestand your problem.
With fixes as above, it's OK (in C99, at least).

Jun 9 '06 #4
Chris Dollin wrote:
int main(int argc,char *argv[])
{
char *myarr[] = calloc(10,sizeo f(char *)); // compiler error;


Yes, because `myarr` is an array (not a pointer) and you can't
assign to arrays in C.


Grr. You can't /assign/ to them, but you can /initialise/ them. The
initialiser has to be a bunch of values inside {}, which that call
isn't.

--
Chris "brain? what brain?" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/

Jun 9 '06 #5
CJ
Chris Dollin wrote:
Chris Dollin wrote:
int main(int argc,char *argv[])
{
char *myarr[] = calloc(10,sizeo f(char *)); // compiler error;


Yes, because `myarr` is an array (not a pointer) and you can't
assign to arrays in C.


Grr. You can't /assign/ to them, but you can /initialise/ them. The
initialiser has to be a bunch of values inside {}, which that call
isn't.

--
Chris "brain? what brain?" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/


Thanks to all, but I was not seeking help on writting code and/or info
on STDC headers.

The compiler does not complain when "*arr[]" is part of a function
declaration/definition, however it prevents declaration and creation of
the "variable" array (since C99?). I am curious as to why this syntax
was removed, that all.

It seems to me "char *arr[]" is more descriptive to a follow on
programmer thqt arr is an array of char pointers, and "char **arr" is a
pointer to a char pointer. Both provide the compiler all it needs to
increment/decrement the pointer, with the only "missing" information,
the dimension, prevents a compiler from doing a bounds check.

Do any C99 compilers do bounds checks? For example;

char *arr[10];

arr[10] = "my string";

Will assigning my string to an out of bounds dimension generate a
compiler warning/error? GCC 4.0.2 does not seem to mind the
programming bug.

cj

Jun 9 '06 #6
CJ
CJ wrote:
Chris Dollin wrote:
Chris Dollin wrote:

Grr. You can't /assign/ to them, but you can /initialise/ them. The
initialiser has to be a bunch of values inside {}, which that call
isn't.


Never mind, Mr Dollin answered my question, arr is NOT treated as a
pointer and thus cannot be incremented or decremented.

cj

Jun 9 '06 #7

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

Similar topics

8
3673
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with each word read from the file. A few test programs that make use of **List and realloc( List, blah...
13
6021
by: Vinu | last post by:
hai i have a problem i con't able to print the values of argv int main(int argc, wchar_t* argv) { return o; }
19
6787
by: lihua | last post by:
Hi, Group! I got one question here: We all know that fclose() must be called after file operations to avoid unexpected errors.But there are really cases when you forget to do that!Just like what happens in memory operations, everyone knows the importance of freeing the allocated memory, but there do have memory leaks from time to
3
2840
by: classicist | last post by:
Suppose I have a function which takes an argv-style pseudo-matrix and modifies neither the strings nor the pointers. How do I use the const qualifier to signal my intentions w/o drawing compiler warnings? E.g., int sum_of_lengths_of_strings( const char **s, int ns ) { int i, len = 0; for ( i = 0; i < ns; ++i )
32
8589
by: mnaydin | last post by:
Assume the main function is defined with int main(int argc, char *argv) { /*...*/ } So, is it permitted to modify the argv array? The standard says "The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program,". According to my reading of the standard, for example, ++argv and ++argv are both permitted, but not ++argv because it says nothing about the argv array itself. Is my...
34
1884
by: Chad | last post by:
Given the following code that achieves no useful purpose: #include <string.h> #include <stdio.h> #include <string.h> int manip(char *str) { size_t len = strlen(str)-1; if(len >= 3) {
5
3158
by: jerry | last post by:
I need to modify the code of a command-line tool. The source code starts out like this: int main(int argc, char *argv) { int ch, A, B ; while ((ch = getopt(argc, argv, "AB")) != -1) switch (ch) { case 'A':
8
8084
by: yinglcs | last post by:
Hi, I have the following code: colorIndex = 0; def test(): print colorIndex; This won't work. But it works if i do this:
5
2675
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) (); void *arg; struct threadpool_work *next; } threadpool_work_t;
0
8481
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8400
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8672
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6234
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1814
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.