0

I was recently reading about ways to create something that resembles c++ objects, but in C, and I came across some pretty good examples. However, there was this single piece of code that had me thinking for hours, since it was the first time I saw this kind of syntax, and I didn't find anything like it on Google...


The block itself is this one:

struct stack {
     struct stack_type * my_type;
     // Put the stuff that you put after private: here
};
struct stack_type {
     void (* construct)(struct stack * this); // This takes uninitialized memory
     struct stack * (* operator_new)(); // This allocates a new struct, passes it to construct, and then returns it
     void (*push)(struct stack * this, thing * t); // Pushing t onto this stack
     thing * (*pop)(struct stack * this); // Pops the top thing off the stack and returns it
     int this_is_here_as_an_example_only;
}Stack = {
    .construct = stack_construct,
    .operator_new = stack_operator_new,
    .push = stack_push,
    .pop = stack_pop
};

Assuming all the functions being set to the pointers are defined somewhere else, my doubts are the following:

1) Why does the point ( '.' ) mean, or whats its purpose when initializing the function pointers? (example: .construct = stack_construct )

2) why is there an equal sign after 'Stack' at the end of the struct definition, and why is there something other than a ';' ( in this case the word 'Stack' ), given the fact that there is no typedef at the beginning? I assume it has something to do with initialization (like a constructor, I don't know), but it is the first time I see a struct = {...,...,...} in the definition. I've seen that when you initialize a struct like in the following example:

typedef struct s{
int a;
char b;
}struc;

void main(){
    struc my_struct={12,'z'};
}

But this is not in the main declaration of the struct, and still, there are no '=' within the {}, unlike the first example, where it showed something like...

struc my_struct={ a = 12,
                  b = 'z'};

3) This is a minor doubt, meaning, I'm much more interested in the first two. Anyway, here it goes... At the beginning of the first code, it says something like '// Put the stuff that you put after private: here'. Why is that? how would that make them private?


That is all, I would appreciate anything, this had me thinking for hours! Thanks in advance, have a great day!

J3STER
  • 1,027
  • 2
  • 11
  • 28
  • 1
    Read [this](http://en.cppreference.com/w/c/language/struct_initialization) page thoroughly. It explains everything with examples. – DeiDei Jan 22 '17 at 22:36
  • Because such a question can be easily answered by reading the documentation. It would not be of much benefit to the community, plus it has already been answered before. It seems like you put more effort into the question, rather than researching it. I didn't downvote though. – DeiDei Jan 22 '17 at 23:19

1 Answers1

0

1) Don't worry about the members being functions, it's just this:

What does dot (.) mean in a struct initializer?

2) In

struct S {int i;}
s = {0};

the first line names a type that can be used to declare and initilize a variable.

It is really equivalent with something like

double d = 1;

where you replace double with struct S {int i;}, replace s with d, and replace 1 with the initializer for a struct, {0}. Now combine this with the dot syntax.

It just has the side effect of also defining struct S.

Update: Regarding 3), I do not think that with private they refer to an implementation of access control, but rather refer to the fact that in C++, you would usually list the data members in the private section of the class, so I understand this as instructions to add the data members after the type identifying element.

Community
  • 1
  • 1
Ludwig Schulze
  • 2,155
  • 1
  • 17
  • 36