Click here to Skip to main content
13,054,329 members (77,818 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I tried this code but it is throwing the below error:
int main(int argc, char **argv)
       int total=0;
        for(int i=0;i<argv;++i)>
       return 0;

Error: Pointer type needed instead of int.
Posted 22-Feb-11 23:47pm
Updated 23-Feb-11 2:41am
CPallini 23-Feb-11 8:40am
And you, please, don't bother posting such pointless comments.
CDP1802 23-Feb-11 6:23am
On the contrary. Most people don't mind helping a beginner. He did not ask us to do his homework and obviously simply got confused by the types of argc and argv. char** is something I also would not want to encounter right at the beginning
CPallini 23-Feb-11 8:42am
Small sidenote: requirements are for two integers, you should check that too.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

OK: Some slight problems here:
argc contains the number of arguments, not argv. Change to:
for(int i=1;i<argc;++i)

and argv contains the arguments themselves, not argc. Change to:

And all should be well.

[edit]And take out the > symbol on the end of your for loop! (If it is really there...) - OriginalGriff[/edit]

Nuri Ismail correctly suggested: the loop must start from 1 (int i = 1) and 'argc' must be at least 3, because 'argv[0]' is reserved for the process name. Example cmd line: 'my_prog.exe 5 6' -> argv[0] = my_prog.exe, argv[1] = 5 and argv[2] = 6. :thumbsup:

I have updated my response to illustrate that.

It was also (very correctly) pointed out that you should check your inputs for:
Number of arguments - argc should be equal to three or you have a problem!
Valid digts - is the argument actually a number?

If you need help with that, please ask again in another question!
CDP1802 23-Feb-11 6:14am
And perhaps he should check that argc really equals two, no more, no less. As it is now, you could also pass 200 arguments. Also, it might be worth a thought what happens when I pass %64§ and +xdfg# as arguments.
OriginalGriff 23-Feb-11 6:15am
Or at least a minimum of two.
I did consider that, but as he is a complete beginner I thought "just fix the problem he has now, don't confuse him." :laugh:
Nuri Ismail 23-Feb-11 6:40am
Griff, the loop must start from 1 (int i = 1) and 'argc' must be at least 3, because 'argv[0]' is reserved for the process name. Example cmd line: 'my_prog.exe 5 6' -> argv[0] = my_prog.exe, argv[1] = 5 and argv[2] = 6.
OriginalGriff 23-Feb-11 6:43am
Good point :blush: My bad - it's been a while since I played with C command line arguments...
I'd 5 that if I could...
Nuri Ismail 23-Feb-11 6:54am
No problem, normally I wouldn't notice such a detail, but nearly a month ago I wrote a small cmd line app which works with arguments.

BTW, all of the remarks from your answer are good and valid. Maybe you should update the answer in order to reflect this little detail about the argv[0], because I'm not sure if the OP will take care to read the comments. :)

Best Regards,
OriginalGriff 23-Feb-11 7:28am
Good idea - done.
Nuri Ismail 23-Feb-11 8:09am
Thank you, Griff! 5+
SAKryukov 23-Feb-11 22:31pm
Anyway, 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 23 Feb 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100