M
Mark
Hello
drilling the pointers in K&R 2nd edition. Dazzled and confused with pointers
and arrays of pointers. Trying to figure out why this code doesn't work
properly (want to print out the elements of the array):
#include <stdio.h>
static const char *lineptr[] = {"abc", "d", "ef", "jh", "xyz"};
int main(void)
{
while (*lineptr) {
printf("string '%s'\n", *lineptr++); /*XXX*/
}
return 0;
}
Compiler is not happy with XXX line: invalid lvalue in increment
My undestanding is that 'lineptr' is an array of pointers, 'lineptr' itself
is pointing on the first entry, so why can't we just move along the array
just increasing this pointer one by one? Seems like ANSI C doesn't agree
with me
But changing code to this makes it compiled and run:
#include <stdio.h>
static const char *lineptr[] = {"abc", "d", "ef", "jh", "xyz"};
int main(void)
{
const char **tmp = lineptr;
while (*tmp) {
printf("string '%s'\n", *tmp++);
}
return 0;
}
I have gut feeling this latter snippet is wrong either. Could you help to
clearly understand what's going on and where I'm wrong.
-- Mark
drilling the pointers in K&R 2nd edition. Dazzled and confused with pointers
and arrays of pointers. Trying to figure out why this code doesn't work
properly (want to print out the elements of the array):
#include <stdio.h>
static const char *lineptr[] = {"abc", "d", "ef", "jh", "xyz"};
int main(void)
{
while (*lineptr) {
printf("string '%s'\n", *lineptr++); /*XXX*/
}
return 0;
}
Compiler is not happy with XXX line: invalid lvalue in increment
My undestanding is that 'lineptr' is an array of pointers, 'lineptr' itself
is pointing on the first entry, so why can't we just move along the array
just increasing this pointer one by one? Seems like ANSI C doesn't agree
with me
But changing code to this makes it compiled and run:
#include <stdio.h>
static const char *lineptr[] = {"abc", "d", "ef", "jh", "xyz"};
int main(void)
{
const char **tmp = lineptr;
while (*tmp) {
printf("string '%s'\n", *tmp++);
}
return 0;
}
I have gut feeling this latter snippet is wrong either. Could you help to
clearly understand what's going on and where I'm wrong.
-- Mark