A Wei, come in and do the written test of C language array. I heard you are very brave

📌 Author: Foxny

📃 Update record: July 8, 2021

❌ Corrigendum record: None

📜 This article states: due to the limited level of the author, it is inevitable that there are errors and inaccuracies in this article. I also want to know these errors. I sincerely hope the readers can criticize and correct them!

📝 Example:

🔍 Topic explanation display:

Array written test questions (answer + detailed explanation)

8 big questions (63 small questions in total), 1 point for each small question, the full score is 63 points

📚 explain:

① It is suggested to take out paper and pen to write the results you think;

② It is suggested not to look at the answers first, and then look at the answers for verification after writing;

③ There are hyperlinks in front of some topics corresponding to knowledge points, you can choose to review by yourself;

④ For the wrong question, you can see the analysis part below the answer to the question for in-depth understanding;

⑤ You can reply to several questions you have done correctly in the comment area;

First question:

1 point for each sub question, full score 7 points

📌 review: [vitamin C language] Chapter 5 - operators (0x05 type length of operand sizeof)

💬 Predict the running result of the following code (sizeof)

int main()
{
    int a[] = {1, 2, 3, 4}; // One dimensional array

    /* 1 */  printf("%d\n", sizeof(a));
    /* 2 */  printf("%d\n", sizeof(a + 0));
    /* 3 */  printf("%d\n", sizeof(*a));
    /* 4 */  printf("%d\n", sizeof(a + 1));
    /* 5 */  printf("%d\n", sizeof(a[1]));
    /* 6 */  printf("%d\n", sizeof(&a));
    /* 7 */  printf("%d\n", sizeof(*&a));
    /* 8 */  printf("%d\n", sizeof(&a + 1));
    /* 9 */  printf("%d\n", sizeof(&a[0]));
    /* 10 */ printf("%d\n", sizeof(&a[0] + 1));

    return 0;
}

💡 answer:

/* 1 */  printf("%d\n", sizeof(a)); // 16
/* 2 */  printf("%d\n", sizeof(a + 0)); // 4/8
/* 3 */  printf("%d\n", sizeof(*a)); // 4
/* 4 */  printf("%d\n", sizeof(a + 1)); // 4/8
/* 5 */  printf("%d\n", sizeof(a[1])); // 4
/* 6 */  printf("%d\n", sizeof(&a)); // 4/8
/* 7 */  printf("%d\n", sizeof(*&a)); // 16
/* 8 */  printf("%d\n", sizeof(&a + 1)); // 4/8
/* 9 */  printf("%d\n", sizeof(&a[0])); // 4/8
/* 10 */ printf("%d\n", sizeof(&a[0] + 1)); // 4/8

🔑 Resolution:

1️⃣

2️⃣

3️⃣

4️⃣

5️⃣

6️⃣

7️⃣

8️⃣

9️⃣

🔟

Second question:

📌 review: [vitamin C language] Chapter 4 - array (0x02 initialization of one-dimensional array)

1 point for each sub question, full score 7 points

💬 Predict the running result of the following code (sizeof)

int main()
{
    
    char arr[] = {'a','b','c','d','e','f'}; // Character array

    /* 1 */  printf("%d\n", sizeof(arr));
    /* 2 */  printf("%d\n", sizeof(arr+0));
    /* 3 */  printf("%d\n", sizeof(*arr));
    /* 4 */  printf("%d\n", sizeof(arr[1]));
    /* 5 */  printf("%d\n", sizeof(&arr));
    /* 6 */  printf("%d\n", sizeof(&arr+1));
    /* 7 */  printf("%d\n", sizeof(&arr[0]+1));

    return 0;
}

💡 answer:

/* 1 */  printf("%d\n", sizeof(arr));  // 6
/* 2 */  printf("%d\n", sizeof(arr+0)); // 4/8
/* 3 */  printf("%d\n", sizeof(*arr)); // 1
/* 4 */  printf("%d\n", sizeof(arr[1])); // 1
/* 5 */  printf("%d\n", sizeof(&arr)); // 4/8
/* 6 */  printf("%d\n", sizeof(&arr+1)); // 4/8
/* 7 */  printf("%d\n", sizeof(&arr[0]+1)); // 4/8 

🔑 Resolution:

1️⃣

2️⃣

3️⃣

4️⃣

5️⃣

6️⃣

7️⃣

Third question:

1 point for each sub question, full score 7 points

💬 Predict the running results of the following code (strlen)

int main()
{

    char arr[] = { 'a','b','c','d','e','f' }; // Character array

    /* 1 */  printf("%d\n", strlen(arr));
    /* 2 */  printf("%d\n", strlen(arr + 0));
    /* 3 */  printf("%d\n", strlen(*arr));
    /* 4 */  printf("%d\n", strlen(arr[1]));
    /* 5 */  printf("%d\n", strlen(&arr));
    /* 6 */  printf("%d\n", strlen(&arr + 1));
    /* 7 */  printf("%d\n", strlen(&arr[0] + 1));

    return 0;
}

💡 answer:

/* 1 */  printf("%d\n", strlen(arr)); // Random value
/* 2 */  printf("%d\n", strlen(arr + 0)); // Random value
/* 3 */  printf("%d\n", strlen(*arr)); // error
/* 4 */  printf("%d\n", strlen(arr[1])); // error
/* 5 */  printf("%d\n", strlen(&arr)); // Random value
/* 6 */  printf("%d\n", strlen(&arr + 1)); // Random value - 6
/* 7 */  printf("%d\n", strlen(&arr[0] + 1)); // Random value - 1

🔑 Resolution:

1️⃣

2️⃣

3️⃣

4️⃣

5️⃣

6️⃣

7️⃣

Fourth question:

1 point for each sub question, full score 7 points

💬 Predict the running result of the following code (sizeof)

int main()
{
    char arr[] = "abcdef";

    /* 1 */  printf("%d\n", sizeof(arr));
    /* 2 */  printf("%d\n", sizeof(arr+0));
    /* 3 */  printf("%d\n", sizeof(*arr));
    /* 4 */  printf("%d\n", sizeof(arr[1]));
    /* 5 */  printf("%d\n", sizeof(&arr));
    /* 6 */  printf("%d\n", sizeof(&arr+1));
    /* 7 */  printf("%d\n", sizeof(&arr[0]+1));

    return 0;
}

💡 answer:

/* 1 */  printf("%d\n", sizeof(arr)); // 7
/* 2 */  printf("%d\n", sizeof(arr+0)); // 4/8
/* 3 */  printf("%d\n", sizeof(*arr)); // 1
/* 4 */  printf("%d\n", sizeof(arr[1]));// 1
/* 5 */  printf("%d\n", sizeof(&arr)); // 4/8
/* 6 */  printf("%d\n", sizeof(&arr+1)); // 4/8
/* 7 */  printf("%d\n", sizeof(&arr[0]+1)); // 4/8

🔑 Resolution:

1️⃣

2️⃣

3️⃣

4️⃣

5️⃣

6️⃣

7️⃣

Fifth question:

1 point for each sub question, full score 7 points

💬 Predict the running results of the following code (strlen)

int main()
{
    char arr[] = "abcdef";

    /* 1 */  printf("%d\n", strlen(arr));
    /* 2 */  printf("%d\n", strlen(arr + 0));
    /* 3 */  printf("%d\n", strlen(*arr));
    /* 4 */  printf("%d\n", strlen(arr[1]));
    /* 5 */  printf("%d\n", strlen(&arr));
    /* 6 */  printf("%d\n", strlen(&arr + 1));
    /* 7 */  printf("%d\n", strlen(&arr[0] + 1));

    return 0;
}

💡 answer:

/* 1 */  printf("%d\n", strlen(arr)); // 6
/* 2 */  printf("%d\n", strlen(arr+0)); // 6
/* 3 */  printf("%d\n", strlen(*arr)); // error
/* 4 */  printf("%d\n", strlen(arr[1])); // error
/* 5 */  printf("%d\n", strlen(&arr)); // 6
/* 6 */  printf("%d\n", strlen(&arr+1)); // Random value
/* 7 */  printf("%d\n", strlen(&arr[0]+1)); // 5

🔑 Resolution:

1️⃣

2️⃣

3️⃣

4️⃣

5️⃣

6️⃣

7️⃣

Sixth question:

1 point for each sub question, full score 7 points

📌 review: [vitamin C language] Chapter 10 - Advanced pointer (Part 1) (definition of 0x00 character pointer)

💬 Predict the running result of the following code (sizeof)

int main()
{
    char* p = "abcdef";

    /* 1 */  printf("%d\n", sizeof(p));
    /* 2 */  printf("%d\n", sizeof(p + 1));
    /* 3 */  printf("%d\n", sizeof(*p));
    /* 4 */  printf("%d\n", sizeof(p[0]));
    /* 5 */  printf("%d\n", sizeof(&p));
    /* 6 */  printf("%d\n", sizeof(&p + 1));
    /* 7 */  printf("%d\n", sizeof(&p[0] + 1));

    return 0;
}

