为什么C函数不能返回数组? [英] Why can C functions not return arrays?

查看:299
本文介绍了为什么C函数不能返回数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,一个结构(记录数据结构)可以是一个函数的返回类型,但数组不能。什么样的设计在C语言的原因阵列的特性是一个例外?

In C, a struct (record data structure) can be the return type of a function, but an array cannot be. What design characteristics of the C Language cause arrays to be an exception?

推荐答案

在C语言中一个赤裸的数组类型是不可拷贝的主要是历史原因。出于这个原因,它是不可能的数组初始化数组,数组分配给阵列,按值作为参数传递数组或函数返回数组。 (初始化环境有一个明显的例外个char [6] =你好;

A naked array type in C language is not copyable for primarily historical reasons. For this reason it is not possible to initialize arrays with arrays, assign arrays to arrays, pass arrays by value as parameters or return arrays from functions. (Initialization context has a notable exception of char s[6] = "Hello";.)

这仍然是可能的,如果数组封装在结构类型,这表明该限制是纯属声明做上述所有。有没有令人信服的技术理由。

It is still possible to do all the above if the array is wrapped in struct type, which demonstrates that the limitation is purely declarative in nature. There's no compelling technical reason for it.

C语言继承了其阵列实施办法,从它的历史predecessors - B和BCPL语言。在B / BCPL阵列被公开作为指针实现,这意味着为了一个阵列分配给另一个实际重新指针presented分配。 C语言遵循了不同的方法。在C数组不是指针,但C数组的接口规范保持与B / BCPL的表面兼容。在C数组仍然是pretend是在大多数情况下指针。这是一个原因,他们不会立即拷贝的。

C language inherited its approach to array implementation from its historical predecessors - B and BCPL languages. In B/BCPL arrays were openly implemented as pointers, meaning that an attempt to assign one array to another actually represented assignment of pointers. C language followed a different approach. In C arrays are not pointers, but the interface specification of C arrays is kept superficially compatible with that of B/BCPL. Arrays in C still "pretend" to be pointers in most contexts. This is one reason they are not immediately copyable.

这篇关于为什么C函数不能返回数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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