victoria277
victoria277

Reputation: 67

Understanding variables with structs C programming

I need some assistance understanding how variables of a typedef work with structs

/*This is the Struct, with a new type Program*/
typedef struct prog{
    char move[MAXNUMTOKENS][MAXTOKENSIZE];
    int cm; 
}Program;

Program prog;   // new variable of type Program that has an array and an int

My question is what does this next statement do, does it initialize the int cm in the structs to zero ?

prog.cm=0;

Upvotes: 0

Views: 87

Answers (1)

TomP89
TomP89

Reputation: 1460

Think of a struct as a template for a new object.

The line Program prog is creating a new structure object based on the template defined at the top.

So prog.cm=0; means that yes you are initializing that specific objects cm field to zero.

The typedef is there simply so you dont have to write struct prog 'variableName' when you want to create a new struct object

Upvotes: 1

Related Questions