简体   繁体   English

将C数组作为char *函数参数传递

[英]Passing C array as char* function parameter

I've got some code I'm mantaining with the following variable declaration: 我有一些用以下变量声明维护的代码:

char tmpry[40];

It's being used with this function: 此函数正在使用它:

char *SomeFunction(char *tmpryP) {
   // Do stuff.
}

The function call is: 函数调用为:

SomeFunction(&tmpry[0]);

I'm pretty damn sure that's just the same as: 我非常确定这与以下内容相同:

SomeFunction(tmpry);

I've even checked that the char* pointer in SomeFunction ends up pointing to the same memory location as the array in both cases. 我什至检查了在两种情况下SomeFunction中的char *指针最终都指向与数组相同的内存位置。

My question is a sanity check as to whether the two function calls are identical (and therefore the original programmer was just being nasty)? 我的问题是是否两个函数调用是否相同(因此原始程序员只是讨厌)?

The two are equivalent and I think SomeFunction(tmpry); 两者是等效的,我认为SomeFunction(tmpry); is more readable. 更具可读性。

they are exactly the same. 他们是完全一样的。

someArray[i]

means exactly 确切地意味着

*(someArray + i)

where someArray in the second case is a pointer to the memory location. 其中,第二种情况下的someArray是指向内存位置的指针。 Similarly, 同样,

&someArray[i]

means exactly 确切地意味着

(someArray + i)

In both these cases the terms are pointers to memory locations. 在这两种情况下,这些术语都是指向存储位置的指针。

It may be significant, if SomeFunction is a macro and takes "sizeof" of one of its arguments, because sizeof(tmpry) may not necessarily be equal to sizeof(&tmpry[0]) . 如果SomeFunction是一个宏并且使用其参数之一的"sizeof" ,这可能很重要,因为sizeof(tmpry)不一定等于sizeof(&tmpry[0])

Otherwise, as others pointed out, they are exactly the same. 否则,正如其他人指出的那样,它们是完全相同的。

有关数组和指针C编程常见问题解答部分解决了这个问题(以及许多其他常见的C问题和困惑)。

As everyone else said, the two notations are equivalent. 就像其他人所说的,这两种表示法是等效的。

I would normally use the simpler one, unless there are multiple calls like this: 我通常会使用较简单的方法,除非有多个这样的调用:

SomeFunction(&tmparry[0]);
SomeFunction(&tmparry[10]);
SomeFunction(&tmparry[NNN]);

Where ideally all the constants (magic numbers) would be enum (or #define ) constants. 理想情况下,所有常量(幻数)都是枚举(或#define )常量。

Both are one and the same although second one looks nice and clarifies the intention of passing the array to the function. 尽管第二个看起来不错并且阐明了将数组传递给函数的意图,但两者都是相同的。 But how does the SomeFunction know the size of the array being passed, is it always assumed as 40 ? 但是SomeFunction如何知道要传递的数组的大小,是否始终假定为40? I feel it is better to pass the size also as the parameter to SomeFunction. 我觉得最好将大小也作为参数传递给SomeFunction。

These two variants are equivalent and passing like this 这两个变体是等效的,并且像这样传递

SomeFunction(tmpry);

looks cleaner. 看起来更干净。

The declaration 报关单

int a[10]; int a [10]; int *pa; int * pa;

There is one difference between an array and a pointer that must be kept in mind. 必须牢记数组和指针之间的区别。 A pointer is a variable, so pa=a and pa++ are legal. 指针是变量,因此pa = a和pa ++是合法的。 But an array name is not a variable; 但是数组名称不是变量; construction like a=pa and a++ are illegal 像a = pa和a ++这样的构造是非法的

As format parameters in a function definition, char s[] and char *s are equivalent; 作为函数定义中的格式参数,char s []和char * s是等效的;

From: The C Programming Language 2th , Page 99-100 来自: C编程语言第二版 ,第99-100页

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

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