I need a technical explanation on why I get 'error: invalid lvalue inincrement'

C

Chad

Given the following...

#include <stdio.h>
#include <stdlib.h>

void
printme(char *s)
{
printf("%s\n", s);
}

int
main( void )
{
/*char **cmd;*/
char *cmds[] = {
"This",
"is",
"a line",
NULL
};

for( ;*cmds != NULL; cmds++ ) {
printme( *cmds);
}

return 0;
}

I get 'error: invalid lvalue in increment' when I try to complie it..

[cdalten@localhost oakland]$ gcc -g -Wall traverse.c -o traverse
traverse.c: In function ‘main’:
traverse.c:21: error: invalid lvalue in increment
[cdalten@localhost oakland]$


Why can't I just increment cmds in this case?


Chad
 
K

Keith Thompson

Chad said:
Given the following...
[...]
/*char **cmd;*/
char *cmds[] = {
"This",
"is",
"a line",
NULL
};

for( ;*cmds != NULL; cmds++ ) {
printme( *cmds);
} [...]

I get 'error: invalid lvalue in increment' when I try to complie it..

[cdalten@localhost oakland]$ gcc -g -Wall traverse.c -o traverse
traverse.c: In function ‘main’:
traverse.c:21: error: invalid lvalue in increment
[cdalten@localhost oakland]$


Why can't I just increment cmds in this case?

Because cmds is the name of an array, not a pointer.

An array name (or, more generally, any expression of array type) is
implicitly converted to a pointer to the array's first element in most
contexts -- but the resulting pointer is not an lvalue.

If you have a pointer *object*, you can increment it, but all you have
here is a pointer *value* (i.e., an address). That value isn't stored
anywhere.

You can do something like this:

char **ptr;

for (ptr = cmds; *ptr != NULL; ptr++ ) {
printme( *ptr);
}
 
C

CBFalconer

Chad said:
Given the following...

#include <stdio.h>
#include <stdlib.h>

void printme(char *s) {
printf("%s\n", s);
}

int main( void ) {
/*char **cmd;*/
char *cmds[] = {
"This",
"is",
"a line",
NULL
};

for ( ;*cmds != NULL; cmds++ ) {
printme( *cmds);
}
return 0;
}

I get 'error: invalid lvalue in increment' when I try to complie it..
.... snip ...

Why can't I just increment cmds in this case?

Because there is no such thing. Try the following for the for:

for (p = cmds; *p; p++) printme(p);

You may need to declare a "char* p;" earlier.
 
K

Keith Thompson

CBFalconer said:
Chad wrote: [...]
char *cmds[] = {
"This",
"is",
"a line",
NULL
};

for ( ;*cmds != NULL; cmds++ ) {
printme( *cmds);
} [...]
Why can't I just increment cmds in this case?

Because there is no such thing.

There is no such thing as what? There certainly is such a thing as
cmds; it's just not something you can increment.
Try the following for the for:

for (p = cmds; *p; p++) printme(p);

You may need to declare a "char* p;" earlier.

No, you need a "char **p;" -- as I explained in my own response about
9 hours ago. In any case, section 6 of the comp.lang.c FAQ is a
better source of information than either of our followups.
 
C

CBFalconer

Keith said:
CBFalconer said:
Chad wrote:
[...]
Why can't I just increment cmds in this case?

Because there is no such thing.

There is no such thing as what? There certainly is such a thing
as cmds; it's just not something you can increment.
Try the following for the for:

for (p = cmds; *p; p++) printme(p);

You may need to declare a "char* p;" earlier.

No, you need a "char **p;" -- as I explained in my own response
about 9 hours ago. In any case, section 6 of the comp.lang.c FAQ
is a better source of information than either of our followups.

True. That somebody spent more than 30 seconds spitting out an
answer.
 
R

Richard

CBFalconer said:
Keith said:
CBFalconer said:
Chad wrote:
[...]

Why can't I just increment cmds in this case?

Because there is no such thing.

There is no such thing as what? There certainly is such a thing
as cmds; it's just not something you can increment.
Try the following for the for:

for (p = cmds; *p; p++) printme(p);

You may need to declare a "char* p;" earlier.

No, you need a "char **p;" -- as I explained in my own response
about 9 hours ago. In any case, section 6 of the comp.lang.c FAQ
is a better source of information than either of our followups.

True. That somebody spent more than 30 seconds spitting out an
answer.

correct answer.
 
Z

Zach

CBFalconer said:
Chad wrote: [...]
char *cmds[] = {
"This",
"is",
"a line",
NULL
};
for ( ;*cmds != NULL; cmds++ ) {
printme( *cmds);
} [...]
Why can't I just increment cmds in this case?
Because there is no such thing.

There is no such thing as what? There certainly is such a thing as
cmds; it's just not something you can increment.
Try the following for the for:
for (p = cmds; *p; p++) printme(p);
You may need to declare a "char* p;" earlier.

No, you need a "char **p;" -- as I explained in my own response about
9 hours ago. In any case, section 6 of the comp.lang.c FAQ is a
better source of information than either of our followups.

Why "char **p;" and not 'char *p;' ?

Zach
 
K

Keith Thompson

Zach said:
CBFalconer said:
Chad wrote: [...]
char *cmds[] = {
"This",
"is",
"a line",
NULL
};
for ( ;*cmds != NULL; cmds++ ) {
printme( *cmds);
} [...]
Why can't I just increment cmds in this case?
Because there is no such thing.

