Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » learning c, example code gives error? [solved](The following is my version of example code from the book (from the bookshelf) "Learning to code with c" which I am currently working through (and enjoying).)
learning c, example code gives error? [solved] [message #1839713] Fri, 26 March 2021 10:48 Go to next message
ahmed rabea is currently offline ahmed rabeaFriend
Messages: 3
Registered: March 2021
Junior Member
#include <stdio.h>

void main (void)
{
int intval = 255958283;
void *vptr = &intval;

printf ("The value at vptr as an int is %d\n", *((int *) vptr));
printf ("The value at vptr as a char is %d\n", *((char *) vptr));
}

It compiles with:

gcc -o void_pointer_2_out void_pointer_2.c

but gives error:

./void_pointer_2.c: line 3: syntax error near unexpected token `('
./void_pointer_2.c: line 3: `void main (void)'

I have also cut and pasted the code directly from the PDF and get the same error.

What is causing the error?
Re: learning c, example code gives error? [solved] [message #1839754 is a reply to message #1839713] Sat, 27 March 2021 10:22 Go to previous message
Tauno Voipio is currently offline Tauno VoipioFriend
Messages: 742
Registered: August 2014
Senior Member
This is off-topic for Eclipse, it is just plain C programming question.

There must be a non-printing character on the complaint line or before it.
When I copied your example from the message, it gave:

--- clip clip ---

macbook2t:Desktop tauno$ gcc -o void void.c
void.c:3:1: warning: return type of 'main' is not 'int' [-Wmain-return-type]
void main (void)
^
void.c:3:1: note: change return type to 'int'
void main (void)
^~~~
int
1 warning generated.

-- run example --

macbook2t:Desktop tauno$ ./void
The value at vptr as an int is 255958283
The value at vptr as a char is 11
macbook2t:Desktop tauno$

--- clip clip ---

The complaint of the type of main() is correct, the proper form is:

int main(int arcg, char *argv[])

You also need to provide the return value with a return statement. The value macros for the return are in stdlib.h.

Another thing is that you're testing unhealthy pointer usage. That kind of coding is prone to produce hard-to-find bugs.



--

Tauno Voipio
Previous Topic:MinGW paths from Qt, import CMake project on windows
Next Topic:pigpiod says that Pi400 does not appear to be a raspberry pi
Goto Forum:
  


Current Time: Fri Mar 29 14:49:58 GMT 2024

Powered by FUDForum. Page generated in 0.02062 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top