Returning array from function in C
以下为了通俗易懂,使用意译。
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的更多相关文章
- Object、Function、String、Array原生对象扩展方法
JavaScript原生对象的api有些情况下使用并不方便,考虑扩展基于Object.Function.String.Array扩展,参考了prototype.js的部分实现,做了提取和修改,分享下: ...
- [转] iOS ABI Function Call Guide
source: apple ARMv6 Function Calling Conventions When functions (routines) call other functions (sub ...
- jQuery源码06-jQuery = function(){};给JQ对象,添加一些方法和属性,extend : JQ的继承方法,jQuery.extend()
/*! * Includes Sizzle.js 选择器,独立的库 * http://sizzlejs.com/ */ (function( window, undefined ) { //" ...
- javascript中的Function和Object
写的很好,理解了很多,特此转发记录 转自:http://blog.csdn.net/tom_221x/archive/2010/02/22/5316675.aspx 在JavaScript中所有的对象 ...
- JavaScript Array map() 方法
语法: array.map(function(currentValue,index,arr), thisValue) currentValue:必须.当前元素的值index:可选.当期元素的索引值ar ...
- JS原型的问题Object和Function到底是什么关系
var F = function(){}; Objcert.prototype.a = function(){}; Function.prototype.b = function(){}; F 既能访 ...
- js中Array的一些扩展
IE下很多Array的方法都不被支持.每次都要写.所以记下来,以免忘记: 以下是对Array的一些扩展,在FF ,google 下是不需要加的. /** * 方法Array.filter(functi ...
- 回文数组(Rotate Array (JS))
旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...
- polyfill之javascript函数的兼容写法——Array篇
1. Array.isArray(obj) if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype. ...
随机推荐
- java继承-子类调用父类的方法中包含子类重写的方法
# 看题目是不是很绕,这个我也不知道怎么才能更简单的表达了... # 先看代码: public class Common { public static void main(String[] args ...
- Tarjan无向图的割点和桥(割边)全网详解&算法笔记&通俗易懂
更好的阅读体验&惊喜&原文链接 感谢@yxc的腿部挂件 大佬,指出本文不够严谨的地方,万分感谢! Tarjan无向图的割点和桥(割边) 导言 在掌握这个算法前,咱们有几个先决条件. [ ...
- Summer training #4
D:找到两个数 一个是另一个的整数倍(1也算) 因为N是600000 调和级数为ln(n+1) 算一下 可以直接爆 #include <bits/stdc++.h> #include &l ...
- 配置LANMP环境(7)-- 配置nginx反向代理,与配置apache虚拟主机
一.配置nginx反向代理 1.修改配置文件 vim /etc/nginx/nginx.conf 在35行http下添加一下内容: include /data/nginx/vhosts/*.conf; ...
- Python——import与reload模块的区别
原创声明:本文系博主原创文章,转载或引用请注明出处. 1. 语法不同 import sys reload('sys') 2. 导入特性不同 import 和reload都可以对同一个模块多次加载, ...
- WPF绑定命令
一.目的 降低代码耦合度(降低UI层和BLL层的代码耦合度),将UI层的后台代码更好的转移到BLL层中,让视图和业务逻辑分离的更好 二.使用方式 1.创建一个RelayCommand,继承IComma ...
- MFC Bitmap::FromBITMAPINFO返回空问题
领导临时给了一个任务,项目上的一个问题,就是FromBITMAPINFO不成功,之前也没有弄过MFC的东西,但领导让干就干吧.咱也不知道怎么回事,咱也不敢问. 上来直接度娘,试过各种办法,都没与解决, ...
- IIS无法启动解决方案
1.第一步 2.第二步
- 多线程(四)wait()、notify()以及notifyAll()
六.线程的等待和唤醒 1.wait()和notify()的简单示范 public class Wait extends Thread{ public synchronized void run() { ...
- 【GDKOI2018】总结
前言 车祸现场... day1 T1:其实就是对于每个点的有用的时间点建一个点,然后连边,对于询问(x,y),从点(y,inf),往回走,能走到的最早的x的时间点就是答案. 比赛上用最后的一个多小时来 ...