G
Grumble
I've come across code that has me confused. Here it is, stripped down:
#include <stdlib.h>
#include <stdio.h>
#define N 20
int main(void)
{
int (*p)[];
int *q;
int i;
p = malloc(N*sizeof(int));
q = malloc(N*sizeof(int));
for (i=0; i < N; ++i)
{
(*p) = i+666; q = i+666;
}
for (i=0; i < N; ++i)
{
printf("%d == %d\n", ((int *)p), q);
}
return 0;
}
$ gcc-3.4.3 -Wall -ansi -pedantic -O1 testcase.c
I don't understand why anyone would define 'p' that way? Do they want to
make it clear that 'p' will point to an array (not just one element)
sometime in the future? I'd just use 'q'.
It seems strange to have to write (*p) every time I want to access
the i-th element. Moreover, it seems even stranger that (*p) is
equivalent to ((int *)p).
I'd appreciate your comments on this weird (to me) syntax.
#include <stdlib.h>
#include <stdio.h>
#define N 20
int main(void)
{
int (*p)[];
int *q;
int i;
p = malloc(N*sizeof(int));
q = malloc(N*sizeof(int));
for (i=0; i < N; ++i)
{
(*p) = i+666; q = i+666;
}
for (i=0; i < N; ++i)
{
printf("%d == %d\n", ((int *)p), q);
}
return 0;
}
$ gcc-3.4.3 -Wall -ansi -pedantic -O1 testcase.c
I don't understand why anyone would define 'p' that way? Do they want to
make it clear that 'p' will point to an array (not just one element)
sometime in the future? I'd just use 'q'.
It seems strange to have to write (*p) every time I want to access
the i-th element. Moreover, it seems even stranger that (*p) is
equivalent to ((int *)p).
I'd appreciate your comments on this weird (to me) syntax.