LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2011, 11:03 AM   #1
oscaringolilingo
Member
 
Registered: Dec 2010
Posts: 34

Rep: Reputation: 3
How come *++argv works and *++myarray doesn't?


Ok, so I'm reading the C programming language, 2nd ed by Kernighan and Ritchie and I decide to compile this bit of code from part "5.10 Command line arguments":

echo2.c
Code:
#include <stdio.h>
/* echo command-line arguments; 2nd version */
main(int argc, char *argv[])
{
while (--argc > 0)
printf("%s%s", *++argv, (argc > 1) ? " " : "");
printf("\n");
return 0;
}
So it compiles without complaints and runs smoothly as expected:

Code:
$ ./echo2 hello world
hello world
I notice they use "*++argv" to scan through "char *argv[]", so I decide to try my own version:

pointerarray2.c
Code:
#include <stdio.h>
/* test */
main()
{

int mylength = 5;
char *myarray[] = {"abcd","efg","hijklm","nopq","rs","tuvwxyz"};

while (--mylength > 0)
printf("%s%s", *++myarray, (mylength > 1) ? " " : "");
printf("\n");
return 0;
}
And when trying to compile I get this

Code:
$ gcc -o pointerarray2 pointerarray2.c
pointerarray2.c: In function ‘main’:
pointerarray2.c:10: error: lvalue required as increment operand
WHY? Isn't "char *myarray[]" the same as "char *argv[]" ?? Is this just whimsy compiler design or what is it? I mean, I could use argv[n] or something to scan through the array but my aim is not to get it working, but to understand why ANSI C makes this difference.
 
Old 02-07-2011, 01:02 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
When char *something[] is the point of definition of an array, the symbol something is the value indicating the location of the array; the value itself has no location (i.e. the compiler inserts the value where it's needed,) which makes it an rvalue. When you use char *something[] as a function argument, you indicate that you're expecting a pointer to an array of fixed size (it's really taken as char **something); to do this, a pointer to the array must be pushed to the stack, which makes it an lvalue, which can be incremented.
Kevin Barry

Last edited by ta0kira; 02-07-2011 at 01:15 PM.
 
1 members found this post helpful.
Old 02-07-2011, 01:11 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by oscaringolilingo View Post
Is this just whimsy compiler design or what is it?
Certainly it is nothing to be blamed on the compiler design. It is a questionable choice in the language design, and one that confuses most beginning C programmers.

To try to clarify Kevin's correct answer, consider this program
Code:
#include <stdio.h>
/* test */
void work(int mylength, char *myarray[])
{
   while (--mylength > 0)
      printf("%s%s", *++myarray, (mylength > 1) ? " " : "");
   printf("\n");
}
main()
{
   int mylength = 5;
   char *myarray[] = {"abcd","efg","hijklm","nopq","rs","tuvwxyz"};
   work( mylength, myarray );
   return 0;
}
That might look like it does the same thing as your code that I copied and split part into a function. But this version compiles and runs.

The key is in the meaning of
Code:
char *myarray[]
which has a very different meaning when declared as an argument of a function than when declared as a variable.

Last edited by johnsfine; 02-07-2011 at 01:17 PM.
 
1 members found this post helpful.
Old 02-07-2011, 01:18 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by johnsfine View Post
Code:
#include <stdio.h>
/* test */
void work(int mylength, char*myarray[])
{
   while (--mylength > 0)
      printf("%s%s", *++myarray, (mylength > 1) ? " " : "");
   printf("\n");

}
main()
{
   int mylength = 5;
   char *myarray[] = {"abcd","efg","hijklm","nopq","rs","tuvwxyz"};
   work( mylength, myarray );
   return 0;
}
Here is an additional clarification:
Code:
#include <stdio.h>
/* test */
main()
{

int mylength = 5;
char *mydata[] = {"abcd","efg","hijklm","nopq","rs","tuvwxyz"};
//char *myarray[] = mydata; /*this will not get you anywhere!*/
char **myarray = mydata;

while (--mylength > 0)
printf("%s%s", *++myarray, (mylength > 1) ? " " : "");
printf("\n");
return 0;
}
Kevin Barry
 
1 members found this post helpful.
Old 02-07-2011, 04:37 PM   #5
oscaringolilingo
Member
 
Registered: Dec 2010
Posts: 34

Original Poster
Rep: Reputation: 3
Oh wow, that was so simple, thank you all for illustrating me!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C cidr argv quantt Programming 11 01-26-2009 07:03 AM
Question about argv[] ghoughto Programming 6 10-25-2004 09:38 AM
argv[] help wanted. (In C) vexer Programming 9 11-17-2003 05:09 AM
argc argv linuxanswer Programming 8 10-25-2003 07:54 PM
Help with argv pilot1 Programming 7 08-23-2003 02:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration