0

I have trouble with structures in c.

I have two structures like

typedef struct
{
    char isim[256];
    int deger;
    struct ekstra *sonra;
}ekstra;

typedef struct
{
    char *name;
    int val;
    struct ekstra *next;
}node;

/*and main is*/

int main()
{
    int i;
    node dizi[12];

    for(i=0;i<12;i++)
    {
        dizi[i].name = malloc("asdasd"*sizeof(int));
        strcpy (dizi[i].name,"asdasd");
        /*and trouble starts here*/
        **dizi[i].next = malloc(sizeof(ekstra));
        printf("%s",dizi[i].next->isim);**
    } 
}

the error is

error: dereferencing pointer to incomplete type

How can I hold place for dizi[i].next?

4
  • Welcome. I think you should edit your question so the code is printed appropriately. Commented Jun 5, 2012 at 13:42
  • You allocate space for an entire struct when really you just need a pointer to that struct, also even though you allocate space you never assign it a value. Also, format your code; someone else shouldn't have to. Commented Jun 5, 2012 at 13:44
  • That code doesn't compile. I get the following error: conflicting declaration ‘typedef struct ekstra ekstra’ Commented Jun 5, 2012 at 13:45
  • 1
    are these ** part of the actual code? Commented Jun 5, 2012 at 13:46

3 Answers 3

8

struct ekstra is not the same as ekstra.

Your first struct typedef should be declared as follows:

typedef struct ekstra
{
    char isim[256];
    int deger;
    struct ekstra *sonra;
}ekstra;
Sign up to request clarification or add additional context in comments.

Comments

0

typedef struct... ekstra;

This means: "create a type that is called ekstra". From now on this type can be used just as any variable type (int, char etc).

struct ekstra *next;

This means: Somewhere in my program there is a struct of some type, I don't know what it contains, but I want to point to an element of that type. This is the meaning of incomplete type.

To fix your problems, simply replace this row with ekstra *next;.


More comments not directly related to the question:

dizi[i].name = malloc("asdasd"*sizeof(int));

This is pure nonsense code. It means: "Create a constant string literal in the ROM part of my program. In this constant string literal, store the letters "asdasd" and a null termination character. Then take the address of this ROM memory location, which is completely irrelevant to my application, convert it to an integer so that I get a 32-bit nonsense number. Then multiply this nonsense number with the sizeof an int, which doesn't make any sense to anyone either. Then take this completely nonsense result and allocate a random amount of dynamic memory based on this. Then watch the program crash.

Comments

-1

I don't understand code line like

malloc("asdasd"*sizeof(int));

But, I think you problem should slove like this

dizi[i].next = (ekstra *)malloc(sizeof(ekstra));

and you struct define should like

typedef struct node{
    int a;
    int b;
    struct node *next;
}node;

3 Comments

-1 for teaching to typecast the result of malloc in C. Read this and this.
@Lundin - Actually, my compiler gives an compile error if the cast isn't there: error C2440: '=' : cannot convert from 'void ' to 'ekstra *' Conversion from 'void' to pointer to non-'void' requires an explicit cast. I'm using Visual Studio 2008 C++ compiler though. Does it make a difference if you are using native C compiler?
@dcp Indeed, C and C++ are very different in this case. C allows implicit function prototypes of malloc and it doesn't have the strict type checking of C++, which caused your compiler error. Don't compile C code with a C++ compiler.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.