Check my code please?

S

Steve

OK, this is the first time I've used structs. I'm trying to create a linked
list.

Obviously most of it is missing but I'm trying to get it to compile
error-free with the struct and functions defined. Why do I get the error
"parse error before '*' token" 5 times (once for each use of list_node*)?

==================================

struct list_node {
char content[10];
struct list_node *next;
};


/* Creates a new list and returns a pointer to the first node */
list_node* create_list(void) {
return 0;
}

/* Copies a string into a new node and adds to the list in alphabetical
order. Returns int success value */
int insert(list_node*, char*) {
return 0;
}


/* Removes the first occurrence of the string parameter. Returns int success
value */
int remove(list_node*, char*) {
return 0;
}

/* Prints the list of string from first to last */
void print(list_node*) {
return;
}

/* Destroys the list, deallocates all associtaed memory. Returns int success
value */
int destroy_list(list_node*) {
return 1;
}



int main()
{


}
 
E

Eric Sosman

Steve said:
OK, this is the first time I've used structs. I'm trying to create a linked
list.

Obviously most of it is missing but I'm trying to get it to compile
error-free with the struct and functions defined. Why do I get the error
"parse error before '*' token" 5 times (once for each use of list_node*)?

Because C is not C++.
struct list_node {
char content[10];
struct list_node *next;
};

This defines a type whose name is `struct list_node' ...
/* Creates a new list and returns a pointer to the first node */
list_node* create_list(void) {

.... and `list_node' is only half of the name, hence the
error message.
 
M

manoj1978

Steve said:
OK, this is the first time I've used structs. I'm trying to create a linked
list.

Obviously most of it is missing but I'm trying to get it to compile
error-free with the struct and functions defined. Why do I get the error
"parse error before '*' token" 5 times (once for each use of list_node*)?

==================================

struct list_node {
char content[10];
struct list_node *next;
};


/* Creates a new list and returns a pointer to the first node */
list_node* create_list(void) {
return 0;
}

use "struct list_node*" instead of "list_node*"
 
J

Jesse Meyer

Steve said:
Why do I get the error "parse error before '*' token" 5 times (once
for each use of list_node*)?

==================================

struct list_node {
char content[10];
struct list_node *next;
};


/* Creates a new list and returns a pointer to the first node */
list_node* create_list(void) {
return 0;
}

[snip]

Because 'listnode*' is invalid: either use a typedef, or try
'struct listnode *create_list(void) { /* ... */ }'
 
C

CBFalconer

Steve said:
OK, this is the first time I've used structs. I'm trying to
create a linked list.

Obviously most of it is missing but I'm trying to get it to
compile error-free with the struct and functions defined. Why do
I get the error "parse error before '*' token" 5 times (once for
each use of list_node*)?

==================================

struct list_node {
char content[10];
struct list_node *next;
};

/* Creates a new list and returns a pointer to the first node */
list_node* create_list(void) {
return 0;
}

use "struct list_node*" instead of "list_node*"

Or add, after the struct definition:

typedef struct list_node list_node;

You can combine this with the type declaration, as in:

typedef struct list_node {
char content[10];
struct list_node *next;
} list_node;
 
L

LaDainian Tomlinson

OK, this is the first time I've used structs. I'm trying to
create a linked list.

/* Copies a string into a new node and adds to the list in
alphabetical order. Returns int success value */
int insert(list_node*, char*) {
return 0;
}

In addition to what others have said, you have unnamed parameters
in many of your function definitions. This is okay for function
declarations such as (assuming a list_node typedef):

int insert( list_node *, char * );

which generally appear near the beginning of a file. But you
have to name the parameters when you write the code for the
function later:

int insert( list_node *head, char *str ){
...
}


Brandan L.
 
C

Chris Torek

[on "struct list_node" vs "list_node"]

Or add, after the struct definition:

typedef struct list_node list_node;

You can combine this with the type declaration, as in:

typedef struct list_node {
char content[10];
struct list_node *next;
} list_node;

I dislike typedefs in general -- I find code using the "struct"
keyword far easier to read -- but if you are going to use typedef
here, you might as well put it in *first*, and then use it in
the structure definition:

typedef struct list_node LIST_NODE;

struct list_node {
char content[10];
LIST_NODE *next; /* can use the typedef here, now */
};

Note that, if you do use typedefs, spelling typedef'ed names in
ALL_CAPS or UsingFunkyCapitalization or with some other special
distinguishing characteristic -- POSIX uses an "_t" suffix, for
instance -- is generally a good idea. (Otherwise it is impossible
to tell at a glance whether something is a typedef, and because
typedefs do not quite work right syntactically, it is often important
to distinguish them, to avoid name collisions.)

(Consider:

typedef int x;
void f(x x) { ... }

which is legal but horrible C code. Yes, you can do the same with
ordinary variable names; but it is even worse when you do it with
type-aliases.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,180
Latest member
CryptoTax Software
Top