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?

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
Kyu Bi
  • 31
  • 2
  • Welcome. I think you should edit your question so the code is printed appropriately. – A Friedrich Jun 05 '12 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. – Hunter McMillen Jun 05 '12 at 13:44
  • That code doesn't compile. I get the following error: conflicting declaration ‘typedef struct ekstra ekstra’ – dcp Jun 05 '12 at 13:45
  • 1
    are these `**` part of the actual code? – moooeeeep Jun 05 '12 at 13:46

3 Answers3

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;
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
-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;
laifjei
  • 606
  • 2
  • 7
  • 16
  • -1 for teaching to typecast the result of malloc in C. Read [this](http://c-faq.com/malloc/mallocnocast.html) and [this](http://stackoverflow.com/questions/1565496/specifically-whats-dangerous-about-casting-the-result-of-malloc). – Lundin Jun 05 '12 at 14:00
  • @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 Jun 05 '12 at 14:07
  • @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. – Lundin Jun 05 '12 at 14:16