当前位置:网站首页>C language array and pointer exercises

C language array and pointer exercises

2022-06-11 03:07:00 shlyyy


Preface

Understanding C Language pointer Based on this article , Do the following exercises to better master the knowledge of arrays and pointers .


7、 ... and 、 practice

The array name indicates the address of the first element of the array

But there are 2 Exceptions :

  1. sizeof( Array name ), The array name represents the entire array , The total size of the array is calculated , Unit is byte .
  2. & Array name , The array name represents the entire array ,& Array name It takes out the address of the entire array

besides , All array names encountered represent the address of the first element of the array .

*(arr + n) = arr[n]

7.1 One dimensional array

void test1()
{
    
    int a[] = {
     1,2,3,4 };
    printf("%d\r\n", sizeof(a)); 
    printf("%d\r\n", sizeof(a + 0));
    printf("%d\r\n", sizeof(*a));
    printf("%d\r\n", sizeof(a + 1));
    printf("%d\r\n", sizeof(a[1]));
    printf("%d\r\n", sizeof(&a));
    printf("%d\r\n", sizeof(*&a));
    printf("%d\r\n", sizeof(&a + 1));
    printf("%d\r\n", sizeof(&a[0]));
    printf("%d\r\n", sizeof(&a[0] + 1));
}

void test1()
{
    
    int a[] = {
     1,2,3,4 };

	// sizeof( Array name ) The total size of the array is calculated 
    printf("%d\r\n", sizeof(a)); // 16
    
    //  Array name a Is the address of the first element of the array ,32 The address of the bit program is 4 byte 
    printf("%d\r\n", sizeof(a + 0)); // 4
    
    //  Array name a Is the address of the first element of the array ,*a Get the first element of the array 1, The type is int size 4 byte 
    // *(arr + n) = arr[n]  therefore  *a=*(a+0)=a[0]
    printf("%d\r\n", sizeof(*a)); // 4
    
    //  Array name a Is the address of the first element of the array ,a+1 Get the address of the next element , The address is 4 Byte size 
    printf("%d\r\n", sizeof(a + 1)); // 4
    
    //  The array element type is int size 4 byte 
    printf("%d\r\n", sizeof(a[1])); // 4
    
    // & The array name represents the address of the entire array , The address is 4 Byte size 
    printf("%d\r\n", sizeof(&a)); // 4
    
    // &a For the address of the entire array , Solve the star to get the whole array , amount to * And & Offset , The array size is 16 byte 
    printf("%d\r\n", sizeof(*&a)); // 16
    
    // &a For the address of the entire array , Add 1 Get past the array a Address after , Address size 4 byte 
    printf("%d\r\n", sizeof(&a + 1)); // 4
    
    //  Address of the first element of the array , Address size 4 byte 
    printf("%d\r\n", sizeof(&a[0])); // 4
 
    //  Address of the first element of the array , The type is int*, Add 1 Represents the address of the next element , Address size 4 byte 
    printf("%d\r\n", sizeof(&a[0] + 1)); // 4
}

7.2 A character array 1

void test2()
{
    
    char arr[] = {
     'a','b','c','d','e','f' };
    printf("%d\n", sizeof(arr));
    printf("%d\n", sizeof(arr + 0));
    printf("%d\n", sizeof(*arr));
    printf("%d\n", sizeof(arr[1]));
    printf("%d\n", sizeof(&arr));
    printf("%d\n", sizeof(&arr + 1));
    printf("%d\n", sizeof(&arr[0] + 1));

    printf("%d\n", strlen(arr));
    printf("%d\n", strlen(arr + 0));
    printf("%d\n", strlen(*arr));
    printf("%d\n", strlen(arr[1]));
    printf("%d\n", strlen(&arr));
    printf("%d\n", strlen(&arr + 1));
    printf("%d\n", strlen(&arr[0] + 1));
}

Reference resources :

