שאלה קטנה על struct בשפת c;

  • פותח הנושא
  • פורסם בתאריך

New member
שאלה קטנה על struct בשפת c;

מדוע ההקצאה שביצעתי לא עובדת (הוא רושם שיש typecasting מvoid* לstruct monom ) ?
typedef struct MONOM{ int mekadem; int power; }monom; void main(void) { monom first; first = (monom) malloc(sizeof(monom)); if(first == NULL) { printf("failed to allocate memory! bye bye..."); exit(1); } }​
 

sadov

New member
malloc

הבעיה לא בstruct שלך. הפוקצייה malloc מחזירה פוינטר מסוג * void (הכתובת בזיכרון למשתנה החדש)ואתה מבצע casting על הכתובת למשתנה מסוג monom. first צריך להיות פוינטר ל monom*) monom ) וכך גם ה casting.
 
למעלה