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);
                            ^
6
  • You compiler wants to cry. Enable warnings and pay heed to them. If your compiler really does not warn for shadowing names, get one which is <10 years old. Commented Jan 8, 2016 at 17:58
  • Do not declare variables in a single line, specially when one of them is a pointer, and the other one isn't. Commented Jan 8, 2016 at 18:01
  • That's exactly what I said in my answer, answer immediately downvoted... also if this is the heart of the problem here... Commented Jan 8, 2016 at 18:04
  • i'm compiling with terminal which the compiler is : GNU GCC compiler ,, how could i get another one ? Commented Jan 8, 2016 at 18:05
  • @iharob it works with a pointer to pointer code : struct poo **adres ; Commented Jan 8, 2016 at 18:08

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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; ^
@JadeGenome adres[0]=poo; is enough . No need of & with poo .
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;

5 Comments

Bad suggestion! No need to malloc() at all, just use struct poo *adres[4]; instead. Also, check the return value of malloc().
But it's not a good idea, see this answer please.
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
@iharob : i want to know why it's not a good idea, i want to get where's the problem in that solution, please .
@JadeGenome I don't think it's clear what you want to do. And in c it works does not mean that it's correct.
-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;

3 Comments

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);
@rootkea Don't just assume that malloc() worked succesfully.
@iharob Oops. Updated the answer. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.