-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc.txt
executable file
·46 lines (33 loc) · 1.25 KB
/
c.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
UNDERSTANDING STRUCTS
---------------------
Previously i thought that the were equivalent. ie. I thought an array was just a pointer to the first element.
struct HooStruct1 {
UInt32 mNumberOfFloats;
CGFloat arggg[1024];
};
struct HooStruct2 {
UInt32 mNumberOfFloats;
CGFloat *arggg;
};
I was wrong. HooStruct1 allocates the memory as well
NSUInteger test1 = sizeof(struct HooStruc1); // == 4100
NSUInteger test2 = sizeof(struct HooStruc2); // == 8
Sooo, you can use this to define a variable size array. Like so..
struct HooStruct3 {
UInt32 mNumberOfFloats;
CGFloat arggg[1]; // This is key.. must come last in struct. Size of 1, then when we malloc we can multiply it by the quantity we want
};
int arraySize = 50;
struct HooStruct3 *arrayOf50;
arrayOf50 = malloc( sizeof *arrayOf50 + (arraySize - 1) * sizeof arrayOf50->arggg[0] );
arrayOf50->mNumberOfFloats = arraySize;
...
free( arrayOf50 );
** -- **
char (*j)[20]; // j is a pointer to an array of 20 char
j = (char(*)[20])malloc(20);
const int * grape; // object is readonly
int const * grape; // object is readonly
int * const grape_jelly; // pointer is readonly
const int * const grape_jam; // object and pointer readonly
int const * const grape_jam; // object and pointer readonly