By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

Beginer's question about array pointer, why doesn't my programme work?

P: n/a
#######################################
........
void argParser(int, char**);

int main(int argc, char** argv){
argParser(argc, argv);
return 1;
}

void argParser(int argc, char** argv){
char (*p)[argc] = (char(*)[argc])argv;

for(int i=0; i< argc; i++){
cout << *p+i << endl;
}
########################################
I've compiled above prgramme using gcc 3.4.2, and it passes, but it
doesn't work as I expected.
I thought it should simply output the executable programme name and all
the command line argument, but under linux I get some unmeaningful
characters. Why doesn't it work, thanks for helping me!

Dec 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
hn********@gmail.com said:
#######################################
.......
void argParser(int, char**);

int main(int argc, char** argv){
argParser(argc, argv);
return 1;
}

void argParser(int argc, char** argv){
char (*p)[argc] = (char(*)[argc])argv;

for(int i=0; i< argc; i++){
cout << *p+i << endl;
}
########################################
I've compiled above prgramme using gcc 3.4.2, and it passes,
This is not legal C90, because cout and endl are not defined, and because
you are using a VLA and for(int... syntax. It is not legal C99, because of
the cout/endl thing. It is not even legal C++, because of the VLA usage.

So a diagnostic message is *required*, whichever language you're using. If
you're not getting one, I suggest you check your compiler switches very
carefully indeed.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 20 '06 #2

P: n/a
hn********@gmail.com wrote:
>
#######################################
.......
void argParser(int, char**);

int main(int argc, char** argv){
argParser(argc, argv);
return 1;
}

void argParser(int argc, char** argv){
char (*p)[argc] = (char(*)[argc])argv;

for(int i=0; i< argc; i++){
cout << *p+i << endl;
}
########################################
I've compiled above prgramme using gcc 3.4.2, and it passes, but it
doesn't work as I expected.
junk.c: In function `argParser':
junk.c:9: warning: ISO C89 forbids variable-size array `p'
junk.c:9: warning: ISO C89 forbids variable-size array `type name'
junk.c:11: `for' loop initial declaration used outside C99 mode
junk.c:12: `cout' undeclared (first use in this function)
junk.c:12: (Each undeclared identifier is reported only once
junk.c:12: for each function it appears in.)
junk.c:12: `endl' undeclared (first use in this function)
junk.c:14: parse error at end of input

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Dec 20 '06 #3

P: n/a
hn********@gmail.com wrote:
>
#######################################
.......
void argParser(int, char**);

int main(int argc, char** argv){
argParser(argc, argv);
return 1;
}

void argParser(int argc, char** argv){
char (*p)[argc] = (char(*)[argc])argv;

for(int i=0; i< argc; i++){
cout << *p+i << endl;
}
########################################
/* BEGIN argParser.c */

#include <stdio.h>

void argParser(int argc, char** argv);

int main(int argc, char** argv)
{
argParser(argc, argv);
return 0;
}

void argParser(int argc, char** argv)
{
int index = 0;

while (argv[index] != NULL) {
puts(argv[index++]);
}
argc;
}

/* END argParser.c */
--
pete
Dec 20 '06 #4

P: n/a
On 20 Dec 2006 01:52:10 -0800, hn********@gmail.com wrote:
void argParser(int argc, char** argv){
char (*p)[argc] = (char(*)[argc])argv;

for(int i=0; i< argc; i++){
cout << *p+i << endl;
}
########################################
I've compiled above prgramme using gcc 3.4.2, and it passes, but it
doesn't work as I expected.
I thought it should simply output the executable programme name and all
the command line argument, but under linux I get some unmeaningful
characters. Why doesn't it work, thanks for helping me!
Setting aside the invalid mixture of C99 and <OT>C++ features already
noted, and the valuelessness of making argv a VLA type, that's the
wrong type. It claims argv points to argc _char_ elements, when
actually it points to argc (but the number doesn't matter) _pointer to
char_ elements. You then try to print various parts of the
(array/sequence of) pointers as if they were strings.

If you make it char * (*p) [argc], then you need *(*p + i) or more
clearly (*p)[i] to get a pointer to the i'th string, which can be
printed with << in C++ or printf ("%s\n", ) or just puts ( ) .
If you leave it char * * p then just *(p+i) or p[i].

- David.Thompson1 at worldnet.att.net
Jan 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.