Originally posted by inkedmn
in all the examples i've seen, when a structure is created, it never initializes the variables to values, it just declares the variable and it's type. is it possible to initialize a variable within a structure when the structure is created?
You can initialize an object, but not a type.
Code:
#include <stdio.h>
struct ink { /* type */
char name[60];
int age;
};
int main( void )
{
struct ink /* type */ svar /* object */ = {"inkedmn"}; /* initializer */
union {/* type */
int i;
char c;
} uvar /* object */ = { 42 }; /* initializer */
printf("svar.name = \"%s\"\n", svar.name);
printf("uvar.i = %d\n", uvar.i);
return 0;
}
/* my output
svar.name = "inkedmn"
uvar.i = 42
*/
A search might turn up some info about structs/unions.