以下为了通俗易懂,使用意译。

I've here very intersting discussion about the best and common ways to return an array from a function..
我最近很热衷于讨论从函数返回数组的最佳及常用方法
Some solutions to use output parameter and copy the value of the array into the value of this output parameter array.
Other solution to pass an array and use it inside the function.

一些解决方案是使用数组作为输出参数,并将值复制到这个参数。

Others to allocate the array inside the function and return refrence to it, but the caller have to free it once done.

其他一些则是在函数内部申请空间来存放数组,并将引用(此处应该是指针地址)返回给主调函数,但调用者必须在使用完以后释放。

Others to return a struct contains this pointer...

再者就是返回一个结构,包含这个数组的指针。

以下解决方案与诸君分享:

Please enjoy; stackoverflow.com/questions/8865982/return-array-from-function-in-c

const char numbers[] = "0123456789abcdef";

void getBase(int n, int b, char* str)
{
const size_t SIZE = ;
int digits=SIZE;
while (n > )
{
int t = n%b;
n/=b;
str[--digits] = numbers[t];
} int length = SIZE - digits; memmove(str,str + digits,length);
str[length] = '\0';
}

You just have to make sure that your str is large enough to avoid an array-overrun.

int main(){

    char str[];

    getBase(,,str);

    printf(str);

    return ;
}

返回结果


Returning array from function in C的更多相关文章

  1. Object、Function、String、Array原生对象扩展方法

    JavaScript原生对象的api有些情况下使用并不方便,考虑扩展基于Object.Function.String.Array扩展,参考了prototype.js的部分实现,做了提取和修改,分享下: ...

  2. [转] iOS ABI Function Call Guide

    source: apple ARMv6 Function Calling Conventions When functions (routines) call other functions (sub ...

  3. jQuery源码06-jQuery = function(){};给JQ对象,添加一些方法和属性,extend : JQ的继承方法,jQuery.extend()

    /*! * Includes Sizzle.js 选择器,独立的库 * http://sizzlejs.com/ */ (function( window, undefined ) { //" ...

  4. javascript中的Function和Object

    写的很好,理解了很多,特此转发记录 转自:http://blog.csdn.net/tom_221x/archive/2010/02/22/5316675.aspx 在JavaScript中所有的对象 ...

  5. JavaScript Array map() 方法

    语法: array.map(function(currentValue,index,arr), thisValue) currentValue:必须.当前元素的值index:可选.当期元素的索引值ar ...

  6. JS原型的问题Object和Function到底是什么关系

    var F = function(){}; Objcert.prototype.a = function(){}; Function.prototype.b = function(){}; F 既能访 ...

  7. js中Array的一些扩展

    IE下很多Array的方法都不被支持.每次都要写.所以记下来,以免忘记: 以下是对Array的一些扩展,在FF ,google 下是不需要加的. /** * 方法Array.filter(functi ...

  8. 回文数组(Rotate Array (JS))

    旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...

  9. polyfill之javascript函数的兼容写法——Array篇

    1. Array.isArray(obj) if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype. ...

随机推荐

  1. CH5105 Cookies饼干(线性DP)

    题意理解 圣诞老人共有\(M\)个饼干,准备全部分给\(N\)个孩子. 每个孩子有一个贪婪度,第 i 个孩子的贪婪度为 \(g[i]\). 如果有 \(a[i]\) 个孩子拿到的饼干数比第 \(i\) ...

  2. Netty TCP 通信失败

    前段时间,在搞Netty TCP 通信,踩了一些坑,今天就在这篇总结一下 Netty通信失败原因 Netty TCP 通信失败的可能原因: 1.服务端或客户端,其中一端没有正常启动 2.是否在正确的位 ...

  3. HDU - 4992 Primitive Roots (原根)

    模板题,可用于求一个数的所有原根. #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f ...

  4. oracle—数据泵及常用参数

    -- 1.创建目录dumpcreate or replace directory dump as '/home/oracle/dump'; -- 2.授权:Grant read,write on di ...

  5. 【转】分布式文件系统FastDFS架构剖析

    FastDFS是一款类Google FS的开源分布式文件系统,它用纯C语言实现,支持Linux.FreeBSD.AIX等UNIX系统.它只能通过专有API对文件进行存取访问,不支持POSIX接口方式, ...

  6. Linux 常用命令备忘

    安装wget 方便联网下载:  centos : sudo yum -y install wget 安装vim   :  yum -y install vim* set nu              ...

  7. 对Sting类型的探讨

    string类型经常和基本数据类型一起被我们熟练运用,但却不被归为基本数据类型,他是特殊的引用类型.引用数据类型还有类,接口.数组.枚举类型和注解类型. 我们来看下jdk对他的解释: String是在 ...

  8. tomcat下载与安装

    https://www.cnblogs.com/limn/p/9358657.html

  9. @RequestMapping的简单理解

    @Controller public class ItemController { @Autowired private ItemService itemService; 获取路径参数.../item ...

  10. What makes a good feature

    import numpy as np import matplotlib.pyplot as plt greyhounds = 500 # 灰猎犬500只 labs = 500 # 拉布拉多犬500只 ...