简体   繁体   English

C-如果声明为int *,则函数中的参数无法处理int *

[英]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 intintleft_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.

相关问题 C 编程:参数:无法转换为 int* - C Programming : Parameter : Can't convert to int* 无法在C中将int初始化为0 - Can't initialize int at 0 in c 当 function 原型具有声明为 int a[][10] 的参数时,编译器将其视为 int (*a)[10] 是否真的如此? - Is it actually true that when function prototype has a parameter declared as int a[][10] the compiler treats it as int (*a)[10]? C将int数组指针作为参数传递给函数 - C pass int array pointer as parameter into a function 使用Fortran中的** int参数调用C函数 - Calling C function with **int parameter from Fortran 为什么 int function 即使没有在 C 中首先声明它们也能工作 - Why int function works even when they're not declared first in C C : 非常确定一个参数是一个 int 参数 - C : Veryfing that a parameter is an int parameter “int(*)[]”在函数参数中是否会衰减为“int **”? - Does “int (*)[]” decay into “int **” in a function parameter? 调用函数int strrindex(char s [],char t [])而不在c中传递size参数 - calling a function int strrindex(char s[], char t[]) without passing the size parameter in c 标识符在C函数参数列表中做什么? 例如:int foo(int IDENTIFIER parameter_name); - What does an identifier do in C function parameter list? For example: int foo(int IDENTIFIER parameter_name);
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM