1

I am trying to create a function which allocates memory for a structure array defined in "main". The problem seems to be that my function does not recognize the structure. What is wrong with the following code?

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

typedef struct typecomplex { float r; float i;  } complex;
complex *myfunction(int n);

int main (int argc, char *argv[]) {
    complex *result = myfunction(1000);
    exit(0);
}

... and in another file...

struct complex *myfunction(int n)  {
  complex *result = (complex *)malloc(n*sizeof(*complex));
  if(result==NULL) return(NULL);
      else return(result);
}
JWDN
  • 382
  • 3
  • 13
  • Your `struct` is called `_complex`. Why are You screwing with those dirty `typedefs`? Also, in this case Your should move `struct's` deffinition to *.h file to be included from both *.c files. – Kamiccolo Oct 15 '13 at 19:48
  • error: ‘complex’ undeclared (first use in this function) – JWDN Oct 15 '13 at 19:48
  • Gotta put the declaration in a common header file... (and please **don't cast the return value of `malloc()`!** And don't clutter the reserved namespace (i. e. don't use identifiers that begin with underscores), etc., etc. –  Oct 15 '13 at 19:48
  • @H2CO3 - any way to do it without using header files? – JWDN Oct 15 '13 at 19:50
  • 3
    @JWDN You could duplicate the definition, but it's better to use a header file so the definition isn't in two places at once; any time you do that, you're likely to make a mistake where you update one and forget to update the other. Why wouldn't you want to use a header file? – Brian Campbell Oct 15 '13 at 19:52
  • @BrianCampbell, I just prefer things to be explicit and visible in the main code without having to wade through large header files - not a good reason, but legacy. Same with defining the functions. I've just avoided structures for years up 'till now so this is the first time I've encountered this problem. – JWDN Oct 15 '13 at 20:05

3 Answers3

2

Building on fvdalcin's answer:

myprog.c:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>   
#include "mycomplex.h"


int main (int argc, char *argv[]) {
    complex *result = myfunction(1000);
    exit(0);
}

mycomplex.h:

#ifndef __MYCOMPLEX_H__
typedef struct typecomplex { float r; float i;  } complex;
complex *myfunction(int n);
#define __MYCOMPLEX_H__
#endif

(The #ifdef's are a good idea to keep it from being included more than once.)

mycomplex.c:

#include <stdlib.h>
#include "mycomplex.h"

complex *myfunction(int n)  {
  complex *result = malloc(n*sizeof(complex));
  if(result==NULL) return(NULL);
      else return(result);
}

Note subtle but important fixes here--sizeof(complex) instead of sizeof(complex*), declaration of myfunction() doesn't include the keyword "struct", and no cast on malloc()--it doesn't need one and can hide the fact that you may be missing the include file with its prototype (see Do I cast the result of malloc?). myfunction() could actually by simplified down to one line:

return malloc(n*sizeof(complex));
Community
  • 1
  • 1
willus
  • 501
  • 3
  • 7
1

Move this declaration typedef struct _complex { float r; float i; } complex; to the "other" file. This other file has to be your foo.h file, which has an foo.c equivalent which implements the methods declared in the foo.h. Then you can simply add the foo.h to your main.c file and everything will work fine.

fvdalcin
  • 1,047
  • 1
  • 8
  • 26
1

Here is a code with corrections that compiles well:

typedef struct typecomplex { float r; float i;  } complex;
complex *myfunction(int n)  {          
  complex *result = (complex *)malloc(n*sizeof(complex)); //removed * from sizeof(*complex)
  if(result==NULL) return(NULL);
      else return(result);
}

int main (int argc, char *argv[]) {
    complex *result = myfunction(1000);
    exit(0);
}
Igor Popov
  • 2,588
  • 17
  • 20
  • thanks for catching the *. This will work but I need the function in a separate file so many programs can call it :) – JWDN Oct 15 '13 at 20:08