Contents
- Arrays as Arguments of Functions
Defining Arrays
- Arrays store in memory with continuous blocks.
- Array has a small unit called elements.
- When defining an array, the number of elements should be an integer greater than 0.
int Array [10]; // 10 is the number of elements
// and it can not be 0
- An array can be defined as variable-length or fixed-length arrays
- Fixed-length Array can be initialized with {0} but Variable-length Array not.
int FixedLenArray[10] = {0}; // It can be initialized with {0}
int VariableLenArray[Len]; // It can not be initialized
- Fixed-length Array can be in any scopes but can not be a function parameter. There is a way to make this possible (please check chapter 7).
void
ArraySize (int Array[]) // declare as incomplete type or a pointer
{
// The Array in the function
// has been converted to pointer
printf ("In Parameter : %d\n", sizeof (Array));
}
int main ()
{
int Array[5] = {1,2,3,4,5};
// In the scope of original defined place
printf ("In Origin : %d\n", sizeof (Array));
ArraySize (Array);
return 0;
}
In Origin : 20
In Parameter : 4
- The length of Variable-Length Arrays must be ordinary identifiers or members of struct or union (not tags, lable).
- Variable-Length Arrays are like the malloc(). When you enter the function, it will allocate spaces on the stack and release like free () when exit the function.
void ArrayNumWithVariable (int Num)
{
short Array2[Num]; // Variable-length, can not be initialized
printf ("Size of Array2 : %d (bytes)\n", sizeof (Array2));
printf ("Members of Array2 : %d\n", sizeof (Array2) / sizeof (short));
}
int
ArrayLen ()
{
return 50;
}
int main ()
{
int Num = 10;
int Array[5] = {0}; // Fixed-length, can be initialized
int Array3[ArrayLen()]; // Variable-length, can not be initialized
// when using only operator "sizeof", it only get the size of
// the array
printf ("Size of Array : %d (bytes)\n", sizeof (Array));
// if you want to get the member og the array, you must divide the
// size of type after you get the size of entire array
printf ("Members of Array : %d\n", sizeof (Array) / sizeof (int));
ArrayNumWithVariable (Num);
printf ("Size of Array3 : %d (bytes)\n", sizeof (Array3));
printf ("Members of Array3 : %d\n", sizeof (Array3) / sizeof (int));
return 0;
}
Size of Array : 20 (bytes)
Members of Array : 5
Size of Array2 : 20 (bytes)
Members of Array2 : 10
Size of Array3 : 200 (bytes)
Members of Array3 : 50
Accessing Array Elements
- It is permissible to read/write an array even if the index is larger than the defined number of elements. But it may make the program crash.
int Array[5] = {0};
Array[10] = 0; // This is permissible but may crash
- The array variable will be convert to pointer if it has only the variable name, expect sizeof
int Array[5] = {[3] = 5};
// The result will be the same
printf ("Array[3] : %d\n", Array[3]);
printf ("*(Array + 3) : %d\n", *(Array + 3));
Array[3] : 5
*(Array + 3) : 5
Initializing Arrays
- Variable-length array can not be initialized
- Static array can only initialized by constants. Because the variable will be save to a fixed space in the program.
- You can define array without the number of elements. But it must has initializers.
// The array must has initializers to
// define the size.
int Array[] = {1,2,3,4,5};
- If you have the length and with some initializers, the others are initialized to 0
// Other elements after Array[3] will be 0
int Array[50] = {5,6,7,8};
- If there are more initializers or comma(,) than the length, they will be ignored. The array size of the following example will be all the same.
// Elements after 6 will be ignored
int Array[5] = {1,2,3,4,5,6,7,8};
// Last comma will be ignored
int Array2[5] = {1,2,3,4,5,};
// The elements of Array3 will be 5
int Array3[] = {1,2,3,4,5,};
// The value of the array will be
// 1, 0, 0, 5, 0, 0
int Array[5] = {1,[3]=5};
- The element designators can only be a constant but the value it assigned can be a variable.
// The value of the array will be
// 1, 0, 0, 10, 0, 0
int Len = 10;
int Array[5] = {1,[3]=Len};
- If the array is without length, the value of element designators can be the length (only available in 1 dim array)
// The value of the array will be
// 0, 0, 0, 0, 0, 0, 6, 7
// 8 elements
int Array[] = {[6]=6, 7};
Strings
- A string array must has a null character '\0' at the end
char Array[] = "I am m7821";
printf ("String Length : %d\n", strlen (Array));
printf ("String Size : %d\n", sizeof (Array));
String Length : 10
String Size : 11
Multidimensional Arrays
- In multidimensional arrays, only the length of the array can be incomplete all other elements should be a specific number
// They are all [3][2][3] arrays
int Array[][2][3] = {{0},{0},{0}};
int Array2[][2][3] = {[2]={0}};
void
DumpMem (void *Buffer, int Length)
{
int Index;
uint8_t *TmpBuf = (uint8_t *) Buffer;
printf ("\nDump memory from 0x%08x\n", Buffer);
for (Index = 0; Index < Length; Index++) {
if (Index % 0x10 == 0)
printf ("0x%08x : ", &TmpBuf[Index]);
printf ("%02x ", TmpBuf[Index]);
if (Index % 0x10 == 0xf)
printf ("\n");
}
printf ("\n");
}
int main ()
{
int Array[5][5];
int *ArrPtr = (int *)Array;
int Row, Col, Num = 0;
for (Row = 0; Row < 5; Row++)
for (Col = 0; Col < 5; Col++)
Array[Row][Col] = Num++;
printf ("ArrPtr[5] = %d, Address : 0x%08x\n", ArrPtr[5], &ArrPtr[5]);
printf ("Array[1][0] = %d, Address : 0x%08x\n", Array[1][0], &Array[1][0]);
DumpMem (ArrPtr, sizeof (Array));
return 0;
}
ArrPtr[5] = 5, Address : 0x0028fe80
Array[1][0] = 5, Address : 0x0028fe80
Dump memory from 0x0028fe6c
0x0028fe6c : 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00
0x0028fe7c : 04 00 00 00 05 00 00 00 06 00 00 00 07 00 00 00
0x0028fe8c : 08 00 00 00 09 00 00 00 0a 00 00 00 0b 00 00 00
0x0028fe9c : 0c 00 00 00 0d 00 00 00 0e 00 00 00 0f 00 00 00
0x0028feac : 10 00 00 00 11 00 00 00 12 00 00 00 13 00 00 00
0x0028febc : 14 00 00 00 15 00 00 00 16 00 00 00 17 00 00 00
0x0028fecc : 18 00 00 00
int main ()
{
int Array[5][5][5];
int Row, Col, Height;
for (Row = 0; Row < 5; Row++)
for (Col = 0; Col < 5; Col++)
for (Height = 0; Height < 5; Height++)
Array[Row][Col][Height] = Row | Col << 8 | Height << 16;
DumpMem (Array, sizeof (Array));
return 0;
}
Dump memory from 0x0028fce0
0x0028fce0 : 00 00 00 00 00 00 01 00 00 00 02 00 00 00 03 00
0x0028fcf0 : 00 00 04 00 00 01 00 00 00 01 01 00 00 01 02 00
0x0028fd00 : 00 01 03 00 00 01 04 00 00 02 00 00 00 02 01 00
0x0028fd10 : 00 02 02 00 00 02 03 00 00 02 04 00 00 03 00 00
0x0028fd20 : 00 03 01 00 00 03 02 00 00 03 03 00 00 03 04 00
0x0028fd30 : 00 04 00 00 00 04 01 00 00 04 02 00 00 04 03 00
0x0028fd40 : 00 04 04 00 01 00 00 00 01 00 01 00 01 00 02 00
0x0028fd50 : 01 00 03 00 01 00 04 00 01 01 00 00 01 01 01 00
0x0028fd60 : 01 01 02 00 01 01 03 00 01 01 04 00 01 02 00 00
0x0028fd70 : 01 02 01 00 01 02 02 00 01 02 03 00 01 02 04 00
0x0028fd80 : 01 03 00 00 01 03 01 00 01 03 02 00 01 03 03 00
0x0028fd90 : 01 03 04 00 01 04 00 00 01 04 01 00 01 04 02 00
0x0028fda0 : 01 04 03 00 01 04 04 00 02 00 00 00 02 00 01 00
0x0028fdb0 : 02 00 02 00 02 00 03 00 02 00 04 00 02 01 00 00
0x0028fdc0 : 02 01 01 00 02 01 02 00 02 01 03 00 02 01 04 00
0x0028fdd0 : 02 02 00 00 02 02 01 00 02 02 02 00 02 02 03 00
0x0028fde0 : 02 02 04 00 02 03 00 00 02 03 01 00 02 03 02 00
0x0028fdf0 : 02 03 03 00 02 03 04 00 02 04 00 00 02 04 01 00
0x0028fe00 : 02 04 02 00 02 04 03 00 02 04 04 00 03 00 00 00
0x0028fe10 : 03 00 01 00 03 00 02 00 03 00 03 00 03 00 04 00
0x0028fe20 : 03 01 00 00 03 01 01 00 03 01 02 00 03 01 03 00
0x0028fe30 : 03 01 04 00 03 02 00 00 03 02 01 00 03 02 02 00
0x0028fe40 : 03 02 03 00 03 02 04 00 03 03 00 00 03 03 01 00
0x0028fe50 : 03 03 02 00 03 03 03 00 03 03 04 00 03 04 00 00
0x0028fe60 : 03 04 01 00 03 04 02 00 03 04 03 00 03 04 04 00
0x0028fe70 : 04 00 00 00 04 00 01 00 04 00 02 00 04 00 03 00
0x0028fe80 : 04 00 04 00 04 01 00 00 04 01 01 00 04 01 02 00
0x0028fe90 : 04 01 03 00 04 01 04 00 04 02 00 00 04 02 01 00
0x0028fea0 : 04 02 02 00 04 02 03 00 04 02 04 00 04 03 00 00
0x0028feb0 : 04 03 01 00 04 03 02 00 04 03 03 00 04 03 04 00
0x0028fec0 : 04 04 00 00 04 04 01 00 04 04 02 00 04 04 03 00
0x0028fed0 : 04 04 04 00
int main ()
{
int Array[2][3][4][5];
int Row, Col, Height, Dim4;
for (Row = 0; Row < 2; Row++)
for (Col = 0; Col < 3; Col++)
for (Height = 0; Height < 4; Height++)
for (Dim4 = 0; Dim4 < 5; Dim4++)
Array[Row][Col][Height][Dim4] = Row | Col << 8 | Height << 16 | Dim4 << 24;
DumpMem (Array, sizeof (Array));
return 0;
}
Dump memory from 0x0028fcf0
0x0028fcf0 : 00 00 00 00 00 00 00 01 00 00 00 02 00 00 00 03
0x0028fd00 : 00 00 00 04 00 00 01 00 00 00 01 01 00 00 01 02
0x0028fd10 : 00 00 01 03 00 00 01 04 00 00 02 00 00 00 02 01
0x0028fd20 : 00 00 02 02 00 00 02 03 00 00 02 04 00 00 03 00
0x0028fd30 : 00 00 03 01 00 00 03 02 00 00 03 03 00 00 03 04
0x0028fd40 : 00 01 00 00 00 01 00 01 00 01 00 02 00 01 00 03
0x0028fd50 : 00 01 00 04 00 01 01 00 00 01 01 01 00 01 01 02
0x0028fd60 : 00 01 01 03 00 01 01 04 00 01 02 00 00 01 02 01
0x0028fd70 : 00 01 02 02 00 01 02 03 00 01 02 04 00 01 03 00
0x0028fd80 : 00 01 03 01 00 01 03 02 00 01 03 03 00 01 03 04
0x0028fd90 : 00 02 00 00 00 02 00 01 00 02 00 02 00 02 00 03
0x0028fda0 : 00 02 00 04 00 02 01 00 00 02 01 01 00 02 01 02
0x0028fdb0 : 00 02 01 03 00 02 01 04 00 02 02 00 00 02 02 01
0x0028fdc0 : 00 02 02 02 00 02 02 03 00 02 02 04 00 02 03 00
0x0028fdd0 : 00 02 03 01 00 02 03 02 00 02 03 03 00 02 03 04
0x0028fde0 : 01 00 00 00 01 00 00 01 01 00 00 02 01 00 00 03
0x0028fdf0 : 01 00 00 04 01 00 01 00 01 00 01 01 01 00 01 02
0x0028fe00 : 01 00 01 03 01 00 01 04 01 00 02 00 01 00 02 01
0x0028fe10 : 01 00 02 02 01 00 02 03 01 00 02 04 01 00 03 00
0x0028fe20 : 01 00 03 01 01 00 03 02 01 00 03 03 01 00 03 04
0x0028fe30 : 01 01 00 00 01 01 00 01 01 01 00 02 01 01 00 03
0x0028fe40 : 01 01 00 04 01 01 01 00 01 01 01 01 01 01 01 02
0x0028fe50 : 01 01 01 03 01 01 01 04 01 01 02 00 01 01 02 01
0x0028fe60 : 01 01 02 02 01 01 02 03 01 01 02 04 01 01 03 00
0x0028fe70 : 01 01 03 01 01 01 03 02 01 01 03 03 01 01 03 04
0x0028fe80 : 01 02 00 00 01 02 00 01 01 02 00 02 01 02 00 03
0x0028fe90 : 01 02 00 04 01 02 01 00 01 02 01 01 01 02 01 02
0x0028fea0 : 01 02 01 03 01 02 01 04 01 02 02 00 01 02 02 01
0x0028feb0 : 01 02 02 02 01 02 02 03 01 02 02 04 01 02 03 00
0x0028fec0 : 01 02 03 01 01 02 03 02 01 02 03 03 01 02 03 04
- Another way to pass multi-dim array
void
ArrayTest (int (*Array)[])
{
int Col;
for (Col = 0; Col < 5; Col++)
printf ("%02d, ", (*Array)[Col]);
printf ("\n");
}
int main ()
{
int Row, Col;
int Array[5][5] = {0}, Num=0;
for (Row = 0; Row < 5; Row++)
for (Col = 0; Col < 5; Col++)
Array[Row][Col] = Num++;
ArrayTest (Array);
ArrayTest (&Array[1]);
ArrayTest (&Array[2]);
ArrayTest (&Array[3]);
ArrayTest (&Array[4]);
return 0;
}
00, 01, 02, 03, 04,
05, 06, 07, 08, 09,
10, 11, 12, 13, 14,
15, 16, 17, 18, 19,
20, 21, 22, 23, 24,
Reference :
C in a nutshell
0 意見:
張貼留言