Question

I want to write this program where I have this in one file named intarray.c:

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

struct IntArray
{
    int length;
    int *array;
};

struct IntArray erzeugeArr(int length)
{
    struct IntArray erzeugt = {length, (int *)malloc(sizeof(int)*length)};
    return erzeugt;
}

and this in the test main:

struct IntArray *string;
string = &(erzeugeArr(5));

Now, what I get is this:

Misuse of undefined type "struct IntArray"

because of the last bit with string = …. I have searched a lot, but I don’t understand the problem! If I do it like this:

void erzeugeArr(struct IntArray *erzeuge, int length)
{
    struct IntArray erzeugt = {length, (int *)malloc(sizeof(int)*length)};
    erzeuge = &erzeugt;
}

and

struct IntArray *string;
erzeugeArr(string, 5);

, then I can compile my program, but when starting it, I get a segmentation fault, which I assume I get because the main has no access to erzeugt?

Was it helpful?

Solution

I guess you are returning the address of a local (struct) variable, which, when you try to use it later, will give you a segmentation fault (trying to access an invalid address).

You can dynamically allocate a structure in function and return it.

        struct IntArray* erzeugeArr(int length)
        {
            struct IntArray *erzeugt=(struct IntArray*)malloc(sizeof(struct IntArray*));
            erzeugt->array = (int*) malloc(sizeof (int)*length);
            return erzeugt;
        }

        int main()
        {
            struct IntArray *str;
            str=erzeugeArr(10);
            //use str for processing
        }

OTHER TIPS

The string = &(erzeugeArr(5)); statement attempts to take the address of an expression (the function returns an expression of type struct IntArray.) Only objects have addresses, expressions dont.

You could do something like this:

struct IntArray
{
  int length;
  int *array;
}

struct IntArray *alloc_arr(int length)
{
  struct IntArray *erzeugt = calloc(sizeof(struct IntArray));
  erzeugt->length = length;
  erzeugt->array = calloc(length * sizeof(int));
  return erzeugt;
}

void free_arr(struct IntArray *p)
{
  free(p->array);
  free(p);
}

Alternatively, you could also do something a little sneakier which allocates the struct at a size big enough to contain the array of ints inline:

struct IntArray
{
  int length;
  int array[1];
}

struct IntArray *alloc_arr(int length)
{
  struct IntArray *erzeugt = calloc(sizeof(struct IntArray) + (length-1) * sizeof(int));
  erzeugt->length = length;
  return erzeugt;
}

void free_arr(struct IntArray *p)
{
  free(p);
}

&(erzeugeArr(5)) is awkward. It seems to return an address of the function. But C compiler would fail due to lvalue required error.

Instead try the following:-

struct IntArray *string,s1;

s1=erzeugeArr(5);
string = (struct IntArray *) &s1;  // If you really require pointer access
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top