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

H

hn.ft.pris

#######################################
........
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!
 
R

Richard Heathfield

(e-mail address removed) 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.
 
C

CBFalconer

#######################################
.......
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
 
P

pete

#######################################
.......
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 */
 
D

Dave Thompson

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) 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.

- David.Thompson1 at worldnet.att.net
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top