473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

#######################################
........
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
4 1264
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
3
by: s.subbarayan | last post by:
Dear all, I encountered the following piece of program: #include <stdio.h> void strprint(char *str); void main() { char *spr="hello"; strprint(spr); }
28
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: ...
11
by: CBegin | last post by:
I am trying out a few things, need ur help to clear a doubt: I have a character pointer initialised as below: char *ptr = "TestString"; When i print this ptr thru printf , it prints the string...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
5
by: hn.ft.pris | last post by:
Hi: I'm a beginer of STL, and I'm wondering why none of below works: ######################################################################## .......... string str("string"); if ( str == "s" )...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.