void in struct casten

hier sind die structs:

Code:
typedef struct line
{
	char *id;
	int x1;
	int y1;
	int x2;
	int y2;
}	lineTyp;

typedef struct
{
    char *pfad;
	char *svg;
} names;

typedef struct 
{
	int offsetX;
	int offsetY;
	char prompt[4];
	int defWidth;
	int defHeight;
	int defRad;	
}	defValue;

typedef struct rect
{
	char *id;
	int x;
	int y;
	int width;
	int height;
	char *fill;
}	rectTyp;

typedef struct circle
{
	char *id;
	int cx;
	int cy;
	int r;
	char *fill;
}	circleTyp;

typedef struct polygon
{
	char *id;
	int *x;
	int *y;
	char *fill;
} polyTyp;

typedef struct text
{
	char *id;
	char *text;
	int x;
	int y;
	int font_size;
	char *fill;
}	textTyp;

typedef struct liste
{
	struct liste *next;
	struct liste *last;
	int head;
	int shape;
	void *inhalt;
}	listenTyp;

struct liste *start=NULL; //Anfang Liste
struct liste *end=NULL;   //Ende Liste
struct liste *temp=NULL;  //Hilfszeiger

Funktion in der die Elemente eingefügt werden
Code:
void insert_list(void *daten, int shape) //wird so aufgerufen insert(&lin,1)
{
    struct liste *pointerEnd;

    if(start == NULL)
    {
      if((start = (struct liste *) malloc(sizeof(listenTyp))) == NULL)
      {
         printf("Kein Speicherplatz vorhanden für anfang\n");
         return;
      }
		
	  start->next = NULL;
      start->last = NULL;

      // Daten in das erste Element schreiben
	  
	    if(shape==1) //Linie
        {
			//temp->inhalt = malloc(sizeof(temp->inhalt));
		    //temp->inhalt = (struct line*) malloc(sizeof(lineTyp));
			start->inhalt = daten;
			start->shape = 1;
			start->next = NULL;

		if(shape==2) //Rechteck
		{
			start->inhalt = daten;
			start->shape = 2;
			start->next = NULL;

		}

		if(shape==3) //Text
		{
			start->inhalt = daten;
			start->shape = 3;
			start->next = NULL;

		}

		if(shape==4) //Kreis
		{
			start->inhalt = daten;
			start->shape = 4;
			start->next = NULL;

		}
    }

    else
    {
        temp = start;
        while (temp->next != NULL)
        {
            temp = temp->next;
        }

        if((temp->next = (struct liste *)malloc(sizeof(listenTyp))) == NULL)
        {
            printf("Kein Speicherplatz für das letzte Element\n");
            return;
        }

        temp=temp->next;

        if(shape==1) //Linie
        {
			temp->inhalt = (struct line*) malloc(sizeof(lineTyp));
			temp->inhalt = daten;
			temp->shape = 1;
			temp->next = NULL;
		}

		if(shape==2) //Rechteck
		{
			temp->inhalt = daten;
			temp->shape = 2;
			temp->next = NULL;
		}

		if(shape==3) //Text
		{
			temp->inhalt = daten;
			temp->shape = 3;
			temp->next = NULL;
		}

		if(shape==4) //Kreis
		{
			temp->inhalt = daten;
			temp->shape = 4;
			temp->next = NULL;
		}
	} 
}
}

