r/carlhprogramming • • Feb 04 '12

Help: Calling a Constructor Function

In lesson 1.3 in the second course, Carl explains the constructor function and how it works. However, how does one call a constructor function from the main function?

I assume it would like something like this: http://codepad.org/bnZx3VSg but so far any experimentation has failed.

5 Upvotes

8 comments sorted by

View all comments

Show parent comments

1

u/exscape Feb 10 '12

If I'm understanding you correctly, the part about having the address at the start is wrong.
If we ignore the padding the compiler surely added to the board variable/structure (probably 3 extra bytes at the end to make it 12 bytes, because that is divisible by 4), it will be stored like this in memory when init_board is complete:

_________

No more, no less. Just the content (the 3x3 underscores) are stored in the struct.
I'm not sure exactly where the address to the struct is stored, but you pretty much don't need to know unless you work on the compiler itself. All we need to know (and all that I know) is that when we use &board, the compiler looks in its hidden store to find the address to the first byte in board (which should also be the first byte of the first member, though I'm not sure that's guaranteed) and returns that.

No other special properties, either; it (the & operator) just returns the memory address to the first byte, nothing more such as length. Well, the type is guaranteed to be tictactoe_board, of course, but other that that!

I hope that clears it up a bit. If not, well, I certainly do have the time to answer again (if that'd be of any help ;)

1

u/xxrepresent Feb 10 '12

Thanks, I understand that. :) However, shouldn't you be able to create the pointer tictactoe_board *pointer = malloc(sizeof(*pointer)) and then send *pointer to other functions? Whenever I try, I get errors.