04-11-2007, 04:03
|
|
|
חבר מתאריך: 17.05.05
הודעות: 7,321
|
|
[בשפת C] אי-הבנה של גדלי סוגי משתנים
הרצתי את התכנית הבאה, התוצאות רשומות בצורת הערה:
קוד:
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
int a;
float arr[10];
char c;
}struct_type;
struct_type s;
int main(array<System::String ^> ^args)
{
char c;
int n;
double d;
printf("Size of type int is: %d\n", sizeof(int)); //4 bytes
printf("Size of type char is: %d\n", sizeof(char)); // 1 byte
printf("size of type double is: %d\n", sizeof(double)); //8 bytes
printf("size of type struct_type is:%d\n\n", sizeof(struct_type)); // 48 bytes
printf("Size of variable n is: %d\n", sizeof(n)); // 4 bytes
printf("Size of variable c is: %d\n", sizeof(c)); // 1 byte
printf("Size of variable d is: %d\n", sizeof(d)); //8 bytes
printf("Size of variable sis: %d\n", sizeof(s)); //48 bytes
getch();
}
אני לא מבין:
(1) איך זה שסוג int הוא בגודל 4 בתים(!) כשהוא צריך להיות 2 בתים.
(2) למה גודל ה-struct הוא 48 בתים? איך הגיעו לזה? לפי החישוב שלי זה צריך להיות:
קוד:
4 + 4 * 10 + 1 = 45
4 -> int
4 * 10 -> float arr[10]
1 -> char
(3) למה כשאני מוסיף ל-struct עוד משתנים מסוג char, גודל ה-struct אינו משתנה, אך כשאני מוסיף משתנים מסוג double, גודל ה-struct כן עולה?
תודה רבה!
כנראה בהמשך אפציץ אותכם בשאלות נוספות
נערך לאחרונה ע"י DCD בתאריך 04-11-2007 בשעה 04:05.
|