简体   繁体   English

如何在C中将char(*)([long unsigned int])作为参数传递?

[英]How do I pass a char(*)([long unsigned int]) as a parameter in C?

I have a variable type: 我有一个变量类型:

char (*p)[12] = calloc(n, sizeof(*p));

I need the variable to stay this way, but I am having trouble passing it as a parameter due to the function type giving me errors: 我需要变量保持这种方式,但是由于函数类型给我错误,我无法将其作为参数传递:

void myMethod(char *p) { ... }

What can I use in myMethod as the parameter to make this work? 我可以在myMethod中使用什么作为参数来完成这项工作?

What can I use in myMethod as the parameter to make this work 我可以在myMethod中使用什么作为参数来完成这项工作

Possibly this: 可能是这样的:

void myMethod(char (*p)[12])

Use the same type for the parameter as you did for the variable: 对参数使用与对变量相同的类型:

void myMethod(char (*p)[12])
{
  // do something with p
}
...
char (*p)[12] = calloc(n, sizeof *p);
myMethod(p);

Remember that p is a pointer to a 12-element array of char , not a simple pointer to char . 请记住, p是指向12个元素的char数组的指针,而不是指向char的简单指针。 Thus, the type of p[i] will be char [12] (which in most cases will decay to char * ). 因此, p[i]的类型将为char [12] (在大多数情况下,它将衰减为char * )。

The typical C idiom is to pass a char* and a size_t (or unsigned int or whatever equivalent) parameter. 典型的C习惯用法是传递char*size_t (或unsigned int或任何等效形式)参数。 Then the contract of the function is that it will respect the size_t argument as the length of the array/buffer. 然后,函数的约定是它将size_t参数视为数组/缓冲区的长度。

If you must insist that the pointer be not changed, you use the const qualifier. 如果必须坚持不更改指针,则可以使用const限定符。

char (*p)[12] = ...

With the previous you have NOT defined an array of 12 elements, each one being a pointer to char BUT a pointer to an array with 12 char elements. 在前面的示例中,您尚未定义12个元素的数组,每个元素都是指向char的指针, 指向包含12个char元素的数组的指针。

As explained in the following lines: 如以下各行所述:

int *a[3];

Start at a . 从开始。 Look right, say array of size 3. Look left and say pointer. 向右看,说出大小为3的数组。向左看,说出指针。 Look right and see nothing. 向右看,什么也看不到。 Look left and say int. 向左看,说出int。 All together you say a is an array of size 3 pointers to int. 大家都说a是一个大小为3的int指针数组。

Adding parentheses is when it gets weird: 添加括号是当它变得怪异的时候:

int (*a)[3];

The parentheses change the order just like in an expression. 括号就像在表达式中一样更改顺序。 When you look right after a , you see the right parenthesis, which you cannot jump over until you look left. 当您在右边看时,您会看到右括号,除非您向左看,否则您不能跳过它。 Hence, you would say a is a pointer to an array of 3 ints. 因此,您会说a是指向3个整数的数组的指针。

Check these links for better explanation: 检查这些链接以获得更好的解释:


Still if you want to pass the array to a function you can either copy it, or since it is an array pass a pointer to the first element (you can simply do it by using the name of the array): 不过,如果要将数组传递给函数,则可以复制它,或者由于它是数组,所以将指针传递给第一个元素(您可以使用数组的名称来完成此操作):

myMethod( p ) <- how you call the function

I personally would prefer the latter case, since you're passing to the function a simple pointer and not an array by copy of all of its elements 我个人更喜欢后一种情况,因为您要向函数传递一个简单的指针,而不是将其所有元素的副本作为数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM