expected expression before ‘)’ token

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sephiehush
    New Member
    • Oct 2020
    • 1

    expected expression before ‘)’ token

    Code:
    #include <stdio.h>
    #include "memory.h"
    
    #define POOL_SIZE 10
    
    // pool of memory
    struct record pool[POOL_SIZE]; 
    struct record * top=pool;  // pool is constant; a pointer to the stack top.
    
    void init_pool() // Initialize the pool
    {
      int i;
      struct record *r=pool;
      struct record *s;
    
      pool[POOL_SIZE-1].next=NULL;  
    
      for(i=1;i<POOL_SIZE;i++) { 
        s=r++;
        s->next=r;
      }
    }
    
    
    // Gets a node from the pool. Returns NULL if pool is empty. (A BETTER DESIGN)
    struct record * new_node()  
    {
      struct record *r=pool;
      pool=(pool *)malloc(sizeof(pool[POOL_SIZE]));
    }
    
    
    // Push a node to the pool.
    void free_node(struct record *r)
    {
      r->next=top;
      top=r;
    }
    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?
    Last edited by Banfa; Oct 2 '20, 11:52 AM. Reason: Added code tags
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    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?
    Code:
    pool=(struct record *)malloc(sizeof(pool[POOL_SIZE]));

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      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.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        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?
        Last edited by donbock; Nov 24 '20, 05:31 AM. Reason: Added rvalue / lvalue sentence.

        Comment

        Working...