Funktion für die Ausgabe der Liste.
Code:
void ausgeben() 
{
	// Hilfszeiger auf den Anfang der Liste setzen
	temp = start;
	// Erstes Element ausgeben

	if((temp->shape)==1)
	{
		//struct line test;// = (struct line *) malloc(sizeof(lineTyp));
		//test = temp->inhalt;
		struct line *test = &(struct line*)(temp->inhalt);

		printf("\nid: %s  \n",test->id);
		printf("x1: %d  \n",test->x1);
		printf("x2: %d  \n",test->x2);
		printf("y1: %d  \n",test->y1);
		printf("y2: %d  \n",test->y2);
		printf("---------------------------------------\n");
	}

	if((temp->shape)==2)
	{
		struct rect *test = (struct rect *) malloc(sizeof(rectTyp));
		test = (struct rect *)(temp->inhalt);

		printf("\nid: %s  \n",test->id);
		printf("x: %d  \n",test->x);
		printf("y: %d  \n",test->y);
		printf("height: %d  \n",test->height);
		printf("width: %d  \n",test->width);
		printf("fill: %d  \n",test->fill);
		printf("---------------------------------------\n");
	}

		if((temp->shape)==3)
	{
		struct text *test = (struct text *) malloc(sizeof(textTyp));
		test = (struct text *)(temp->inhalt);

		printf("\nid: %s  \n",test->id);
		printf("x: %d  \n",test->x);
		printf("y: %d  \n",test->y);
		printf("height: %d  \n",test->font_size);
		printf("fill: %d  \n",test->fill);
		printf("---------------------------------------\n");
	}

		
		if((temp->shape)==4)
	{
		struct circle *test = (struct circle *) malloc(sizeof(circleTyp));
		test = (struct circle *)(temp->inhalt);

		printf("\nid: %s  \n",test->id);
		printf("cx: %d  \n",test->cx);
		printf("cy: %d  \n",test->cy);
		printf("r: %d  \n",test->r);
		printf("fill: %d  \n",test->fill);
		printf("---------------------------------------\n");
	}
	
	while (temp->next != NULL) 
	{
	//Hilfszeiger auf nächstes Element setzen
	temp = temp->next;

	// Daten ausgeben
	//temp->data.id = NULL;
	if((temp->shape)==1)
	{
		/*struct line *test = (struct line *) malloc(sizeof(lineTyp));
		test = (struct line *)(temp->inhalt);*/

		struct line *test = &(struct line*)(temp->inhalt);

		printf("\nid: %s  \n",test->id);
		printf("x1: %d  \n",test->x1);
		printf("x2: %d  \n",test->x2);
		printf("y1: %d  \n",test->y1);
		printf("y2: %d  \n",test->y2);
		printf("---------------------------------------\n");
	}

	if((temp->shape)==2)
	{
		struct rect *test = (struct rect *) malloc(sizeof(rectTyp));
		test = (struct rect *)(temp->inhalt);

		printf("\nid: %s  \n",test->id);
		printf("x: %d  \n",test->x);
		printf("y: %d  \n",test->y);
		printf("height: %d  \n",test->height);
		printf("width: %d  \n",test->width);
		printf("fill: %d  \n",test->fill);
		printf("---------------------------------------\n");
	}

		if((temp->shape)==3)
	{
		struct text *test = (struct text *) malloc(sizeof(textTyp));
		test = (struct text *)(temp->inhalt);

		printf("\nid: %s  \n",test->id);
		printf("x: %d  \n",test->x);
		printf("y: %d  \n",test->y);
		printf("height: %d  \n",test->font_size);
		printf("fill: %d  \n",test->fill);
		printf("---------------------------------------\n");
	}

		
		if((temp->shape)==4)
	{
		struct circle *test = (struct circle *) malloc(sizeof(circleTyp));
		test = (struct circle *)(temp->inhalt);

		printf("\nid: %s  \n",test->id);
		printf("cx: %d  \n",test->cx);
		printf("cy: %d  \n",test->cy);
		printf("r: %d  \n",test->r);
		printf("fill: %d  \n",test->fill);
		printf("---------------------------------------\n");
	}
	}
}
 
du meinst:

Code:
void insert_list(void *daten, int shape) //wird so aufgerufen insert(&lin,1)

"lin" ist eine struct variable von line und wird insert_list übergeben
 
du meinst:

Code:
void insert_list(void *daten, int shape) //wird so aufgerufen insert(&lin,1)

"lin" ist eine struct variable von line und wird insert_list übergeben
Wie ich bereits sagte, ist das dann Unsinn. (wenn es nicht gerade eine globale Variable wäre...)

