473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2150
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.