void test2()
{
    
    char arr[] = {
     'a','b','c','d','e','f' };
    
    // sizeof( Array name ) Calculate the size of the entire array 
    printf("%d\n", sizeof(arr)); // 6
    
    //  Array name arr Represents the address of the first element of the array , Add 0 The address of the first element of the array , Address 4 Byte size 
    printf("%d\n", sizeof(arr + 0)); // 4
    
    // arr Represents the address of the first element of the array ,*arr Get the first element of the array a, The type is char size 1 byte 
    printf("%d\n", sizeof(*arr)); // 1
    
    //  The array index is zero 1 The element of is b, The type is char size 1 byte 
    printf("%d\n", sizeof(arr[1])); // 1
    
    // &arr For the address of the entire array , Address 4 Byte size 
    printf("%d\n", sizeof(&arr)); // 4
    
    // &arr For the address of the entire array , Add 1 Get the address across the entire array , Address size 4 byte 
    printf("%d\n", sizeof(&arr + 1)); // 4
    
    //  Address of the first element of the array , Add 1 Get the address of the next element , Address size 4 byte 
    printf("%d\n", sizeof(&arr[0] + 1)); // 4

    
    // arr Address of the first element of the array , because '\0' The location is unknown , Here is a random number 
    printf("%d\n", strlen(arr)); //  random number , Test for 19
    
    //  Array name arr Represents the address of the first element of the array , Add 0 The address of the first element of the array , because '\0' The location is unknown , Here is a random number 
    printf("%d\n", strlen(arr + 0)); //  random number , Test for 19
    
    //  Array name arr Represents the address of the first element of the array ,*arr Get the first element a,a As strlen Parameters of , The calculated address is a Of ascii Code value 97=0x61 String length at , A memory access exception error will appear ,error
    printf("%d\n", strlen(*arr));

    //  ditto , The calculated address is b Of ascii Code value 98=0x62 String length at , A memory access exception occurred ,error
    printf("%d\n", strlen(arr[1]));
    
    // &arr Get the address of the entire array , because '\0' The location is unknown , Here is a random number 
    printf("%d\n", strlen(&arr)); //  random number , Test for 19
    
    // &arr Get the address of the entire array , Add 1 Get the address across the entire array , because '\0' The location is unknown , Here is a random number 
    printf("%d\n", strlen(&arr + 1)); //  random number , Test for 13
    
    //  Address of the first element of the array , Add 1 Get the address of the next element , because '\0' The location is unknown , Here is a random number 
    printf("%d\n", strlen(&arr[0] + 1)); //  random number , Test for 18
}

7.3 A character array 2

void test3()
{
    
    char arr[] = "abcdef";
    printf("%d\n", sizeof(arr));
    printf("%d\n", sizeof(arr + 0));
    printf("%d\n", sizeof(*arr));
    printf("%d\n", sizeof(arr[1]));
    printf("%d\n", sizeof(&arr));
    printf("%d\n", sizeof(&arr + 1));
    printf("%d\n", sizeof(&arr[0] + 1));

    printf("%d\n", strlen(arr));
    printf("%d\n", strlen(arr + 0));
    printf("%d\n", strlen(*arr));
    printf("%d\n", strlen(arr[1]));
    printf("%d\n", strlen(&arr));
    printf("%d\n", strlen(&arr + 1));
    printf("%d\n", strlen(&arr[0] + 1));
}

Reference resources :

void test3()
{
    
    char arr[] = "abcdef"; //  The string is in the stack area , It automatically includes '\0'
    
    // sizeof( Array name ) Calculate the size of the entire array  
    printf("%d\n", sizeof(arr)); // 7
    
    //  The array name is the address of the first element of the array , Add 0 Or the address of the first element , Address size 4 byte 
    printf("%d\n", sizeof(arr + 0)); // 4
    
    // arr Represents the address of the first element of the array ,*arr Get the first element of the array a, The type is char size 1 byte 
    printf("%d\n", sizeof(*arr)); // 1
    
    //  The array index is zero 1 The element of is b, The type is char size 1 byte 
    printf("%d\n", sizeof(arr[1])); // 1
    
    // &arr For the address of the entire array , Address 4 Byte size 
    printf("%d\n", sizeof(&arr)); // 4
    
    // &arr For the address of the entire array , Add 1 Get the address across the entire array , Address size 4 byte 
    printf("%d\n", sizeof(&arr + 1)); // 4
    
    //  Address of the first element of the array , Add 1 Get the address of the next element , Address size 4 byte 
    printf("%d\n", sizeof(&arr[0] + 1)); // 4

    
    // arr Address of the first element of the array ,strlen encounter '\0' end , The resulting length does not include '\0'
    printf("%d\n", strlen(arr)); // 6
    
    //  Array name arr Represents the address of the first element of the array , Add 0 The address of the first element of the array ,strlen encounter '\0' end , The resulting length does not include '\0'
    printf("%d\n", strlen(arr + 0)); // 6
    
    //  Array name arr Represents the address of the first element of the array ,*arr Get the first element a,a As strlen Parameters of , The calculated address is a Of ascii Code value 97=0x61 String length at , A memory access exception error will appear ,error
    printf("%d\n", strlen(*arr)); // error
    
    //  ditto , The calculated address is b Of ascii Code value 98=0x62 String length at , A memory access exception occurred ,error
    printf("%d\n", strlen(arr[1])); // error
    
    // &arr Get the address of the entire array ,strlen encounter '\0' end , The resulting length does not include '\0'
    printf("%d\n", strlen(&arr)); // 6
    
    // &arr Get the address of the entire array , Add 1 Get the address across the entire array , because '\0' The location is unknown , Here is a random number 
    printf("%d\n", strlen(&arr + 1)); //  random number , Test for 12
    
    //  Address of the first element of the array , Add 1 Get the address of the next element ,strlen encounter '\0' end , The resulting length does not include '\0'
    printf("%d\n", strlen(&arr[0] + 1)); // 5
}

