c++ - Assign int value to a pointer -
i need assign int value pointer, how it?
below small example of want.
struct { int a; } name; int temp = 3; struct name *obj = null; now, need assign value '3' struct's 'a'.
with
struct { int a; }name; you define struct variable allocates memory struct (e.g. on stack when local variable inside function). then, int temp = 3;, sufficient assign struct member like
name.a = temp; if want declare struct type only, use
struct name { int a; }; then can define number of struct variables based on type, like
struct name thename; and same assignment thename members above:
thename.a = temp; or, can define pointer struct , have allocate memory yourself:
struct name *nameptr; nameptr = malloc(sizeof(struct name)); nameptr->a = temp; note have tagged question both c , c++ - structs, should decide language take - see differences between struct in c , c++.
Comments
Post a Comment