0

I'm trying to make an array of pointers that points to structures addresses, here's the code:

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

typedef struct poo {

        int a,b;

} poo;

int main() {


struct poo *adres,poo;

poo.a = 2;

I'm wondering why this works :

adres = &poo;

printf("%d\n",adres->a);

and when I try to make an array of pointers it doesn't work :

 adres = malloc(4*sizeof(*adres));

adres[0] = &poo;

printf("%d\n",adres[0]->a);

Error :

 poo.c: In function ‘main’: poo.c:23:13: error: incompatible types when
 assigning to type ‘struct poo’ from type ‘struct poo *’
     adres[0] = &poo;
              ^ poo.c:25:26: error: invalid type argument of ‘->’ (have ‘struct poo’)
     printf("%d\n",adres[0]->a);
                            ^
Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

2

Because adres[0] is not a pointer. If you want to copy the structure poo then do adres[0] = poo, it's that simple.

Then since adres[0] is not a pointer, you need to use the normal structure-access syntax using a dot to access members in the structures, e.g adres[0].a.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • @iharob i changed the arrow with the dot and it shows that error when compiling : poo.c:25:13: error: incompatible types when assigning to type ‘struct poo’ from type ‘poo * {aka struct poo *}’ adres[0] = &poo; ^ – Jade Genome Jan 08 '16 at 18:13
  • @JadeGenome `adres[0]=poo;` is enough . No need of `&` with `poo` . – ameyCU Jan 08 '16 at 18:14
0

struct poo *adres is a pointer to a struct poo, not a pointer to a pointer to struct poo, which is what an array of pointers is. If you want an array of pointers (assuming you wanted an array of 4 pointers), it would be:

struct poo **adres;
adres = malloc(4*sizeof(*adres)); /* this allocates space for 4 pointers */
adres[0] = &poo;
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
DBug
  • 2,502
  • 1
  • 12
  • 25
  • Bad suggestion! No need to `malloc()` at all, just use `struct poo *adres[4];` instead. Also, check the return value of `malloc()`. – Iharob Al Asimi Jan 08 '16 at 18:03
  • But it's not a good idea, see [this answer please](http://stackoverflow.com/a/34683211/1983495). – Iharob Al Asimi Jan 08 '16 at 18:09
  • I was just explaining the difference, not how the memory was obtained. What if the number of addresses needed was not static and could not be determined until runtime? Would have to malloc then. But I do agree if number of pointer needed is static, then malloc is not necessary – DBug Jan 08 '16 at 18:12
  • @iharob : i want to know why it's not a good idea, i want to get where's the problem in that solution, please . – Jade Genome Jan 08 '16 at 18:22
  • @JadeGenome I don't think it's clear what you want to do. And in [tag:c] it works does not mean that it's correct. – Iharob Al Asimi Jan 08 '16 at 19:06
-1
adres = malloc(4*sizeof(*adres));

This is causing problem.

What this means is to allocate 4*sizeof (struct poo) bytes as tyoe of *adres is struct poo and then assign that address to adres.

Then this line

adres[0] = &poo;  

The type of adres[0] is struct poo whereas type of &poo is struct poo*. So type mismatch!


To store the address of struct variables we need an array of pointers to struct variables. which can be accomplished using dynamic memory allocation or statically depending upon requirement.

  1. Static Memory allocation

    struct poo *adresArray[4];

    Use this when you know in advance the number of struct variable pointers to create and also do not want to delete (free) or add the pointers at runtime.

  2. Dynamic Memory allocation

    poo **adresArray = malloc(n *sizeof(adres));

    Use this when you do not know in advance the number of struct variable pointers to create and also you do want to delete (free) or add the pointers at runtime.


Correct code would be: (Dynamic Memory Allocation)

poo **adresArray = malloc(n *sizeof(adres));
if(!adresArray)
{
    printf("Malloc failed!\n");
    exit(1);
}
adresArray[0] = &poo;
rootkea
  • 1,474
  • 2
  • 12
  • 32
  • already did. Error : poo.c:23:13: error: incompatible types when assigning to type ‘struct poo’ from type ‘struct poo *’ adres[0] = &poo; ^ poo.c:25:26: error: invalid type argument of ‘->’ (have ‘struct poo’) printf("%d\n",adres[0]->a); – Jade Genome Jan 08 '16 at 17:58
  • @rootkea Don't just assume that `malloc()` worked succesfully. – Iharob Al Asimi Jan 08 '16 at 17:59
  • @iharob Oops. Updated the answer. Thanks! – rootkea Jan 08 '16 at 18:02