473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

I think this is a problem in gcc.

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?
Jun 2 '08 #1
8 2149
Aereshaa <Ae******@gmail.comwrites:
>For some reason, when I compile this code:
>int main(){
char* a =3D malloc(5);
long* l =3D (*long) a;
} //I shortened it to isolate the problem.
>I get this error:
>error.c:3: error: expected expression before =91long=92
>But there IS an expression before 'long'. Why is this?
(long *) instead of (* long) ?

--
Chris.
Jun 2 '08 #2
On Jun 2, 11:38*am, Aereshaa <Aeres...@gmail.comwrote:
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
Jun 2 '08 #3
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 =3D malloc(5);
long* l =3D (*long) a;
} //I shortened it to isolate the problem.
I get this error:
error.c:3: error: expected expression before =91long=92
But there IS an expression before 'long'. Why is this?

(long *) instead of (* long) ?

--
Chris.
Oh, #it! I was under the impression that it referred to the first
'long' in the line.
Thanks.
Jun 2 '08 #4
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 =3D malloc(5);
long* l =3D (*long) a;
>} //I shortened it to isolate the problem.
>I get this error:
>error.c:3: error: expected expression before =91long=92
>But there IS an expression before 'long'. Why is this?
(long *) instead of (* long) ?
--
Chris.

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

Jun 2 '08 #5
On Sun, 1 Jun 2008 20:48:08 -0700 (PDT), Aereshaa <Ae******@gmail.com>
wrote:
>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 =3D malloc(5);
long* l =3D (*long) a;
} //I shortened it to isolate the problem.
I get this error:
error.c:3: error: expected expression before =91long=92
But there IS an expression before 'long'. Why is this?

(long *) instead of (* long) ?

--
Chris.

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
Jun 2 '08 #6
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"
Jun 2 '08 #7
"kongwe...@gmail.com" <kongwe...@gmail.comwrote:
Aereshaa wrote:
error.c:3: error: expected expression before ‘long’

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 *).

--
Peter
Jun 2 '08 #8
Aereshaa wrote:
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.

--
Ian Collins.
Jun 2 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Matthew Thorley | last post by:
Greetings, Maybe someone out there can lend me an eye? I've been stumped, banging my head against the wall trying to figure out why my script doesn't work. I've tried every thing I could think of,...
86
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that:...
3
by: Angel | last post by:
Hello again (and again, and again...) I think I'm getting closer to solving my initial problem of calling unmanaged code. I managed to call the functions with user-defined structs w/o getting any...
40
by: Neo The One | last post by:
I think C# is forcing us to write more code by enforcing a rule that can be summarized as 'A local variable must be assgined *explicitly* before reading its value.' If you are interested in what...
9
by: Peter Oliphant | last post by:
For some reson my code is generating a LNK1215 error, which 'suggests' I re-install VS C++. So I did. which did NOT solve the problem. The weid part is it seems to be caused by my one CPP file, but...
3
by: lsumnler | last post by:
I have started to work through Teach Yourself ASP.Net in 24 hours. The problem that I have noticed is - When I place a textbox Web Control on the screen and then try to change the "ID" in the...
3
by: YYZ | last post by:
I'm new to VB.net, but not new to OO in general, and I'm very strong in VB6. However, I don't know what I did in VB.net to cause this to happen. I've got 2 projects open at the same time, in the...
5
by: Chad | last post by:
I want to create a class which contains a date, integer and string property which all allow a NULL value. I don't want to store the values in an Object datatype. I prefer more strongly typed...
29
by: GhostInAK | last post by:
I'm seeing a terribly distubing number of questions that have no purpose in existing. As an example: How do I change the position of a stream? Hmm.. Could it be some method on the stream...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.