-3

I 'm trying to implement a linked list in C , and I am a total noob at C, but good at c++. I 'm getting this syntax error when using malloc(), no idea why. I get error C2059:syntax error :')'. Line 169.

the line is

entry_p->next_p = (entry_p *)malloc(sizeof(node));

which is in listInsert. Any help would be appreciated.

EDIT: Error is fixed, replaced with

struct listEntry *entry_p = list_p ->head_p;

but now im getting program breakage, happening on this line

while(entry_p->next_p!=NULL)

any ideas why? Trying to figure out it in debugger but no luck so far.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define SUCCESS 0
#define FAIL    1

char *phonetic[] = { "alpha", "bravo", "charlie", "delta", "echo", "foxtrot",
                     "golf", "hotel", "india", "juliet", "kilo", "lima", "mike",
                     "november", "oscar", "papa", "quebec", "romeo", "sierra",
                     "tango", "uniform", "victor", "whisky", "xray", "yankee", 
                     "zulu" };

unsigned char indexes[] = { 1, 14, 17, 3, 22, 0, 5, 18, 24, 11, 4, 6, 13, 21,
                            2, 12, 25, 19, 10, 16, 7, 9, 23, 15, 20, 8 };                       

// represents an entry in the linked-list
typedef struct listEntry
{
  char *data_p;               // pointer to the entry's string
  struct listEntry *prev_p;   // pointer to previous entry in the linked-list  
  struct listEntry *next_p;   // pointer to next entry in the linked-list
};

// represents the linked-list
 typedef struct list
{
  int entryCount;             // number of entries present in the linked-list
  struct listEntry *head_p;   // pointer to the first entry in the list  
  struct listEntry *tail_p;   // pointer to the last entry in the list
};

// Dynamically allocate & initialise an empty linked list
int listCreate(struct list** list_p2)
{
  // allocate struct list from heap 
  *list_p2 = (struct list*) malloc(sizeof(**list_p2));

  if (*list_p2 != NULL)
  {
    // zero-initialize the list structure 
    memset(*list_p2, 0, sizeof(**list_p2));
    return SUCCESS;    
  }

  return FAIL;
}

// Free all entries in the linked-list and the list structure
int listDestroy(struct list *list_p)
{
  if (list_p != NULL)
  {
    struct listEntry *entry_p = list_p->head_p;

    while (entry_p != NULL)
    {
      struct listEntry *next_p = entry_p->next_p;
      // free the current entry
      free(entry_p);
      // move to the next entry
      entry_p = next_p;
    }

    // free list structure
    free(list_p);
  }

  return FAIL;
}

// Traverse the linked-list from head to tail printing out
// the string data from each list entry
int listPrintForward(struct list *list_p)
{ 
  if (list_p)
  {    
    struct listEntry *entry_p = list_p->head_p;
    int count = 0;

    printf("FORWARD: %d entries\n", list_p->entryCount);
    while (entry_p != NULL)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", entry_p->data_p);
      }
      else
      {      
        printf("%s ", entry_p->data_p);
      }

      if (entry_p == list_p->tail_p)
        printf("\n");

      entry_p = entry_p->next_p;
      fflush(stdout);
      count++;         
    }

    return SUCCESS;
  }

  return FAIL;
}

// Traverse the linked-list from tail to head printing out
// the string data from each list entry
int listPrintReverse(struct list *list_p)
{ 
  if (list_p)
  {    
    struct listEntry *entry_p = list_p->tail_p;
    int count = 0;

    printf("REVERSE: %d entries\n", list_p->entryCount);   
    while (entry_p != NULL)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", entry_p->data_p);
      }
      else
      {      
        printf("%s ", entry_p->data_p);
      }

      if (entry_p == list_p->head_p)
        printf("\n");

      entry_p = entry_p->prev_p;
      fflush(stdout);
      count++;         
    }

    return SUCCESS;
  }

  return FAIL;
}

// Insert the given string into the linked-list such that the
// entries in the linked-list are in alphabetical order
void listInsert(struct list *list_p, char *string_p)
{ 
  // Please write the listInsert function
    struct listEntry *entry_p = list_p ->head_p;
    /* Iterate through the list till we encounter the last node.*/
    while(entry_p->next_p!=NULL)
    {
        entry_p = entry_p ->next_p;
    }
    /* Allocate memory for the new node and put data in it.*/
        entry_p->next_p = (entry_p *)malloc(sizeof(node));
        entry_p = entry_p->next_p;
        entry_p->data_p = string_p;
        entry_p->next_p = NULL;



 // return FAIL;  
}

int main(int argc, char **argv)
{
  struct list *list_p = NULL;
  (void) argc;
  (void) argv;

  if (listCreate(&list_p) == SUCCESS)
  {
    unsigned int count;

    // insert every word in the phonetic alphabet into the
    // linked-list.
    printf("INSERT:\n");
    for (count = 0; count < sizeof(indexes); count++)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", phonetic[indexes[count]]);
      }
      else
      {
        printf("%s ", phonetic[indexes[count]]);
      }
      listInsert(list_p, phonetic[indexes[count]]);
    }
    printf("\n");

    // print out the list in alphabetical order
    listPrintForward(list_p);
    // print out the list in reverse alphabetical order
    listPrintReverse(list_p); 

    // Destroy the linked list and free all associated memory
    listDestroy(list_p);               
  }

  return SUCCESS;
} 

Edit 2: Compiler error fixed.

this is the final state of listInsert.

void listInsert(struct list *list_p, char *string_p)
{ 
  // Please write the listInsert function
    //list_p->head_p= NULL;
    struct listEntry *entry_p = list_p ->head_p;
    if (list_p->head_p == NULL)
    {
        struct listEntry *newnode = list_p->head_p;
        list_p->head_p = newnode;

    }
    else
    {

        /* Iterate through the list till we encounter the last node.*/
        while(entry_p->next_p!=NULL)
        {
            entry_p = entry_p ->next_p;
        }
        /* Allocate memory for the new node and put data in it.*/
            entry_p->next_p = (struct listEntry *)malloc(sizeof(struct listEntry));
            entry_p = entry_p->next_p;
            entry_p->data_p = string_p;
            entry_p->next_p = NULL;
    }



 // return FAIL;  
}
Haris
  • 12,120
  • 6
  • 43
  • 70

3 Answers3

0

you have node undefined, you need to rename it to listEntry

entry_p->next_p = (struct listEntry *)malloc(sizeof(struct listEntry));

for the runt-time error

while(entry_p->next_p!=NULL)

this is because entry_p is null when the list is initialized (list is empty and trying to insert the first item). so you need to handle this special case. outlined here:

// Insert the given string into the linked-list such that the
// entries in the linked-list are in alphabetical order
void listInsert(struct list *list_p, char *string_p)
{ 
  // Please write the listInsert function
    struct listEntry *entry_p = list_p ->head_p;

    if(entry_p == null)
    { 
        // create a node and link it to head
        // also fill it with data.
    }
    else 
    {
        /* Iterate through the list till we encounter the last node.*/
        while(entry_p->next_p!=NULL)
        {
            entry_p = entry_p ->next_p;
        }
        /* Allocate memory for the new node and put data in it.*/
        entry_p->next_p = (entry_p *)malloc(sizeof(node));
        entry_p = entry_p->next_p;
        entry_p->data_p = string_p;
        entry_p->next_p = NULL;
    }

 // return FAIL;  
}
mmohab
  • 2,303
  • 4
  • 27
  • 43
  • I do that and i get error C2065: 'listEntry' : undeclared identifier on the same line. How to fix? – user3677487 May 26 '14 at 22:16
  • sorry thanks fixed. now i get breakage on line "while(entry_p->next_p!=NULL)", any ideas why? – user3677487 May 26 '14 at 22:33
  • you need to handle inserting the first item, i outlined some code but you need to complete it – mmohab May 26 '14 at 23:01
  • Ok great. It compiles now. I filled the -if-statement you made with struct listEntry *newnode = list_p->head_p; list_p->head_p = newnode;, and changed (entry_p == null) to (list_p->head_p == NULL). Now it puts it into the linked list, do you know how to sort the items in the list alphabetically? – user3677487 May 26 '14 at 23:28
  • OK first accept the answer if this helps you. For sorting the list, you can do some search on that. this also might help http://stackoverflow.com/questions/17815939/sorting-element-from-linked-list – mmohab May 27 '14 at 18:52
0

The common mistake is not to inconsistency between what is in your head and what you are writing in your code.

Probably you had listEntry as node of your linked list in your head, and in your pace you used that terminology.That is the reason you failed to find the simple error,even after repeated checks.

The correct line is

entry_p->next_p = (struct listEntry  *)malloc(sizeof(struct listEntry));

Common mistake for programmers who transition from C++ to C is to forget struct keyword before struct elements.In C++ it is optional but in C, this is mandatory.

anshuman
  • 98
  • 7
  • but there is a typedef struct listEntry, so is struct listEntry still necessary? or listEntry is enough? – mmohab May 26 '14 at 22:29
  • 1
    @mmohab In the above code listEntry is not enough because typedef has not been used to its full advantage.The definition is unnamed here.If typedef is ended with listEntry then struct will not be needed – anshuman May 26 '14 at 22:34
0

You have typedef struct listEntry { ..... }; . This is a syntax error.

The typedef keyword does not mean to define a new type (despite its name). It means to create an alias for an existing type. The keyword to define a new type is struct.

When you write struct listEntry { ..... };, it defines a new type called struct listEntry. If you then write:

typedef struct listEntry Blarg;

it means that Blarg is an alias for struct listEntry. Sometimes people combine the creation of the struct type and its typedef in one line:

typedef struct listEntry { ...... } Blarg; 

This is just the same effect as having two separate lines as I covered above, but it's a bit less typing.

If your compiler accepts your code, then who knows what it is doing; possibly it just ignores the typedef keyword.


Moving onto malloc, you allocate the wrong amount of space, and have bogus casts. You are not supposed to cast the value returned by malloc. Instead, you specify the correct number of bytes.

If you point to something of type T then you want to allocate enough bytes for a certain number of T's. You can do this by writing:

ptr = malloc( sizeof *ptr );

which allocates enough space for 1 of whatever ptr points to.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365