November 17th, 2013, 06:49 AM
-
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
November 17th, 2013, 08:31 AM
-
In the following snippet:
Code:
int * list2array(void * list);
Where has list been defined?
Where is list2array() implemented?
Jim
November 17th, 2013, 10:03 AM
-
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
November 17th, 2013, 10:30 AM
-
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.
November 17th, 2013, 12:53 PM
-
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.