Error[8]: Undefined offset: 4, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我看到有人的C代码有如下的函数声明: void information_log( const char* fmt , ...) 或像块一样 catch(...){} 这是什么意思? 省略号…,在函数原型中,用于将函数表示为可变参数.也就是说,它允许将可变数量的参数传递给函数.在这种形式中,函数必须为用户定义一些方式来准确指定它们呈现的参数数量,因为C中的可变参数库函数无法动态地确定此信息. 例 我看到有人的C代码有如下的函数声明:

voID information_log( const char* fmt,...)

或像块一样

catch(...){}

这是什么意思?

解决方法 省略号…,在函数原型中,用于将函数表示为可变参数.也就是说,它允许将可变数量的参数传递给函数.在这种形式中,函数必须为用户定义一些方式来准确指定它们呈现的参数数量,因为C中的可变参数库函数无法动态地确定此信息.

例如,stdio函数printf是原型的一个这样的函数:

int printf(const char *format,...);

据推测,根据两个原型之间的相似之处,您描述的information_log函数旨在反映printf的大部分功能,甚至可能在内部使用printf或其中一个表兄弟.

以下是如何实现可变参数函数的示例:

// cstdarg provIDes access to the arguments passed to the ellipsis#include <cstdarg> // or (#include <stdarg.h>)#include <cstdio>#include <cstring>// Concatenates as many strings as are presentvoID concatenate(char ** out,int num_str,...){    // Store where the arguments are in memory    va_List args;    // Find the first variadic argument,relative to the last named argument    va_start(args,num_str);    int out_len = 0;    int * lengths = new int[num_str];    char ** strings = new char*[num_str];    // Extract the strings from the variadic argument List    for(int i = 0; i < num_str; i++)    {        // Specify the position in the argument List and the type        // Note: You must kNow the type,stdarg can't detect it for you        strings[i] = va_arg(args,char *);        lengths[i] = strlen(strings[i]);        out_len += lengths[i];    }    // Concatenate the strings    int dest_cursor = 0;    (*out) = new char[out_len + 1];    for(int i = 0; i < num_str; i++)    {        strncpy( (*out) + dest_cursor,strings[i],lengths[i]);        dest_cursor += lengths[i];    }    (*out)[dest_cursor] = '[+++]';    // Clean up    delete [] strings;    delete [] lengths;    va_end(args);}int main(){    char * output = NulL;    // Call our function and print the result    concatenate(&output,5,"The ","quick"," brown ","fox ","jumps!\n");    printf("%s",output);    delete [] output;    return 0;}
总结

以上是内存溢出为你收集整理的c – 函数原型中的“…”全部内容,希望文章能够帮你解决c – 函数原型中的“…”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
c – 函数原型中的“…”_C_内存溢出

c – 函数原型中的“…”

c – 函数原型中的“…”,第1张

概述我看到有人的C代码有如下的函数声明: void information_log( const char* fmt , ...) 或像块一样 catch(...){} 这是什么意思? 省略号…,在函数原型中,用于将函数表示为可变参数.也就是说,它允许将可变数量的参数传递给函数.在这种形式中,函数必须为用户定义一些方式来准确指定它们呈现的参数数量,因为C中的可变参数库函数无法动态地确定此信息. 例 我看到有人的C代码有如下的函数声明:

voID information_log( const char* fmt,...)

或像块一样

catch(...){}

这是什么意思?

解决方法 省略号…,在函数原型中,用于将函数表示为可变参数.也就是说,它允许将可变数量的参数传递给函数.在这种形式中,函数必须为用户定义一些方式来准确指定它们呈现的参数数量,因为C中的可变参数库函数无法动态地确定此信息.

例如,stdio函数printf是原型的一个这样的函数:

int printf(const char *format,...);

据推测,根据两个原型之间的相似之处,您描述的information_log函数旨在反映printf的大部分功能,甚至可能在内部使用printf或其中一个表兄弟.

以下是如何实现可变参数函数的示例:

// cstdarg provIDes access to the arguments passed to the ellipsis#include <cstdarg> // or (#include <stdarg.h>)#include <cstdio>#include <cstring>// Concatenates as many strings as are presentvoID concatenate(char ** out,int num_str,...){    // Store where the arguments are in memory    va_List args;    // Find the first variadic argument,relative to the last named argument    va_start(args,num_str);    int out_len = 0;    int * lengths = new int[num_str];    char ** strings = new char*[num_str];    // Extract the strings from the variadic argument List    for(int i = 0; i < num_str; i++)    {        // Specify the position in the argument List and the type        // Note: You must kNow the type,stdarg can't detect it for you        strings[i] = va_arg(args,char *);        lengths[i] = strlen(strings[i]);        out_len += lengths[i];    }    // Concatenate the strings    int dest_cursor = 0;    (*out) = new char[out_len + 1];    for(int i = 0; i < num_str; i++)    {        strncpy( (*out) + dest_cursor,strings[i],lengths[i]);        dest_cursor += lengths[i];    }    (*out)[dest_cursor] = '';    // Clean up    delete [] strings;    delete [] lengths;    va_end(args);}int main(){    char * output = NulL;    // Call our function and print the result    concatenate(&output,5,"The ","quick"," brown ","fox ","jumps!\n");    printf("%s",output);    delete [] output;    return 0;}
总结

以上是内存溢出为你收集整理的c – 函数原型中的“…”全部内容,希望文章能够帮你解决c – 函数原型中的“…”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1223898.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存