Pointers,Structs,Unions
char* p
-address to single char value / first in array
char* argv[]
-array of type char* with unknown
char** argv
-address to the first element of an array of type char*
int** data
1. Pointer to pointer to single int value
2. Array of addresses that point to single int
3. Address that points to one array of int values
4. Array of addresses that point to arrays of int values
Sizeof(void*) returns size of address
Include limits.h for supporting max and min values with TYPE_MAX/MIN
Const avoids value being modified, would be illegal if attempting to change so
Enums - maps to int, associates name with value
EXAMPLE:
enum month_name{
Jan,Feb,Mar.......,month_undef
};
Structs
Definition:
Struct date{ //date is the tag
enum day_name day;
Int day_num;
Enum month_name month;
Int year;
} Big_Day { //declaration
Mon,7,Jan,1980 //initialization
};
Function arguments/returns for structs
EXAMPLE:
Struct cus s1;
Struct sal s2;
Struct sale transact(struct cus s1,struct sal s2){
//actions
Return s1;
}
Initializing:
Struct date deadline = {day_undef,1,Jan,2000};
Struct date* mydate = &deadline;
Typedef
typedef struct date{
Enum day_name day;
Int day_num;
Enum month_name month;
Int year;
}Date;
Memory alignment
The max width of the struct depends on the longest
The padding pattern depends on the order of variables
Address of struct give access to first member
Alignment also depends on architecture, compiler extensions can be used to prevent padding
Union
Variants of structs use same memory
Accessing of elements of union uses same notation as struct
If variants exist in union, eg. Book and film example, use separate variable to indicate which variant in use
Comments
Post a Comment