0

im learning c programing. I want to define a global struct array of. so I would have a pointer to that array that each member of the array is a struct of complex numbers. my goal is to be able to acces to this array by his pointer (*vars) and being able to change/read its members on every function at the main.

Im facing troubles with this issue and im not sure how and where to define each thing. I tried this next code:

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

typedef struct complext
{
    double real;
    double img;
} complex;
complex* vars;

int main()
{
    int i;
    vars = malloc(6 * sizeof(vars));
    for (i = 0; i < 6;)
        vars[i]->real = 0;
}

Im getting an error when im trying to acces vars[i]. "request for member 'real' im something not a structure or a union. Thanks!

Stargateur
  • 24,473
  • 8
  • 65
  • 91
Yakalolo
  • 1
  • 2
  • You should typecast the return value of `malloc`. As so: `vars = (var*)malloc(6 * sizeof(*vars));`. Also, you should change the last line to `vars[i].real = 0;`. – cs95 Jun 05 '17 at 16:31
  • No, don't cast when it's not necessary. Sorry, my comment was in answer to Shiva's. – ncarrier Jun 05 '17 at 16:33
  • Tried that but still getting the same error. – Yakalolo Jun 05 '17 at 16:33
  • 1
    @Shiva [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). This is a C question, not in C++. – Stargateur Jun 05 '17 at 16:33
  • @ncarrier My bad. I meant to cast it to `(*vars)`, not `(*complex)`. – cs95 Jun 05 '17 at 16:34

3 Answers3

2

There are 3 bugs in your code
1. vars[i]->real should be vars[i].real. Pls honor the data-types You have defined vars to be a global pointer to the complex structure. To define it as an array use: complex vars[6]; --> Look at @José Fonte 's ans
2. malloc returns a void * cast it to (complex*) --> I learnt this should be ok (look at comments posted by @Stargateur)
3. The for loop at the end has no inc statement for i hence it runs forever
4. The malloc only allocates 6 pointer worth of memory (which is 6*sizeof(int)) since vars is a pointer of type complex. sizeof(vars) should be sizeof(complex) --> @xing pointed this out

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

typedef struct complext {
double real;
double img;
}complex;
complex *vars;

int main()
{
int i;
vars= (complex*)malloc(6*sizeof(complex));
for(i=0;i<6; i++)
vars[i].real=0;
}
Zakir
  • 2,222
  • 21
  • 31
  • 1
    I agree with 1 and 3, not with 2. And as Shiva has spotted, `sizeof(*vars)` should be used instead of `sizeof(vars)` – ncarrier Jun 05 '17 at 16:37
  • 2
    2. [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Stargateur Jun 05 '17 at 16:38
  • @xing: Thanks for pointing it out! overlooked it - corrected now – Zakir Jun 05 '17 at 16:43
1

Your mixing pointers with arrays. Doing it as an array (as a pointer already done by @Zakir):

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

typedef struct complext {
   double real;
   double img;
}complex;
complex vars[6];

int main()
{
   int i;

   for(i=0;i<6;i++)
   vars[i].real=0;
}
José Fonte
  • 4,016
  • 2
  • 16
  • 28
0

vars is of type struct complext *, but vars[i] is of type struct complext, so you just have to use vars[i].real instead of vars[i]->real.

ncarrier
  • 433
  • 3
  • 14