Keith Thompson said:
Osmium said:
Bill Cunningham said:
This code looks correct to me. But the compiler issued this warning.
What's it trying to tell me?
#include <stdio.h>
int main()
{
int *p;
int a[5] = { 0 };
p = &a;
printf("%d\n", *(p + 1));
printf("%d\n", *(p + 3));
}
p.c: In function 'main':
p.c:7:7: warning: assignment from incompatible pointer type [enabled by
default]
The compiler has set up a location and agreed to let you call that
location
'a'. They are telling you that a does not contain an int. Consider: If
it
did contain an int, where would they put a[1]? How would they find a[1]?
Right so far.
In fact, a contains effectively a *pointer* to an int, not an int.
That's
what they are telling you. p is of type pointer to int and a is not an
int.
They want you to fix it.
Was that a typo? p is a pointer to an int. a is an array.
No, I meant what I said. I am not particularly interested in what the
standard says and, in fact, I'm appalled that so many people (10 or 20)
think a language standard can be so interesting. I look at the end result
of all these shenanigans.
In my view, C does not *have* arrays. They go through some sophistry to
make it look like they have arrays, and they use the word "array", but what
they actually have is sophistry. An array has, for example, a *size*. The
thing C has is without bounds. That's not an array. Should you not be able
to set one array to the value of another with the same characteristics?
That is b = a? No, you can't do that in C.
If a was an array Bill should be able to tell an independently compiled
function that a was an array. There is no such way. Such a function can be
satisfied by a parameter that is an ordinary, *indistinguishable* pointer.
What happened to the array? The gurus say it transmogrified or some such.
I say it never existed in the first place. it was all imaginary.
When C sees
int a[10];
it goes into a little dance. It sets aside room for 10 ints in one place
and in another place it creates a variable named 'a'. They set a to point
at the first int in the space set aside. Bill's problem originates with the
variable named 'a'. Thus my answer to him.
Troll score:
[0/10]
Bills confusion is simply the result you get when you misuse words that
already had a well established meaning.