-2
struct list* list_cons(int data_item, struct list* tail);

What does "list*" mean when I declare this structure, and for structures I thought I use curly brackets instead of brackets? Sorry for this question, I tried googling it but I couldn't find anything.

mch
  • 9,424
  • 2
  • 28
  • 42
jake stan
  • 9
  • 1
  • 7

4 Answers4

2

This is a function declaration for the function list_const that accepts two parameter, an integer data_item and a pointer named tail that points to an object of type list. The return value of that function is again a pointer to an object of type list. You could also rewrite this snippet to

struct list; // forward-declare the type

list* list_cons(int data_item, list* tail);  // declare function without 'struct' keyword
lubgr
  • 37,368
  • 3
  • 66
  • 117
0

This looks like code inherited from good old C where you have to state that the type you're using is a struct, unless it's explicitly typedef'd.

The struct list* at the start is the type returned by the function list_cons. In C++ you would simply write list* list_cons. Either syntax here means the function returns a pointer to a list structure.

Same goes for the parameters in the function, the first parameter is simply an int, the second parameter is a pointer to a list structure. Considering that the variable is named tail, I would expect it should be the back of the list.

If you've not covered pointers in C++ then you'll have to go a take a good long look into that first before understanding this. Otherwise the struct list* in C is equivalent of list* in C++.

TheBeardedQuack
  • 449
  • 4
  • 15
0

Function can be declared as:

return_type func_name(arg1, arg2)

struct list* list_cons(int data_item, struct list* tail);

> return_type ---> struct list*
func_name ----> list_cons
arg1 -----> int data_item
arg2 ---------> struct list* tail

So basically, struct list* list_cons(int data_item, struct list* tail); is a function declaration which takes two arguments as data_item and pointer to tail. It returns a pointer of struct list type.

Monk
  • 756
  • 5
  • 16
0

You are MERGING the declaration of a list structure and the declaration of a pointer that points to a list.This function is taking two arguments of type int and a pointer to type list.So It is not needed for every structure to be declared like this.

Pvria Ansari
  • 406
  • 4
  • 20