I think this is a problem in gcc.

A

Aereshaa

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?
 
K

kongweize

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 think you made mistake in
long* l = (*long) a;

the * in (*long) is recognized as a multiplication sign
 
V

vippstar

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
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;
}
 
B

Barry Schwarz

Oh, #it! I was under the impression that it referred to the first
'long' in the line.
Thanks.

After fixing the cast as suggested, be aware that if sizeof(long)
exceeds 5 than any attempt to dereference l yields undefined behavor.
The same is true for a if sizeof(int) exceeds 5 but that is less
likely.


Remove del for email
 
K

Keith Thompson

(I fixed some quoted-printable noise in the above.)
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.
 
P

Peter Nilsson

I think you made mistake in
long* l = (*long) a;

the * in (*long) is recognized as a multiplication sign

Not that it matters, but I think it's much more likely
it's recognised as the indirection operator (unary *).
 
I

Ian Collins

Aereshaa said:
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?

Ignoring the cast problem, this would be more than a little dangerous on
any IPL64 system.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top