-4

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'.

yulian
  • 1,601
  • 3
  • 21
  • 49
Hgaur
  • 109
  • 3
  • 10

6 Answers6

4

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++.

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
2

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.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • 1
    You have to allocate the space for the `struct name` somehow. `malloc` puts it on the heap, and says to allocate `sizeof *obj` (or, put another way, the size of `struct name`). Alternately, you can allocate the struct on the stack without `malloc` as shown in my answer. – par Aug 08 '13 at 05:41
  • 1
    @joni I think billz means that `struct {...} name;` already allocates memory - note that this is different from `struct name {...};` which only declares the structure type – Andreas Fester Aug 08 '13 at 05:46
  • 1
    Very good point Andreas. It should be declared `struct name {...};` – par Aug 08 '13 at 05:48
1

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;
}
Leafy
  • 6,631
  • 1
  • 15
  • 13
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
}
par
  • 17,361
  • 4
  • 65
  • 80
0

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);

}
JackCColeman
  • 3,777
  • 1
  • 15
  • 21
-1
obj->a = temp;

just have a try!

BlackMamba
  • 10,054
  • 7
  • 44
  • 67