我有几个字符串正在尝试传递到函数中进行处理。函数参数必须保持不变。我认为循环字符串并调用函数将是可行的方法,但我不确定如何将几个不同的命名字符串传递到函数中。这就是我的开始
#include <stdio.h>
void convert(char s[], int counts[]);
int main(void)
{
int aray[2];
char text0[] = "This is one of Several strings2use.";
char text1[] = "This sample has less than 987654321 leTTers.";
char text2[] = "Is thIs a string? (definitely)";
""" how do i pass each string one by one into the function? im not trying to
call the function 3 separate times for each string"""
}
void convert(char s[], int counts[])
{
"""this function grabs each string and processes it to find word counts and
character counts. does not return anything and accepts one string at a time"""
}
最佳答案
查找使用变量参数的示例代码。
#include <stdarg.h>
#include <stdio.h>
void sample (int num, char *p, ... )
{
va_list arguments;
char * temp;
int x = 0;
/* Initializing arguments to store all values after num */
va_start ( arguments, num );
printf ("Num of addr[%d]\n", num);
printf ("[%p] ", p);
for ( ; x < num-1; x++ )
{
temp = va_arg ( arguments, char* );
printf ("[%p] ", temp);
}
va_end ( arguments );
printf ("\n");
}
int main()
{
char *test[5]= {"aaa", "bbb", "ccc", "ddd", "eee"};
printf( "[%p] [%p] [%p] [%p] [%p]\n",test[0],test[1],test[2],test[3],test[4]);
sample (sizeof(test)/sizeof(test[0]), test[0],test[1],test[2],test[3],test[4] );
}
关于c - 将多个单独的字符串传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21692151/