c - 将多个单独的字符串传递给函数

标签 c

我有几个字符串正在尝试传递到函数中进行处理。函数参数必须保持不变。我认为循环字符串并调用函数将是可行的方法,但我不确定如何将几个不同的命名字符串传递到函数中。这就是我的开始

#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/

相关文章:

c - C 中数组元素超出索引

c - 当用户使用 GTK+2 在窗口外单击时如何关闭窗口?

c - 允许在不告知数据类型的情况下传递变量?

两个线程能否同时读取同一个const内存块

C - 需要将 unsigned int 一次右移一个位置。

c - C语言求第一个正元素和最后一个负元素的位置和值

c - 转移/减少 Bison 中的冲突

c - 支持边标签的图库

objective-c - 在 Apple 上合并静态库

c - 为 Sublime Text 3 设置 C 编译器