Note that there are some explanatory texts on larger screens.

plurals
  1. POstruct unable to define new type and structs within structs
    primarykey
    data
    text
    <p>I have the following C code.</p> <p>It is supposed to create a house type and a room type. However It seems the room type is not being recognised because I can't create functions of type room.</p> <p>After the code is the compiler error. </p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; //create type Room. struct Room { float width; float length; float height; char *name; }; //create type House. struct House { char *address; /*Rooms in house are an array of pointers. Each pointer to a Room.*/ struct Room *rooms[10]; }; //protype functions. void printHouse (struct House house); Room createRoom(char *name, float width, float length, float height); int main() { //create house h. struct House h; h.address = "10 Palace Road"; for (int i = 0; i &lt; 10; i++) h.rooms[i] = NULL; //create a room (hall) without use of createRoom. Successful. struct Room hall; hall.width = 10; hall.length = 12; hall.height = 9; hall.name = "Hall"; h.rooms[0] = &amp;hall; h.rooms[1] = &amp;createRoom("lounge", 20, 20, 9); printHouse(h); return 0; } Room createRoom(char *name, float width, float length, float height) { struct Room r; r.width = width; r.length = length; r.height = height; r.name = name; return r; } //prints contents of the house. Working okay. void printHouse (struct House house) { printf("%s",house.address); printf("\n\r\n\r"); for (int i=0; i&lt;10; i++) { if (house.rooms[i] != NULL) { struct Room r = *house.rooms[i]; printf("Room #%d: %s", i, r.name); } } } </code></pre> <p>I am getting the following, that I don't know how to fix, during compile. Could anyone tell me what to do here and tell me why Room is not being recognised as a type?. </p> <pre><code>gcc -std=c99 -c -Wall -ggdb -c -o struct.o struct.c struct.c:24:1: error: unknown type name ‘Room’ struct.c: In function ‘main’: struct.c:40:15: error: lvalue required as unary ‘&amp;’ operand struct.c: At top level: struct.c:49:1: error: unknown type name ‘Room’ struct.c: In function ‘createRoom’: struct.c:57:2: error: incompatible types when returning type ‘struct Room’ but ‘int’ was expected struct.c:58:1: warning: control reaches end of non-void function [-Wreturn-type] make: *** [struct.o] Error 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload