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!

5
  • 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;. Commented Jun 5, 2017 at 16:31
  • No, don't cast when it's not necessary. Sorry, my comment was in answer to Shiva's. Commented Jun 5, 2017 at 16:33
  • Tried that but still getting the same error. Commented Jun 5, 2017 at 16:33
  • 1
    @Shiva Do I cast the result of malloc?. This is a C question, not in C++. Commented Jun 5, 2017 at 16:33
  • @ncarrier My bad. I meant to cast it to (*vars), not (*complex). Commented Jun 5, 2017 at 16:34

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I agree with 1 and 3, not with 2. And as Shiva has spotted, sizeof(*vars) should be used instead of sizeof(vars)
@xing: Thanks for pointing it out! overlooked it - corrected now
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;
}

Comments

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.

Comments

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.