[英]C - parameter in function can't handle int if declared as int*
I want to use the C function called is_subsetOf() in two different ways: 我想以两种不同的方式使用名为is_subsetOf()的C函数:
Way 1: int* a and int* b are arrays with sizes >=2. 方式1:int * a和int * b是大小> = 2的数组。 Way 2: int* a has size >=2, but int* b is only size 1, which means b is an int. 方式2:int * a的大小> = 2,但是int * b的大小仅为1,这意味着b是int。
How can I force C to be okay with int* b being of size 1? 我如何强制int * b的大小为1的C正常? Or is this not possible in C? 还是在C中不可能? An int IS an array of size 1?? 一个int是大小为1的数组?
int* is_in(int *left_hand, int n_l, int *right_hand, int n_r) {
int *get_bool;
get_bool = malloc(sizeof(int)*n_l);
for (int i=0; i<n_l; i++) {
if (is_subsetOf(right_hand, n_r, *(left_hand+i), 1)) {
*(get_bool+i) = 1;
}
}
return (get_bool);
}
int desc(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int is_subsetOf(int *a, int n_a, int *b, int n_b) {
qsort(a, n_a, sizeof(int), desc);
qsort(b, n_b, sizeof(int), desc);
int v = includes(a, n_a, b, n_b);
return(v);
}
Here are the messages I get from the compiler. 这是我从编译器获得的消息。 It's just a warning, I know, but I'd like everything to be clean. 我知道这只是一个警告,但我希望一切都干净。
tmp.c: In function ‘is_in’:
tmp.c:73:47: warning: passing argument 3 of ‘is_subsetOf’ makes pointer
from integer without a cast [-Wint-conversion]
if (is_subsetOf(right_hand, n_r, *(left_hand+i), 1)) {
~~~~~~~~~^~~
tmp.c:37:39: note: expected ‘int *’ but argument is of type ‘int’
int is_subsetOf(int *a, int n_a, int *b, int n_b) {
int* a and int* b are arrays with sizes >=2. int * a和int * b是大小> = 2的数组。
No, they are pointers and they don't have any size. 不,它们是指针,没有任何大小。 You probably meant that you pass arrays through them, but they are not arrays. 您可能是说要通过它们传递数组,但它们不是数组。 Know that there is a difference. 知道有区别。
An int IS an array of size 1 一个整数是一个大小为1的数组
No, int a[1];
不, int a[1];
is an array of size 1; 是大小为1的数组; int a;
is just int
. 只是int
。 But arrays can decay into pointers to their first element and variables have addresses, so this is correct: 但是数组可以衰减为指向其第一个元素的指针,并且变量具有地址,因此这是正确的:
int a[1];
int b;
int* ptr1 = a;//Points to the a[0]
int* ptr2 = &b;
Both are now same type and can be used in the same way. 现在两者都是相同的类型,并且可以以相同的方式使用。 Of course you don't know if the int
is followed by any more ints in memory, that kind of checking is up to the programmer (usually by passing the length param as you do). 当然,您不知道int
是否在内存中后面跟随着更多的int,这种检查取决于程序员(通常像您一样通过传递长度参数)。 The following is the code you are actually looking for: 以下是您实际上正在寻找的代码:
is_subsetOf(right_hand, n_r, left_hand+i, 1)
Pointers can be incremented, left_hand+i
will point to the i-th int
after the int
to which left_hand
currently points to. 指针可以被递增, left_hand+i
将指向第i int
后int
到left_hand
当前指向。 Again, validity of such pointer is up to programmer. 同样,这种指针的有效性取决于程序员。
The compiler warning is quite important here, because *(left_hand+i)
is of type int
and the compiler warns that it will treat is as int*
. 此处的编译器警告非常重要,因为*(left_hand+i)
的类型为int
,并且编译器警告其将其视为int*
。 Essentially looking that the value of int
as an address to memory. 本质上是将int
的值视为内存的地址。 That's not at all what you want and it is an error . 那根本不是您想要的,这是 一个错误 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.