#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    An Error in Linux enviorenment, but this is correctly runs in codepad.org


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

    int * list2array(void * list);

    struct list_el {
    int val;
    struct list_el * next;
    };

    typedef struct list_el item;

    void main() {
    item * curr, * head;

    int i;
    int n[10];

    head = NULL;

    for(i=2;i<=10;i++) {
    curr = (item *)malloc(sizeof(item));
    curr->val = i;
    n[i]=curr->val ;
    curr->next = head;
    head = curr;
    }

    curr = head;

    while(curr) {

    for(i=2;i<=10;i++)
    {
    n[i]=curr->val;
    printf("%d\n", n[i]);
    curr = curr->next ;
    }

    }
    }


    //error i got in linux

    Code:
    array.c:12: error: expected ‘(’ before ‘struct’
    array.c: In function ‘main’:
    array.c:16: error: ‘item’ undeclared (first use in this function)
    array.c:16: error: (Each undeclared identifier is reported only once
    array.c:16: error: for each function it appears in.)
    array.c:16: error: ‘curr’ undeclared (first use in this function)
    array.c:16: error: ‘head’ undeclared (first use in this function)
    array.c:24: error: expected expression before ‘)’ token
    array.c:27: error: ‘val’ undeclared (first use in this function)
    
    How to correct this pls help me
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    In the following snippet:
    Code:
    int * list2array(void * list);
    Where has list been defined?

    Where is list2array() implemented?

    Jim
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Again Error .............


    Originally Posted by jimblumberg
    In the following snippet:
    Code:
    int * list2array(void * list);
    Where has list been defined?

    Where is list2array() implemented?

    Jim

    I remove that line but same error is coming
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,363
    Rep Power
    1870
    Are you compiling the right file?
    Code:
    $ cat foo.c
    #include<stdlib.h>
    #include<stdio.h>
    
    int *list2array(void *list);
    
    struct list_el {
      int val;
      struct list_el *next;
    };
    
    typedef struct list_el item;
    
    void main()
    {
      item *curr, *head;
      int i;
      int n[10];
    
      head = NULL;
      for (i = 2; i <= 10; i++) {
        curr = (item *) malloc(sizeof(item));
        curr->val = i;
        n[i] = curr->val;
        curr->next = head;
        head = curr;
      }
    
      curr = head;
      while (curr) {
        for (i = 2; i <= 10; i++) {
          n[i] = curr->val;
          printf("%d\n", n[i]);
          curr = curr->next;
        }
      }
    }
    $ gcc -Wall -Wextra foo.c
    foo.c:13:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    That's only because you used void main and not int main.
    Nothing at all about item being undeclared.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,073
    Rep Power
    1802
    The code you have posted is not the code for which the compiler diagnostics were issued. For starter's in your code line 12 is a blank line; besides that the code compiles fine.

IMN logo majestic logo threadwatch logo seochat tools logo