vi******@gmail.com writes:
On Jun 2, 6:48 am, Aereshaa <Aeres...@gmail.comwrote:
>On Jun 1, 11:42 pm, Chris McDonald <ch...@csse.uwa.edu.auwrote:
Aereshaa <Aeres...@gmail.comwrites:
For some reason, when I compile this code:
int main(){
char* a = malloc(5);
long* l = (*long) a;
} //I shortened it to isolate the problem.
I get this error:
error.c:3: error: expected expression before 'long'
But there IS an expression before 'long'. Why is this?
(I fixed some quoted-printable noise in the above.)
(long *) instead of (* long) ?
Oh, #it! I was under the impression that it referred to the first
'long' in the line.
Thanks.
What you probably want:
l = *(long *)a
I doubt it, unless he's going to change the code substantially.
``l'' is of type long*, not long.
However sizeof (long) is not guaranteed to be less or equal to 5, and
the contents of a are not initialized.
Try this instead:
char *a = malloc(sizeof (long)):
long l;
if(a) {
memset(a, 0, sizeof(long));
l = *(long*)a;
}
That's a rather roundabout way of writing ``long l = 0;'', which
doesn't particularly resemble what the OP was trying to do.
But the OP did say that the posted code was shortened to isolate the
problem. It's not surprising that a piece of code intended only to
exhibit a syntax error wouldn't make much sense logically.
If the OP really is trying to assign the result of malloc(5) to a
long*, I hope he(?) will post again and learn why it's a bad idea.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"