1. ## Struct Malloc question

Hello,

I have 2 structs

Code:
```struct CELL{
int row;
int column;
};
typedef struct CELL Cell;

typedef struct CELL_NODE CellNode;
struct CELL_NODE
{
Cell     cell;
CellNode *next;
};
CellNode *top = NULL;```
I always thought that if you have a pointer in a struct you have to malloc that pointer before you can set it to something.

But my teacher posted sample code of:

Code:
```void addCell(const Cell cell){
CellNode *newNode = NULL;

if ( validCell(cell) )
{
newNode = (CellNode *)malloc(sizeof(CellNode));
assert(newNode != NULL);
if (newNode)
{
newNode->cell = cell;
newNode->next = top;
top = newNode;

checkState();
}
}
}```
The line that has newNode->next = top confuses me because I thought you first had to

Code:
`newNode->next = (CellNode*)malloc(sizeof(CellNode));`
Why can you just set it to top?
When addCell gets called again top is not NULL there is stuff in it, so don't you have to allocate space for it first before you can set it to something?

Thanks

2. I always thought that if you have a pointer in a struct you have to malloc that pointer before you can set it to something.
It's true to a point. The reason is because you need new memory to point to... so somewhere, there is a malloc call to new memory. You see this in the function, where newNode is allocated. What does happen in the real world though, is that you have existing pointers that are arranged in a specific way to make a data structure. The key to all this are pointer assignments. If you have two pointers, lhs and rhs, lhs = rhs will set the value of lhs (an address) to the same value as rhs.

To make sense of it just think about it a little bit. To remove the abstraction, just forget about the addresses - it doesn't really matter what they are - what matters is that you keep what they contain straight.

If you add a node to the start of an existing list like (0, 1)->(0, 2)->(1,0)->NULL, you would want the new start node to point to the rest of the list.

newNode = (0, 0)
top = (0, 1)

newNode->next = top
(0, 0)->(0, 1)
top = newNode
(0, 0)

So now the list is (0, 0)->(0, 1)->...

3. Originally Posted by seal308
I always thought that if you have a pointer in a struct you have to malloc that pointer before you can set it to something.
You only need to be sure you point to valid memory. If an object already exists, you can point to it just fine. If not, then you need to allocate memory for a new object.

Originally Posted by seal308
When addCell gets called again top is not NULL there is stuff in it, so don't you have to allocate space for it first before you can set it to something?
Have you tried tracing it through by hand (on paper)? It appears to work out just fine.

The first time, memory is allocated for a node, and its "next" pointer is set to "top", which is NULL.

Code:
```.
CellNode
+----------+
|          |
| (cell 1) |
|          |    (top)
|  (next)--|---> NULL
|          |
+----------+```
Then "top" is updated with the address of this new cell. So now, it looks like this:

Code:
```.
Top
+----------+
|          |
| (cell 1) |
|          |
|  (next)--|---> NULL
|          |
+----------+```
The next time through, it creates a new node, and its "next" pointer is set to "top":

Code:
```.
CellNode          Top
+----------+    +----------+
|          |    |          |
| (cell 2) |    | (cell 1) |
|          |    |          |
|  (next)--|--->|  (next)--|---> NULL
|          |    |          |
+----------+    +----------+```
Once again, "top" is updated with the address of the new cell:

Code:
```.
Top
+----------+    +----------+
|          |    |          |
| (cell 2) |    | (cell 1) |
|          |    |          |
|  (next)--|--->|  (next)--|---> NULL
|          |    |          |
+----------+    +----------+```
So it appears to be building the list backwards.

Originally Posted by seal308
But my teacher posted sample code of:
Tell your teacher they should not be casting the return value of "malloc()": FAQ > Casting malloc - Cprogramming.com

4. Ok thank you.
I went through it with the lldb debugger and everything is clear now.
Because the node the newNode will point to has already been previously created I don't need to create new memory for it again.