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 ...
- 使用IDA Pro逆向C++程序
使用IDA Pro逆向C++程序 附:中科院李_硕博 : IDA用来做二进制分析还是很强大的 .lib程序是不是很容易分析出源码? 这个得看编译选项是怎么设置的 如果没混淆 没太过优化 大体能恢复源码 ...
- 快速开发MQTT(一)电子工程师眼中的MQTT
转载:https://zhuanlan.zhihu.com/p/54669124 DigCore 主页http://www.digcore.cn 文章首发于同名微信公众号:DigCore 欢迎关注同名 ...
- ingress部署
# 手动部署 # 部署ingress-nginx 官方地址:https://github.com/kubernetes/ingress-nginx # 参考链接:https://www.jianshu ...
- jQuery 3.0 的新特性
1. jQuery 3.0 运行在严格模式下 当下几乎支持jQuery 3.0的浏览器都支持严格模式,该版本正是基于此进行编译发布的. 你的代码已经运行在非严格模式?不用担心,你无需重写.jQuery ...
- jpa介绍
1.jpa的介绍 JPA是Java Persistence API的简称, 中文名为Java持久层API; 是JDK 5.0注解或XML描述对象-关系表的映射关系, 并将运行期的实体对象持久化到数据库 ...
- BZOJ 4522: [Cqoi2016]密钥破解 exgcd+Pollard-Rho
挺简单的,正好能再复习一遍 $exgcd$~ 按照题意一遍一遍模拟即可,注意一下 $pollard-rho$ 中的细节. #include <ctime> #include <cma ...
- 关于java代理(静态代理和动态代理)
参考文章:http://kuangbaoxu.iteye.com/blog/193240
- Codevs 1213 解的个数(exgcd)
1213 解的个数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知整数x,y满足如下面的条件: ax+by+c=0 p< ...
- 灰度图像--图像分割 Marr-Hildreth算子(LoG算子)
学习DIP第49天 转载请标明本文出处:*http://blog.csdn.net/tonyshengtan *,出于尊重文章作者的劳动,转载请标明出处!文章代码已托管,欢迎共同开发: https:/ ...