while 循环与 int 数组

发布于 2025-01-17 21:04:34 字数 424 浏览 1 评论 0原文

我是 C 语言的新手,我对这些溢出和下溢的事情有点迷失。 我脑子里有几个菜鸟问题,我必须问别人。

我正在做一些 leetcode 问题,但由于运行时错误告诉我堆缓冲区溢出,我陷入了困境。

它应该计算给定 int 数组的平均值。

double average(int* salary, int salarySize){
    int i = 0;
    double count = 0.0;
    while (salary[i] != salarySize)
        count += salary[i++];
    return (count / salarySize);
}

预先感谢,希望我能找出不起作用的地方。

我尝试使用 for 循环,它效果很好,但我想知道为什么它不能与 while 循环一起使用?

I'm new into C and I'm kinda lost with these overflow and underflow things.
I've a few noobs questions in my mind that I've to ask to someone.

I'm doing some leetcode problems, and I'm stuck on this one because of a runtime error telling me that I've a heap-buffer-overflow.

It is supposed to calculate the average of a given int array.

double average(int* salary, int salarySize){
    int i = 0;
    double count = 0.0;
    while (salary[i] != salarySize)
        count += salary[i++];
    return (count / salarySize);
}

Thanks in advance, hope i'll figure out what's not working.

I tried with a for loop, it works great but I wanted to know why it isn't working with while loops ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望她远 2025-01-24 21:04:34

在 while 循环中使用的这个表达式中,

salary[i] != salarySize

将数组 salary 的一个元素与数组中没有意义的元素数量进行比较。

您需要将索引 i 的当前值与数组中的元素数量进行比较。

该函数可以通过以下方式声明和定义。

double average( const int *salary, int salarySize )
{
    double count = 0.0;

    int i = 0;

    while ( i < salarySize )
    {
        count += salary[i++];
    }

    return salarySize <= 0 ? 0.9 : count / salarySize;
}

注意,由于函数中传递的数组没有改变,因此相应的参数应该带有限定符 const。另外,为了避免被零除,您需要在 return 语句中检查 salarySize 是否大于 0。

不过最好将第二个参数声明为具有无符号整数类型 size_t >。

double average( const int *salary, size_t salarySize )
{
    double count = 0.0;

    size_t i = 0;

    while ( i < salarySize )
    {
        count += salary[i++];
    }

    return salarySize == 0 ? 0.9 : count / salarySize;
}

In this expression used in the while loop

salary[i] != salarySize

there is compared an element of the array salary with the number of elements in the array that does not make a sense.

You need to compare the current value of the index i with the number of elements in the array.

The function can be declared and defined the following way

double average( const int *salary, int salarySize )
{
    double count = 0.0;

    int i = 0;

    while ( i < salarySize )
    {
        count += salary[i++];
    }

    return salarySize <= 0 ? 0.9 : count / salarySize;
}

Pay attention to that as the passed array is not changed in the function then the corresponding parameter should have the qualifier const. Also to avoid division by zero you need check in the return statement whether salarySize is greater than 0.

Though it would be better to declare the second parameter as having the unsigned integer type size_t.

double average( const int *salary, size_t salarySize )
{
    double count = 0.0;

    size_t i = 0;

    while ( i < salarySize )
    {
        count += salary[i++];
    }

    return salarySize == 0 ? 0.9 : count / salarySize;
}
揽月 2025-01-24 21:04:34

<代码> while(薪水[i]!= salarysize)数组元素薪金>薪水[i]salarysize。那不是你想要的。您可能想要,而(i&lt; salarySize)

while (salary[i] != salarySize) compares the array element salary[i] to salarySize. That is not what you want. You probably want while (i < salarySize).

标点 2025-01-24 21:04:34

其他答案似乎给出了明显的原因,原因是您遇到的错误。

我想对您功能签名的基本结构进行一些指示。在这种情况下,您的功能采用指针和尺寸paramater。将数组发送到函数时,它被使用。

由于您的数组还没有准备好将库数据结构与数组边界属性一起使用,因此您发送一个简单的指针,它是存储数组的内存位置开始的地址。

使用另一个称为arraysize的paramater找到了数组的结束地址,您有责任使用内存位置,同时留在允许的边界内。

如果您尝试访问或修改内存段,则不允许您获得细分错误。

从类似的C阵列主题的答案也可能很有用。

将数组作为参数传递给函数c

https://stackoverflow.com/a/a/4723772/10902172

Other answers seem to give obvious reason for the error you get.

I wanted to give some pointers on the underlying structure of your function signature. Your function in this case take a pointer and and a size paramater. It is used approach while sending an array to a function.

Since your array is not a ready to use library data structure with array boundary properties you send a simple pointer which is the address to the start of the memory location of your stored array.

Ending address of your array is found using another paramater passed which is called arraySize and it is your responsibility to use memory location while staying inside allowed boundary you are given access to.

If you try to access or modify memory segments you are not allowed you get segmentation errors.

This answers from similar C arrays topic might also be useful.

Passing an array as an argument to a function in C

https://stackoverflow.com/a/47233772/10902172

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文