💡 answer:

/* 1 */  printf("%d\n", sizeof(p)); // 4/8
/* 2 */  printf("%d\n", sizeof(p+1)); // 4/8
/* 3 */  printf("%d\n", sizeof(*p)); // 1
/* 4 */  printf("%d\n", sizeof(p[0])); // 1
/* 5 */  printf("%d\n", sizeof(&p));// 4/8
/* 6 */  printf("%d\n", sizeof(&p+1)); // 4/8
/* 7 */  printf("%d\n", sizeof(&p[0]+1)); // 4/8

🔑 Resolution:

1️⃣

2️⃣

3️⃣

4️⃣

5️⃣

6️⃣

7️⃣

Question 7:

1 point for each sub question, full score 7 points

💬 Predict the running results of the following code (strlen)

int main()
{
    char *p = "abcdef";

    /* 1 */  printf("%d\n", strlen(p));
    /* 2 */  printf("%d\n", strlen(p+1));
    /* 3 */  printf("%d\n", strlen(*p));
    /* 4 */  printf("%d\n", strlen(p[0]));
    /* 5 */  printf("%d\n", strlen(&p));
    /* 6 */  printf("%d\n", strlen(&p+1));
    /* 7 */  printf("%d\n", strlen(&p[0]+1));

    return 0;
}

💡 answer:

/* 1 */  printf("%d\n", strlen(p)); // 6
/* 2 */  printf("%d\n", strlen(p+1)); // 5
/* 3 */  printf("%d\n", strlen(*p)); // error
/* 4 */  printf("%d\n", strlen(p[0])); // error
/* 5 */  printf("%d\n", strlen(&p)); // Random value
/* 6 */  printf("%d\n", strlen(&p+1)); // Random value
/* 7 */  printf("%d\n", strlen(&p[0]+1)); // 5

🔑 Resolution:

1️⃣

2️⃣

3️⃣

4️⃣

5️⃣

6️⃣

7️⃣

Question 8:

1 point for each sub question, with a full score of 11 points

💬 Predict the running result of the following code (sizeof)

int main()
{
    int a[3][4] = {0}; // Two dimensional array

    /* 1 */  printf("%d\n",sizeof(a));
    /* 2 */  printf("%d\n",sizeof(a[0][0]));
    /* 3 */  printf("%d\n",sizeof(a[0]));
    /* 4 */  printf("%d\n",sizeof(a[0]+1));
    /* 5 */  printf("%d\n",sizeof(*(a[0]+1)));
    /* 6 */  printf("%d\n",sizeof(a+1));
    /* 7 */  printf("%d\n",sizeof(*(a+1)));
    /* 8 */  printf("%d\n",sizeof(&a[0]+1));
    /* 9 */  printf("%d\n",sizeof(*(&a[0]+1)));
    /* 10 */ printf("%d\n",sizeof(*a));
    /* 11 */ printf("%d\n",sizeof(a[3]));

    return 0;
}

💡 answer:

/* 1 */  printf("%d\n",sizeof(a)); // 48
/* 2 */  printf("%d\n",sizeof(a[0][0])); // 4
/* 3 */  printf("%d\n",sizeof(a[0])); // 16
/* 4 */  printf("%d\n",sizeof(a[0]+1)); // 4/8
/* 5 */  printf("%d\n",sizeof(*(a[0]+1)));// 4
/* 6 */  printf("%d\n",sizeof(a+1)); // 4
/* 7 */  printf("%d\n",sizeof(*(a+1))); // 16
/* 8 */  printf("%d\n",sizeof(&a[0]+1)); // 4/8
/* 9 */  printf("%d\n",sizeof(*(&a[0]+1))); // 16
/* 10 */ printf("%d\n",sizeof(*a)); // 16
/* 11 */ printf("%d\n",sizeof(a[3])); // 16

🔑 Resolution:

1️⃣

2️⃣

3️⃣

4️⃣

5️⃣

6️⃣

7️⃣

8️⃣

9️⃣

🔟

1️⃣1️⃣

Summary:

📚 Meaning of array name:

① sizeof (array name) - the array name represents the entire array and calculates the size of the entire array.

② & array name, where the array name represents the whole array, and the address of the whole array is taken out.

③ In addition, all array names represent the address of the first element.

Gags:

It's not easy to make... The liver came out in the middle of the night! I feel like vomiting when I see drawing tools

Please click three times, thank you!!

End of this chapter

Keywords: C

Added by timcclayton on Sun, 23 Jan 2022 01:24:50 +0200