7.4 String pointer

void test4()
{
    
    char* p = "abcdef";
    printf("%d\n", sizeof(p));
    printf("%d\n", sizeof(p + 1));
    printf("%d\n", sizeof(*p));
    printf("%d\n", sizeof(p[0]));
    printf("%d\n", sizeof(&p));
    printf("%d\n", sizeof(&p + 1));
    printf("%d\n", sizeof(&p[0] + 1));

    printf("%d\n", strlen(p));
    printf("%d\n", strlen(p + 1));
    printf("%d\n", strlen(*p));
    printf("%d\n", strlen(p[0]));
    printf("%d\n", strlen(&p));
    printf("%d\n", strlen(&p + 1));
    printf("%d\n", strlen(&p[0] + 1));
}

Reference resources :

void test4()
{
    
    char* p = "abcdef";
    
    // p Point to the first address of the string , Address size 4 byte 
    printf("%d\n", sizeof(p)); // 4
    
    // p Point to the first address of the string , Add 1 Point to the next element address , Address size 4 byte 
    printf("%d\n", sizeof(p + 1)); // 4
    
    // p Point to the first address of the string ,*p Get the first element of the string , The type is char size 1 byte .*p=*(p+0)=p[0]
    printf("%d\n", sizeof(*p)); // 1
    
    //  The first element of a string , The type is char size 1 byte 
    printf("%d\n", sizeof(p[0])); // 1
    
    // &p Get the pointer variable p Address in the stack , Address size 4 byte 
    printf("%d\n", sizeof(&p)); // 4
    
    // &p Get the pointer variable p Address in the stack , Add 1 Get in stack p Next address for , The type is char** size 4 byte 
    printf("%d\n", sizeof(&p + 1)); // 4
    
    //  The address of the first element of the string , Add 1 Get the address of the next element , The type is char* size 4 byte 
    printf("%d\n", sizeof(&p[0] + 1)); // 4
    
    
	// p Point to the first address of the string ,strlen encounter '\0' end , The resulting length does not include '\0'
    printf("%d\n", strlen(p)); // 6
    
    // p Point to the first address of the string , Add 1 Get the address of the next element , Point to b
    printf("%d\n", strlen(p + 1)); // 5
    
    // p Point to the first address of the string ,*p Get the first element of the string a,a As strlen Parameters of , The calculated address is a Of ascii Code value 97=0x61 String length at , A memory access exception error will appear ,error
    printf("%d\n", strlen(*p)); // error
    
    //  ditto , The calculated address is b Of ascii Code value 98=0x62 String length at , A memory access exception occurred ,error
    printf("%d\n", strlen(p[0])); // error
    
    // &p Get the pointer variable p Address in the stack , because '\0' The location is unknown , Here is a random number 
    printf("%d\n", strlen(&p)); //  random number , Test for 3
    
    // &p Get the pointer variable p Address in the stack , Add 1 Get in stack p Next address for , because '\0' The location is unknown , Here is a random number 
    printf("%d\n", strlen(&p + 1)); //  random number , Test for 11
    
    //  The address of the first element of the string , Add 1 Get the address of the next element , namely b The address of 
    printf("%d\n", strlen(&p[0] + 1)); // 5
}

7.5 Two dimensional array

