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

Home Posts Topics Members FAQ

expected expression before ‘)’ token

1 New Member
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include "memory.h"
  3.  
  4. #define POOL_SIZE 10
  5.  
  6. // pool of memory
  7. struct record pool[POOL_SIZE]; 
  8. struct record * top=pool;  // pool is constant; a pointer to the stack top.
  9.  
  10. void init_pool() // Initialize the pool
  11. {
  12.   int i;
  13.   struct record *r=pool;
  14.   struct record *s;
  15.  
  16.   pool[POOL_SIZE-1].next=NULL;  
  17.  
  18.   for(i=1;i<POOL_SIZE;i++) { 
  19.     s=r++;
  20.     s->next=r;
  21.   }
  22. }
  23.  
  24.  
  25. // Gets a node from the pool. Returns NULL if pool is empty. (A BETTER DESIGN)
  26. struct record * new_node()  
  27. {
  28.   struct record *r=pool;
  29.   pool=(pool *)malloc(sizeof(pool[POOL_SIZE]));
  30. }
  31.  
  32.  
  33. // Push a node to the pool.
  34. void free_node(struct record *r)
  35. {
  36.   r->next=top;
  37.   top=r;
  38. }
let's not talk about backends.(i already tested it)
when i try to compile this, i meet up with those errors.


memory.c: In function ‘new_node’:
memory.c:29:15: error: expected expression before ‘)’ token
pool=(pool *)malloc(sizeof(pool[POOL_SIZE]));
^
how could i fix this error?
Oct 2 '20 #1
3 10872
dev7060
639 Recognized Expert Contributor
memory.c: In function ‘new_node’:
memory.c:29:15: error: expected expression before ‘)’ token
pool=(pool *)malloc(sizeof(pool[POOL_SIZE]));
^
how could i fix this error?
Expand|Select|Wrap|Line Numbers
  1. pool=(struct record *)malloc(sizeof(pool[POOL_SIZE]));
Oct 2 '20 #2
Banfa
9,065 Recognized Expert Moderator Expert
Just to put into words the correction dev7060 has given in the cast on line 29 you had a variable name but when you cast you need to use a type name.
Oct 2 '20 #3
donbock
2,426 Recognized Expert Top Contributor
Line 7 defines pool as an array of struct record.
Line 29 seeks to change pool to point at a newly allocated block of memory. You can’t do this because pool is not a variable (the various array elements are variables, but not pool). Formally, pool is an rvalue; but you can only assign new values to lvalues.

Line 29 allocates a block of memory that is the same size as the POOL_SIZEth element of the pool array. However there isn’t any such element. You’re allowed to reference one element past the end of an array but it isn’t meaningful. All array elements are the same size so you may as well refer to the 0th entry.

If function new_node dynamically allocates a new node then why do you define a static array on line 7?
Nov 24 '20 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: sandy | last post by:
I am a student who is losing his mind. The code below is a header file which has the line: void ClearList(ListType *list); which generates the following compile time errors: variable or...
21
by: Ram Prasad | last post by:
I am trying to write a simple libspf2 plugin code for my postfix ( milter) I am getting this unhelpful error message when I try to compile gcc -g1 -Wall -I/usr/local/include/spf2 -I. -c mfunc.c...
1
by: nagaraj | last post by:
In Dev C++, we tried to compile a application, but it will throw some peculiar error. please find it below "path/main.cpp expected `]' before ';' token" and the source code is "DWORD rtec_Stack...
2
by: lordhoban | last post by:
My program was running fine under visual studio, but I moved it over to Linux, and have run into one problem I have no idea how to fix. error: expected expression before 'int' (all the...
10
by: JEFFREY MUSGRAY | last post by:
I have received alot of help from this site and I am grateful. My problem is that I was able to get the first part of code to work. I tested it via the immediate window. I received the compile error...
9
by: Fredrik Sveide | last post by:
hi im trying to make an simple app and i where almost done but then this error apear and i just cant figure out how to fix it ! Please help me im desperate?
4
by: KBeggs | last post by:
Hi. I'm working on a program for a class that requires me to make a checking account simulator. However, every time I try to compile it, I get errors. I have gotten it down to very minimal errors,...
1
by: Jeevan83 | last post by:
Hi, I gott his error after compiling. I could not find the error, whats the error please? error: error: expected expression before ‘{’ token item={"rice",10,40,30}; #include<stdio.h>...
3
by: adiupadhyay03 | last post by:
struct Person { const char *personName; int personAge; struct Person* next; }P; int comp_person_name(const void *a, const void *b){ int result = strcmp(((P *)a)->personName,((P...
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
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
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 ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.