相关疑难解决方法(0)

什么是阵列衰减?

什么是阵列的衰变?与数组指针有关系吗?

c c++ arrays pointers

358
推荐指数
8
解决办法
5万
查看次数

为什么数组参数的大小与main中的大小不同?

为什么作为参数发送的数组的大小与main中的相同?

#include <stdio.h>

void PrintSize(int p_someArray[10]);

int main () {
    int myArray[10];
    printf("%d\n", sizeof(myArray)); /* As expected, 40 */
    PrintSize(myArray);/* Prints 4, not 40 */
}

void PrintSize(int p_someArray[10]){
    printf("%d\n", sizeof(p_someArray));
}
Run Code Online (Sandbox Code Playgroud)

c arrays function sizeof

101
推荐指数
7
解决办法
7万
查看次数

数组的长度在传递函数后发生变化

我使用以下代码将数组传递给函数并在调用该函数之前和之后打印其大小:

#include <stdio.h>

int passing_array(int array[]) {
    int size = sizeof(array);
    printf("The size after passing: %d\n", size);
}

int main () {
    int a[] = {1, 2, 3};

    int size = sizeof(a);
    printf("The size before passing: %d\n", size);

    passing_array(a);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

运行代码后,我得到了一个奇怪的结果:

通过前的尺寸:12

通过后尺寸:8

我的问题是:为什么数组的大小从12变成8?

c arrays

0
推荐指数
1
解决办法
1799
查看次数

标签 统计

arrays ×3

c ×3

c++ ×1

function ×1

pointers ×1

sizeof ×1