Es muß ein Zeiger vom Typ struct line sein.

Zeig deinen Code wenn du alleine nicht klar kommst, eigentlich hab ich alles schon gesagt. (soweit meine Glaskugel hier funktioniert)

Gruß
 
Zuletzt bearbeitet:
Hi.

C:
void line_input(int def_x, int def_y)

{

        struct line tmp_line;

....
	insert_list(&tmp_line,1);

}
Wie nun schon wiederholt gesagt: Du kannst nicht die Adresse einer lokalen (automatischen) Variablen in die Liste einfügen. Sowie die line_input Funktion verlassen wird, wird der Speicher der Variablen auf dem Stack automatisch freigegeben. D.h. die Adresse die in die Liste eingetragen wurde ist dann ungültig.

So wäre es richtig:
C:
struct line* tmp_line = malloc(sizeof(struct line));
...
insert_list(tmp_line, 1);
Das Gleiche gilt natürlich für alle anderen Funktionen auch.

Gruß

PS: Wo war denn nun das Problem diesen Codeschnipsel wo die Variable deklariert wird und die insert_list Funktion aufgerufen wird hier zu posten? :confused:
 
Hey Danke für deine Hilfe, jetzt mir so manches klar geworden.

Das Prob ist das es noch immer net geht :(

Und weißt du warum es so ist, wenn ich die liste ausgeben will nur das letzte ausgegeben wird?
 
Hey Danke für deine Hilfe, jetzt mir so manches klar geworden.

Das Prob ist das es noch immer net geht :(

Und weißt du warum es so ist, wenn ich die liste ausgeben will nur das letzte ausgegeben wird?
Es müßte dir doch auffallen, das da irgendwas nicht stimmen kann, wenn die Einrückung der Blöcke nicht übereinstimmt und 2 schließende Klammern auf der gleichen Ebene sind:
C:
void insert_list(void *daten, int shape) //wird so aufgerufen insert(&lin,1)
{
    struct liste *pointerEnd;

    if(start == NULL)
    {
      if((start = (struct liste *) malloc(sizeof(listenTyp))) == NULL)
      {
         printf("Kein Speicherplatz vorhanden für anfang\n");
         return;
      }
		
	  start->next = NULL;
      start->last = NULL;

      // Daten in das erste Element schreiben
	  
	    if(shape==1) //Linie
        {
			//temp->inhalt = malloc(sizeof(temp->inhalt));
		    //temp->inhalt = (struct line*) malloc(sizeof(lineTyp));
			start->inhalt = daten;
			start->shape = 1;
			start->next = NULL;

		if(shape==2) //Rechteck
		{
			start->inhalt = daten;
			start->shape = 2;
			start->next = NULL;

		}

		if(shape==3) //Text
		{
			start->inhalt = daten;
			start->shape = 3;
			start->next = NULL;

		}

		if(shape==4) //Kreis
		{
			start->inhalt = daten;
			start->shape = 4;
			start->next = NULL;

		}
    }

    else // zu welchem if gehört dieses else?
    {
    // ...
	} 
} // welcher Block?
} // wieso auf gleicher Ebene?
Im Grunde kannst du dir die if-Abfragen auch komplett sparen:
C:
struct liste *temp;

if (start == NULL) {
  temp = start = malloc(sizeof (listenTyp));
} else {
  for (temp = start; temp->next != NULL; temp = temp->next) ;

  temp->next = malloc(sizeof (listenTyp));
  temp = temp->next;
}

temp->inhalt = daten;
temp->shape = shape;
temp->next = NULL;
Gruß
 
Zuletzt bearbeitet:
Hallo, entschuldigung wegen meiner schlechter Einrückerei...

Hab das jetzt so wie es sich gehört gemacht ^^ (hoff ich mal)

Das mit den if sparen - danke weiß auch nicht wieso ich das so umständlich und vor allem umsonst geschrieben hab...

jetzt speichert das Programm die verschiedenen structs

Danke für deine Hilfe, hat mir wirklich viel gebracht!!
 
Zurück