0

I have a struct called TCarro with this properties.

typedef struct {
  char nome[20];
  char placaDoCarro[5];
} TCarro;

And I would like to know how I could allocate this variable struct TCarro *carro; dynamically. For example, when I use scanf to read the number of cars, how should I allocate some memory so that I could do carro[0]->..., carro[1]->..., ...

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vagner Wentz
  • 391
  • 1
  • 7
  • 29
  • 4
    Any decent beginner books, tutorial or class should have this information. If you don't have a book, or don't attend a class, then I really urge you to get some books and possibly enroll in a class. – Some programmer dude Jul 28 '21 at 19:51
  • 1
    `TCarro *a = malloc(sizeof(TCarro)*nb_items);` something like that – Jean-François Fabre Jul 28 '21 at 20:01
  • Does this answer your question? [malloc for struct and pointer in C](https://stackoverflow.com/questions/14768230/malloc-for-struct-and-pointer-in-c) – Saifeddine Ben Salem Jul 28 '21 at 20:02
  • @Jean-FrançoisFabre but if I would like to see the length about carro, using printf appears this `format specifies type 'int' but the argument has type 'struct TCarro *'` – Vagner Wentz Jul 28 '21 at 20:18
  • @VagnerWentz You can't obtain/display the length of a dynamically allocated array. You will have to keep track of that value separately, ie `printf("%d", nb_items)`. – Remy Lebeau Jul 29 '21 at 00:17

1 Answers1

4

To dynamically allocate memory for a struct, the following can be used:

TCarro *carVariable=malloc(sizeof(TCarro)*someNumbers);

, where someNumbers are the number of elements you want to allocate.

From the error you posted in the comment, I assume you tried doing something like:

printf("%d",carVariable);

As you have specified the integer specifier in printf (%d), the compiler expects the function to receive an integer, but you give it a struct TCarro instead.

So now you may be wondering, what specifier can I use to print my carVariable?

C, actually, does not offer such specifier, because structs are types created by the programmer, so it has no idea of how to print it.

What you could do, if you wanted to print your variable would be to print each individual elements of the array.

Something like this:

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

typedef struct{
  char nome[20];
  char placaDoCarro[5];
}TCarro;

int main(void){
  size_t someNumbers=10; //if you want to allocate 10 cars
  TCarro *carVariable=malloc(sizeof(TCarro)*someNumbers);
  //notice that struct before TCarro is not needed, as you defined it as an existing type (named TCarro)

  strcpy(carVariable[0].nome,"Mercedes");
  strcpy(carVariable[0].placaDoCarro,"xxxx");

  strcpy(carVariable[1].nome,"Ford");
  strcpy(carVariable[1].placaDoCarro,"xxxx");
 
  //printf cars
  printf("%s %s\n",carVariable[0].nome, carVariable[0].placaDoCarro);
  printf("%s %s\n",carVariable[1].nome, carVariable[1].placaDoCarro);

  free(carVariable);
  return 0;
}

Notice that I used strcpy, because I needed to copy a string to each field of the struct.

  • 3
    Consider sizing to the refenced object and not the type: `carVariable = malloc(sizeof(TCarro) * someNumbers);` -->`carVariable=malloc(sizeof( *carVariable * someNumbers);`. Easier to code right, review and maintain. – chux - Reinstate Monica Jul 28 '21 at 22:46
  • Hi, but if I would like to read using scanf the quantity of cars? – Vagner Wentz Jul 28 '21 at 23:42
  • @chux-ReinstateMonica I think you meant "--> `carVariable=malloc(sizeof(*carVariable) * someNumbers);`" (you are missing a `)` on the `sizeof`) – Remy Lebeau Jul 29 '21 at 00:20
  • @VagnerWentz then you simply `scanf()` into `someNumbers` before you then pass `someNumbers` to `malloc()` – Remy Lebeau Jul 29 '21 at 00:21
  • Okay, now I need to pass the variable that created as parameter to another function at functions.h, how can I do this? – Vagner Wentz Jul 29 '21 at 00:22
  • @VagnerWentz then you simply pass both the `carVariable` and the `someNumbers` as parameters to the desired function, eg: `int someNumbers; scanf("%d", &someNumbers); TCarro *carVariable = malloc(sizeof(TCarro)*someNumbers); ... function(carVariable, someNumbers); ... free(carVariable);` – Remy Lebeau Jul 29 '21 at 00:23
  • But `void adicionarCarro(int *indexador, TCarro* carro) {` `Unknown type name 'TCarro'` @RemyLebeau – Vagner Wentz Jul 29 '21 at 00:27
  • @VagnerWentz Obviously, you would need to define `TCarro` before defining `adicionarCarro()`, so it knows what a `TCarro` is. Which means moving the declaration of `TCarro` into `functions.h`. – Remy Lebeau Jul 29 '21 at 00:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235404/discussion-between-vagner-wentz-and-remy-lebeau). – Vagner Wentz Jul 29 '21 at 00:29
  • @RemyLebeau Oops, I really meant `carVariable = malloc(sizeof *carVariable * someNumbers);` without the unnecessary `()` about `*carVariable`. – chux - Reinstate Monica Jul 29 '21 at 01:38