There is no such thing as what? There certainly is such a thing as
cmds; it's just not something you can increment.
Try the following for the for:
for (p = cmds; *p; p++) printme(p);
You may need to declare a "char* p;" earlier.

No, you need a "char **p;" -- as I explained in my own response about
9 hours ago. In any case, section 6 of the comp.lang.c FAQ is a
better source of information than either of our followups.

Why "char **p;" and not 'char *p;' ?

cmds is an array of pointers to char. p needs to step through that
array, pointing to each element in turn. So p needs to be a pointer
to pointer to char. (And you need to pass *p as the argument to
printme().)
 
K

Kaz Kylheku

Putting you at the level of Han!

You're assuming all seconds are the same. According to special relativity. One
Chucky hour == 1 Han second. That may be a general relativistic effect caused
by the proximity of an immense mass, from which not even intelligence can
escape.
 
K

Kaz Kylheku

Please, not that. Accuse me of anything else.

You can prove you're at or above the level of Han by going back and finding one
single C thing that he got wrong. So far the score is:

Chucky: 0 Han: 379

Or something like that. Who could be bothered to count?

And I mean, some original finding! Not like your usual copy-cat job, lifted
from someone who posted hours before you.

Whoever Han is, he was here years before you (he claims, and this claim is
quite credible), and knows his stuff (which is why it's credible).
 
R

Richard

Kaz Kylheku said:
You can prove you're at or above the level of Han by going back and finding one
single C thing that he got wrong. So far the score is:

Chucky: 0 Han: 379

Or something like that. Who could be bothered to count?

And I mean, some original finding! Not like your usual copy-cat job, lifted
from someone who posted hours before you.

Whoever Han is, he was here years before you (he claims, and this claim is
quite credible), and knows his stuff (which is why it's credible).

I remember our Chuck coming huffing and puffing back into the group
after a short absence and apologising to another c.l.c "reg" that he was
a little brusque in one of this replies because he had a mountain of
c.l.c posts to reply to and put right and didn't have the time for
niceties. Talk about a legend in his own lunchtime. How I laughed.
 
K

Keith Thompson

Kaz Kylheku said:
Whoever Han is, he was here years before you (he claims, and this claim is
quite credible), and knows his stuff (which is why it's credible).

And he has chosen to throw away whatever credibility he might have by
being a troll.
 
K

Kenny McCormack

And he has achieved that status by becoming what we "regs" call a "troll".
We hate criticism and do what we can to character assasinate anyone who
questions us or our principles.

HTH. HAND.
 
I

Ike Naar

You can prove you're at or above the level of Han by going back and finding one
single C thing that he got wrong. So far the score is:

Chucky: 0 Han: 379

A freudean slip?
 
P

Phil Carmody

Richard Heathfield said:
Kaz Kylheku said:



Check the archives. He's a relative newbie to clc.


What have you done with the real Kaz Kylheku?

Agree. It's not parody, as it's nothing like the original.

Phil
 
C

CBFalconer

Richard said:
CBFalconer said:

How about hypocrisy? You keep telling other people not to respond
to trolls, yet here you are, responding to a troll.

I haven't yet put her in that class. Probably soon.
 
R

Richard Bos

Phil Carmody said:
Agree. It's not parody, as it's nothing like the original.

It's closer to the old Kaz than the (relatively) new Heathfield is to
the old one, _and_ it knows its stuff. I say it's the original Kaz; and
if not, it's close enough to the old Kaz for the new comp.lang.c.

Richard
 
K

Kaz Kylheku

It's closer to the old Kaz than the (relatively) new Heathfield is to
the old one, _and_ it knows its stuff. I say it's the original Kaz; and
if not, it's close enough to the old Kaz for the new comp.lang.c.

Thanks. I made some PGP key long long ago. I rescued it some years ago
and converted it to a new format, but the machine where that is stored
is collecting dust somewhere. Don't make me go looking.

Some ten more years in the industry changes people.

I've had lots more experience promoting standard-conforming practices to smart
people without bashing them over the head.

Remember that Emily Dickinson poem?

Tell all the Truth but tell it slant---
Success in Cirrcuit lies
Too bright for our infirm Delight
The Truth's superb surprise
As Lightening to the Children eased
With explanation kind
The Truth must dazzle gradually
Or every man be blind---

Is it fashionable to invoke that in a situation such as this? I don't want to
be shallow.

Man, she sure was into insulting blind people, though, holy cow! I'd be
surprised if it's not banned in schools now.
 
J

J. J. Farrell

Kaz said:
Thanks. I made some PGP key long long ago. I rescued it some years ago
and converted it to a new format, but the machine where that is stored
is collecting dust somewhere. Don't make me go looking.

Some ten more years in the industry changes people.

I've had lots more experience promoting standard-conforming practices to smart
people without bashing them over the head.

Remember that Emily Dickinson poem?

Tell all the Truth but tell it slant---
Success in Cirrcuit lies
Too bright for our infirm Delight
The Truth's superb surprise
As Lightening to the Children eased
With explanation kind
The Truth must dazzle gradually
Or every man be blind---

Is it fashionable to invoke that in a situation such as this? I don't want to
be shallow.

Man, she sure was into insulting blind people, though, holy cow! I'd be
surprised if it's not banned in schools now.

Ye gods, he's after the Hindus now as well.
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top