我想通过指针传递一些可变参数,但我还没有完全明白。 解析器不工作,但这不是我的问题。 我使用C的子集,但不用担心语法或词法。更改返回可变参数函数指针
它应该像被使用:
void functionX(void){
action[100]="GO_PLAY(12345,23)";
char id[10];
char offset[10];
ParseCommand("GO_PLAY",action,2,&id,&offset);
// after this id should be "12345" and offset should be "23"
}
bool ParseCommand(char *command, char * buffer,int count, ...){
bool returnvalue=FALSE;
int command_len=O_strlen(command);
int buffer_len=O_strlen(buffer);
int j=command_len+1;
va_list ap;
int parameter=0;
char *parsed_text;
va_start(ap, count); /* Initialize the argument list. */
while(parameter<count) {
while(buffer[j]!=','&& buffer[j]!=')') //search argument
j++;
if(j<buffer_len) { // argument found
calloc(MODULE,parsed_text,j-(command_len+1),sizeof(char));
substr(command_len+1,j,buffer,parsed_text,buffer_len);
*va_arg(ap,char*)=parsed_text; // why isnt this working?
parameter++;
j++; // to continue to search
}
}
leave:
va_end (ap);
return returnvalue;
}
什么是你的代码做什么,以及如何从你所希望看到的有什么不同? – 2011-03-31 13:49:22
而且:那些'calloc'和'substr'调用是什么? C标准库中有一个'calloc',但它只有两个参数。 – 2011-03-31 13:52:26
我重新格式化了你的代码。我希望你不介意。 – 2011-03-31 14:00:17