P99延迟代表什么?我在关于应用程序性能的讨论中一直听到这个,但是找不到可以讨论这个问题的在线资源.
编辑3:对于代码本身,一起检查第一个答案或这篇文章的结尾.
正如标题中所述,我试图找到一种方法来判断是否将可选参数传递给函数.我想要做的就是几乎所有动态语言都处理它们的子串函数.下面是我的目前,但它不起作用,因为我不知道如何判断/何时使用该东西.
char *substring(char *string,unsigned int start, ...){
va_list args;
int unsigned i=0;
long end=-1;
long long length=strlen(string);
va_start(args,start);
end=va_arg(args,int);
va_end(args);
if(end==-1){
end=length;
}
char *to_string=malloc(end);
strncpy(to_string,string+start,end);
return to_string;
}
Run Code Online (Sandbox Code Playgroud)
基本上我想仍然能够不包括我想要的字符串的长度,只是让它到字符串的末尾.但我似乎找不到办法做到这一点.由于也无法知道在C中传递的参数数量,因此我首先想到了这一点.
编辑:这里做的新方法是当前的代码.
#define substring(...) P99_CALL_DEFARG(substring, 3, __VA_ARGS__)
#define substring_defarg_2 (0)
char *substring(char *string,unsigned int start, int end){
int unsigned i=0;
int num=0;
long long length=strlen(string);
if(end==0){
end=length;
}
char *to_string=malloc(length);
strncpy(to_string,string+start,end);
return to_string;
}
Run Code Online (Sandbox Code Playgroud)
然后在一个文件中我调用test.c来查看它是否有效.
#include "functions.c"
int main(void){
printf("str:%s",substring("hello world",3,2));
printf("\nstr2:%s\n",substring("hello world",3));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
functions.c有一个包含for functions.h的函数,它包含了所有需要的东西.这是clang输出(因为clang似乎通常会提供更多细节.
In file …Run Code Online (Sandbox Code Playgroud)