void test5()
{
    
    int a[3][4] = {
     0 };
    printf("%d\n", sizeof(a));
    printf("%d\n", sizeof(a[0][0]));
    printf("%d\n", sizeof(a[0]));
    printf("%d\n", sizeof(a[0] + 1));
    printf("%d\n", sizeof(*(a[0] + 1)));
    printf("%d\n", sizeof(a + 1));
    printf("%d\n", sizeof(*(a + 1)));
    printf("%d\n", sizeof(&a[0] + 1));
    printf("%d\n", sizeof(*(&a[0] + 1)));
    printf("%d\n", sizeof(*a));
    printf("%d\n", sizeof(a[3]));
}

Reference resources :

void test5()
{
    
    int a[3][4] = {
     0 };
    
    // sizeof( Array name ) Calculate the size of the entire array  
    printf("%d\n", sizeof(a)); // 48
    
    //  Two dimensional array a The first element of is a[0], One dimensional array a[0] First element of a[0][0] type int size 4 byte 
    printf("%d\n", sizeof(a[0][0])); // 4
    
    // a[0] Is the first element of a two-dimensional array , It is also a one-dimensional array , The type is int[4],sizeof( Array name ) Calculate the size of the entire array 
    printf("%d\n", sizeof(a[0])); // 16
    
    // a[0] It's a one-dimensional array , The array name indicates the address of the first element of the array , Add 1 Represents the address of the next element , Equivalent to &a[0][0]+1, namely a[0][1] The address of 
    printf("%d\n", sizeof(a[0] + 1)); // 4
    
    // a[0] It's a one-dimensional array , The name of the array is the address of the first element of the array , Add 1 Get the address of the next element ,* Get the value corresponding to the address , namely a[0][1] Place the value of the , The type is int size 4 byte 
    printf("%d\n", sizeof(*(a[0] + 1))); // 4
    
    //  2D array name a Represents the first element of an array a[0] The address of , Add 1 Represents an array a The address of the next element , namely a[1] The address of 
    printf("%d\n", sizeof(a + 1)); // 4
    
    // a+1 obtain a[1] The address of ,* Get a one-dimensional array a[1], Among them is 4 individual int Type element ,sizeof( Array name ) Calculate the size of the entire array . amount to sieof(a[1])
    printf("%d\n", sizeof(*(a + 1))); // 16
    
    // a[0] It's a one-dimensional array ,&a[0] Get the address of the whole array , Add 1 Get over a one-dimensional array a[0] The address of , namely a[1] The address of 
    printf("%d\n", sizeof(&a[0] + 1)); // 4
    
    // &a[0]+1 obtain a[1] The address of ,* Get a one-dimensional array a[1], Among them is 4 individual int Type element ,sizeof( Array name ) Calculate the size of the entire array .
    printf("%d\n", sizeof(*(&a[0] + 1))); // 16
    
    //  2D array name a Represents the first element of an array a[0] The address of ,* Get the first element of a one-dimensional array a[0], Among them is 4 individual int Type element ,sizeof( Array name ) Calculate the size of the entire array .*a=*(a+0)=a[0] sizeof(*a)=sizeof(a[0])
    printf("%d\n", sizeof(*a)); // 16
    
    // a[3] The type of int[4]
    printf("%d\n", sizeof(a[3])); // 16
}

7.6 Array pointer

Array pointers can receive two-dimensional arrays . Understand the above two-dimensional array , The following questions are not very difficult , So I won't write an analysis , Those who are interested can analyze by themselves .

Main application :*(arr + n) = arr[n]

void test6()
{
    
    int nAry[2][4] = {
    
        {
    10,20,30,40},
        {
    60,70,80,90}
    };

    int(*p)[4] = nAry;

    printf("%p\r\n", p);
    printf("%p\r\n", *p);
    printf("%p\r\n", (void*)**p);


    printf("%p\r\n", (void*)sizeof(p));
    printf("%p\r\n", (void*)sizeof(*p));
    printf("%p\r\n", (void*)sizeof(**p));


    printf("%p\r\n", p + 1);
    printf("%p\r\n", *p + 1);
    printf("%p\r\n", (void*)(**p + 1));


    printf("%p\r\n", p[1] + 1);

    printf("%p\r\n", (void*)*((p + 1)[1]));
    printf("%p\r\n", (void*)(*(p + 1))[1]);

    //  Subscript operation has priority over * Operation has high priority 
    printf("%p\r\n", (void*)(*p[1] + 1));
    printf("%p\r\n", (void*)(p[1] + 1)[1]);
}
原网站

版权声明
本文为[shlyyy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110242500022.html