How to allocate a member of a struct to C?

Asked

Viewed 460 times

8

I wonder if it is possible to allocate a atributo of a struct, follows my struct example:

struct MinhaStructExemplo
{
   int * atributo_quantidade; /*Atributo que eu gostaria de alocar na memoria*/
};

For the attribute atributo_quantidade i would like to allocate the positions for it in memory using the malloc, as if it were a pointer, follow what I’ve tried:

#include <stdio.h>
#include <stdlib.h>

struct MinhaStructExemplo
{
    int  * atributo_quantidade; /*Atributo que eu gostaria de alocar na memoria*/
};

int main(void)
{
    int * valor;
    struct MinhaStructExemplo structExemplo;
    valor = malloc(sizeof(int) * 1);
    structExemplo.atributo_quantidade = malloc(sizeof(int) * 1);
}
  • I don’t understand. Maybe because you don’t know how to do it. In this case either you allocate the structure or its member has to be a pointer. What do you want to know? No matter where the structure is, all the memory required for all members will be allocated. Just remembering that members that are pointers only allocate space to the pointer. For the pointed object it has to be manual.

  • I’ll edit the question.

  • And what do you want to do that’s not working? Want to play what’s in value on the member?

  • I want to allocate positions in memory for the member to work with it as a vector.

1 answer

8


This example should clarify further:

#include <stdio.h>
#include <stdlib.h>

struct MinhaStructExemplo {
    int *atributo_quantidade;
};

int main(void) {
    int *valor;
    struct MinhaStructExemplo structExemplo;
    valor = malloc(sizeof(int));
    *valor = 10;
    structExemplo.atributo_quantidade = valor;
    printf("%d\n", *structExemplo.atributo_quantidade);
    structExemplo.atributo_quantidade = malloc(sizeof(int));
    *structExemplo.atributo_quantidade = 30;
    printf("%d\n", *structExemplo.atributo_quantidade);
    structExemplo.atributo_quantidade = malloc(sizeof(int) * 3);
    structExemplo.atributo_quantidade[0] = 1;
    structExemplo.atributo_quantidade[1] = 2;
    structExemplo.atributo_quantidade[2] = 3;
    printf("%d\n", structExemplo.atributo_quantidade[0]);
    printf("%d\n", structExemplo.atributo_quantidade[1]);
    printf("%d\n", structExemplo.atributo_quantidade[2]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you want to save the variable value valor, just place the contents of the variable in the member. Both are pointers. What is saved in the variable is the pointer (created by malloc()) and not value pointed by it (done right away). So when we want to point to the same place, ie to the same value, just a direct assignment.

Then I made a new allocation in memory and with this new address was assigned to the member of the structure. Then a value was placed at this address.

Whenever a type is a pointer, it must contain a memory address that points to where it has a value. To get a memory address there are basically three options: 1) allocate memory with malloc; 2) takes an address of an object with the operator &; 3) copies an existing address into another variable (possibly a literal).

Then the same was done with a sequential allocation simulating a array.

  • Thank you so much is that msm! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.