I need to assign an int value to a pointer, how would I do it?
Below is a small example of what I want.
struct {
int a;
} name;
int temp = 3;
struct name *obj = NULL;
Now, I need to assign this value '3' to struct's 'a'.
I need to assign an int value to a pointer, how would I do it?
Below is a small example of what I want.
struct {
int a;
} name;
int temp = 3;
struct name *obj = NULL;
Now, I need to assign this value '3' to struct's 'a'.
With
struct {
int a;
}name;
you already define a struct variable which allocates memory for the struct (e.g. on the stack when it is a local variable inside a function). Then, with int temp = 3;, it is sufficient to assign to the struct member like
name.a = temp;
If you want to declare a struct type only, then use
struct name {
int a;
};
Then you can define any number of struct variables based on this type, like
struct name theName;
and do the same assignment to theName members as above:
theName.a = temp;
Or, you can define a pointer to a struct and then have to allocate the memory yourself:
struct name *namePtr;
namePtr = malloc(sizeof(struct name));
namePtr->a = temp;
Note also that you have tagged your question both with C and C++ - especially with structs, you should decide which language to take - see Differences between struct in C and C++.
Declaring a pointer to a struct doesn't reserve memory for it, so first you have to do that. For example:
obj = malloc(sizeof(*obj));
Now you can assign the value:
obj->a = temp;
Note that the program as it currently stands does not define "struct name", it defines a variable called "name" that holds a struct. This is probably not what you intended.
The basic problem with the code is name is not the name of the structure but an object or a variable of the structure whose name you have already defined.
If u don't want to name the structure, even then still it need memory to be allocated.
struct
{
int a;
}name, *obj;
int temp = 3;
int main()
{
obj=&name; // 'obj' is pointing to memory area of 'name' : Keep this in mind throughout the code
obj->a=temp;
printf("%d %u %d",temp,&temp,obj->a);
return 0;
}
Best option is to put a name to the structure then use its pointer after allocating memory
typedef struct
{
int a;
}name;
int temp = 3;
name *obj = NULL;
int main()
{
obj = (name *)malloc(sizeof(name));
obj->a=temp;
printf("%d %u %d",temp,&temp,obj->a);
return 0;
}
EDIT (thanks Andreas):
Properly, your struct should be declared like so:
struct name {
int a;
};
void foo() {
struct name n; // allocate space for 'struct name' and call it n
struct name *obj; // a pointer to a 'struct name'
int temp = 3;
obj = &n; // make obj point to n
n.a = temp; // direct assignment to a
obj->a = temp; // assignment to a via pointer dereference
// a is now 3 in any case
}
Here is another, annotated, version of your code. Ran this on Eclipse/Microsoft C compiler, this is NOT C++ code.
#include <stdio.h>
main()
{
// define a structure as a data type
typedef struct
{
int *a;
} name;
// allocate storage for an integer and set it to 3
int temp = 3;
// allocate storage for the name structure
name obj;
// set the value of a in name to point to an integer
obj.a = &temp;
// dereference the integer pointer in the name structure
printf("%d\n", *obj.a);
}