C语言 C - 如果数组中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39742123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:29:20  来源:igfitidea点击:

C - If value in array

carrayssyntax

提问by gusgxrha

I am new to C and am currently studying arrays. I would like to check if a certain value is in an array but I am running in some issues (not only syntax-wise but also from understanding-wise).

我是 C 新手,目前正在研究数组。我想检查某个值是否在数组中,但我遇到了一些问题(不仅在语法方面,而且在理解方面)。

My goal is to have a function which I can call, give it two arguments - the value to search for and the array to search in - and get a 0 or 1 based on whether it was found or not back.

我的目标是有一个我可以调用的函数,给它两个参数——要搜索的值和要搜索的数组——并根据它是否被找到返回一个 0 或 1。

My approach is the following:

我的方法如下:

#include <stdio.h>
#include <stdlib.h>

int valueinarray(float val, float *arr[]);

int main()
{
    float arr[] = {5, 4.5, 4, 3.5, 3, 2.5,};
    int test = valueinarray(4.5, *arr[]);
    printf("%d", test);

    return 0;
}

int valueinarray(float val, float *arr[]){
    int i;
    for(i = 0; i < sizeof(*arr[]); i++){
        if(*arr[i] == val) return 1;
    }
    return 0;
}

I have two questions now especially regarding the syntax:

我现在有两个问题,特别是关于语法的问题:

  1. If I create a function with a pointer as one of its' parameters, do I have to refer to it using "*arr[]" inside the function the whole time? Or is "arr[]" or even "arr" enough?

  2. Do I understand it correctly that I am unable to pass a whole array to a function so I use a pointer instead?

  1. 如果我创建一个带有指针作为其参数之一的函数,我是否必须一直在函数内部使用“*arr[]”来引用它?还是“arr[]”甚至“arr”就足够了?

  2. 我是否正确理解我无法将整个数组传递给函数所以我改用指针?

Moreover, my approach is wrong and I do not see why. Iterating over the array seems to work just fine and even checking if a certain value is in it works as well, the issue seems to be in the way I call the function. I read about double pointers, is this a scenario where they're needed? If not, what are they needed for?

此外,我的方法是错误的,我不明白为什么。迭代数组似乎工作得很好,甚至检查某个值是否在其中也有效,问题似乎出在我调用函数的方式上。我读过双指针,这是需要它们的场景吗?如果不是,他们需要什么?

Thanks a lot.

非常感谢。

采纳答案by Dimitroff

float arr[] = ...;

declares an array(because of []) of floats(because of the float keyword) that is called arr. Similarly in a function declaration:

声明一个名为 arr 的浮点数组(因为 [])(因为有 float 关键字)。同样在函数声明中:

int valueinarray(float val, float *arr[]);

means that the second argument is a pointer(because of *) to an array(because of[]) which isn't what you need at all. You need to accept just an array:

意味着第二个参数是指向数组(因为 [])的指针(因为 *),这根本不是您需要的。您只需要接受一个数组:

int valueinarray(float val, float arr[]);

folowing this logic your code would look like this:

按照此逻辑,您的代码将如下所示:

int valueinarray(float val, float arr[])
{
    int i;
    for(i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
    {
        if(arr[i] == val)
            return 1;
    }
    return 0;
}

Notice a number of changes:

注意一些变化:

  1. The function parameter arr is now an array.

  2. The expression

    sizeof(arr) / sizeof(arr[0]) first takes the size of arr the array in bytes. Then divides it by the size in bytes of the first element in it. And now we are left with the number of elements. If it were a char array, then this is not needed is 1 char takes up 1 byte. For any other type it is necessary. As a side note you could've divided by sizeof(float), but this makes it harder for you if you change the type and is a worse practice than sizeof(arr[0]).

  1. 函数参数 arr 现在是一个数组。

  2. 表达方式

    sizeof(arr) / sizeof(arr[0]) 首先获取数组 arr 的大小(以字节为单位)。然后将其除以其中第一个元素的大小(以字节为单位)。现在我们只剩下元素的数量了。如果它是一个字符数组,则不需要,因为 1 个字符占用 1 个字节。对于任何其他类型,它是必要的。作为旁注,您可以除以 sizeof(float),但是如果您更改类型,这会使您更难,并且是比 sizeof(arr[0]) 更糟糕的做法。

If you didn't divide you could have an array of 10 floats, but you could try to access 40. So you'd be meddling with 120 bytes of the memory right after your array that aren't yours.

如果您不进行除法,您可以拥有一个包含 10 个浮点数的数组,但您可以尝试访问 40 个。因此,您将在不属于您的数组之后立即处理 120 个字节的内存。

Also the asterisk * dereferences outside of declarations. If you have:

还有星号 * 在声明之外取消引用。如果你有:

int a[5];
int x = 5;
int *pointerToInt = &x;

then x is a label for some memory (variable) that stores the value 5. &x is a pointer to the memory that stores the label. The opposite of & is *. Whlie pointerToInt equals &x, *pointerToInt equals x. Also *a equals a[0], but isn't literally the same, *(a+1) equals a[1].

那么 x 是存储值 5 的某个内存(变量)的标签。 &x 是指向存储标签的内存的指针。&的反义词是*。当pointerToInt 等于&x,*pointerToInt 等于x。*a 也等于 a[0],但实际上并不相同,*(a+1) 等于 a[1]。