传递给函数的阵列上使用的sizeof() [英] Using sizeof() on an array passed to a function

查看:154
本文介绍了传递给函数的阵列上使用的sizeof()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于C的sizeof()函数的一个简单的问题。我传递一个数组的函数,该函数内部,我要检查数组的大小。然而,无论输入数组的大小,里面的功能,它似乎总是有一个大小为4,例如:

I have a quick question about the C sizeof() function. I'm passing an array into a function, and inside that function, I want to check the size of the array. However, regardless of the size of the input array, inside the function, it always seems to have a size of 4. For example:

void checkArraySize(char data[])
{
    int internalSize = sizeof(data); //internalSize is reported as 4
}

void main(void)
{
    char data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
    int externalSize = sizeof(data); //externalSize is reported as 8

    checkArraySize(data);                                             
}

由于在code状态的评论之前,我喜欢叫checkArraySize()函数,我检查数组的大小,它显示了8。然而,当我检查数组的大小在函数内部,它只报告具有4个元素(它报告4元素,无论输入数组的大小)。

As the comments in the code state, before I call a function like checkArraySize(), I check the size of the array, and it shows 8. However, when I check the size of the array inside the function, it only reports as having 4 elements (it reports 4 elements regardless of the size of the input array).

很多时候,当我看到这样的问题,这是因为在我的话题的理解的差距。所以,我敢肯定,我只是缺少着如何传递数组的作品的东西。如果没有,任何想法,为什么发生这种情况?

More often than not, when I see issues like this, it's because there is a gap in my understanding of the topic. So, I'm sure I'm just missing something with how passing arrays works. If not, any ideas as to why this is happening?

注:这是一个PIC32,所以我使用Microchip的MPLAB个IDE和XC32编译器

Note: This is on a PIC32, so I'm using Microchip's MPLAB X IDE and XC32 compiler.

推荐答案

阵列C总是按引用传递。这就是为什么你每次都让指针大小,而不是实际尺寸。

Array in C always passed by reference. Thats why you are getting pointer size each time, not actual size.

要与数组用C的工作作为一个参数,你应该通过数组的大小数组。

To work with array in C as an argument, you should pass size of array with array.

我修改程序到工作状态:

I modified your program to working condition:

typedef unsigned char BYTE;

void checkArraySize(BYTE data[], int sizeOfArray)
{
    int internalSize = sizeOfArray;   
    printf("%d", internalSize );                  
}

void main(void)
{
    BYTE data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
    int externalSize = sizeof(data)/sizeof(BYTE);                   //it would return number of elements in array

    checkArraySize(data, externalSize);                                             
}

按引用传递意味着只有数组的第一个元素的地址被发送。如果里面的功能,甚至改变什么的 checkArraySize ,这种变化将反映到原数组了。检查修改上面的例子。

passed by reference means only address of first element of array is sent. If you change anything even inside function checkArraySize, this change would be reflected to original array too. Check modified above example.

typedef unsigned char BYTE;

void checkArraySize(BYTE data[])
{
    int internalSize = sizeof(data);   
    printf("%d\n", internalSize );     
    data[3]=  0x02;             //internalSize is reported as 4
}

void main(void)
{
    BYTE data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
    int externalSize = sizeof(data);                   //externalSize is reported as 8
    printf("Value before calling function: 0x%x\n",data[3]);
    checkArraySize(data);  
    printf("Value after calling function: 0x%x\n",data[3]);
}

输出是:

Value before calling function: 0x4
4
Value after calling function: 0x2

这篇关于传递给函数的阵列上使用的sizeof()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