c ++数组约定 [英] c++ array conventions

查看:43
本文介绍了c ++数组约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以写下面的代码


void func1(int myArr [])

{}


void func2(int * myArr)

{}


void main()

{

int myArr [50];

func1(myArr);

func2(myArr);

}


两者都没问题。有没有人对

正确性有任何想法。一个或另一个?这两个约定意味着什么?

也许func2更通用,所以应该总是使用。

Hi, I could write the following code

void func1(int myArr[])
{}

void func2(int* myArr)
{}

void main()
{
int myArr[50];
func1(myArr);
func2(myArr);
}

and both are fine. Does anyone have any thoughts as to the
"correctness" of one or the other? What do the two conventions imply?
Perhaps func2 is more versatile so should always be used.

推荐答案

DaveG写道:
DaveG wrote:
我可以写下面的代码

void func1(int myArr [])
{}

void func2(int * myArr)
{}

void main()


这不应该编译。 main()必须返回int。

{
int myArr [50];
func1(myArr);
func2(myArr);
}

两者都很好。有没有人对
正确性有任何想法?一个或另一个?


两者都是正确的。它们是等价的,即两个函数都有一个指针

作为参数。

这两个约定意味着什么?


我会说前者意味着该函数需要一个指向数组开始

的指针。后者并不意味着什么。

也许func2更通用,所以应该一直使用。
Hi, I could write the following code

void func1(int myArr[])
{}

void func2(int* myArr)
{}

void main()
This should not compile. main() must return int.
{
int myArr[50];
func1(myArr);
func2(myArr);
}

and both are fine. Does anyone have any thoughts as to the
"correctness" of one or the other?
Both are correct. They are equivalent, i.e. both functions have one pointer
as parameter.
What do the two conventions imply?
I''d say the former implies that the function expects a pointer to the start
of an array. The latter doesn''t imply anything.
Perhaps func2 is more versatile so should always be used.




我更喜欢它,因为它说实际发生了什么:指针被传递。

前者只是将指针隐藏在伪数组符号后面。



I prefer it, because it says what really happens: A pointer is passed. The
former just hides the pointer behind a pseudo array notation.


>这不应该编译。 main()必须返回int。


'实际上并不正确。你可以将main声明为void函数。
> This should not compile. main() must return int.

that''s not actually correct. You can declare main as a void function.
两者都是正确的。它们是等价的,即两个函数都有一个指针
作为参数。


我不认为它们是等价的 - 编译器有很多

信息与func1()特别是当涉及到
多维数组时,我认为这个约定需要

a与func2的正确偏差( )

我会说前者暗示函数需要一个指向数组开头的指针。后者并不意味着什么。


我会说后者意味着传入的数组将是

静态(全局)或在堆栈上创建。

我更喜欢它,因为它说明了真正发生的事情:指针被传递。前者只是将指针隐藏在伪数组符号后面。
Both are correct. They are equivalent, i.e. both functions have one pointer as parameter.

I don''t think they''re equivalent - the compiler has a lot more
information with the func1() especially when it comes to
multi-dimentional arrays, which is where I think this convention takes
a proper deviation from func2()
I''d say the former implies that the function expects a pointer to the start of an array. The latter doesn''t imply anything.
I''d say the latter implies arrays passed in will either be
static(global) or created on the stack.
I prefer it, because it says what really happens: A pointer is passed. The former just hides the pointer behind a pseudo array notation.




我同意



I agree




DaveG写道:

DaveG wrote:
这不应该编译。 main()必须返回int。
这实际上并不正确。你可以将main声明为void函数。
This should not compile. main() must return int.
that''s not actually correct. You can declare main as a void function.




返回void的main函数的行为是未定义的。你必须

总是定义一个int main。

我不认为它们是等价的 - 编译器有更多的信息与func1 ()特别是当涉及到多维数组时,我认为这个约定需要与func2正确偏离()


不应该''我们在multidimensionaly数组中有一个** ???



The behaviour of a main function returning void is undefined. you must
always define a int main.
I don''t think they''re equivalent - the compiler has a lot more
information with the func1() especially when it comes to
multi-dimentional arrays, which is where I think this convention takes
a proper deviation from func2()
Shouldn''t we have a ** in multidimensionaly arrays???

我更喜欢它,因为它说明了实际发生的事情:指针被传递。前者只是将指针隐藏在伪数组符号后面。
I prefer it, because it says what really happens: A pointer is passed. The former just hides the pointer behind a pseudo array notation.



我同意



I agree



我也是。


~M


Me too.

~M


这篇关于c ++数组约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