如何将字符串数组传递给C中的函数?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将字符串数组传递给C中的函数?相关的知识,希望对你有一定的参考价值。

所以我有这个函数(makeStruct)能够接受一个字符串并打印出结构的元素。例如,我传入的字符串是"a = 2.b, 1.d, 3.d; 4.o; milk cheese",它通过我的函数将每个数字,字母和单词存储到我创建的相应struct元素中。这非常好,但只有一个字符串:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stopPoints {
    int  weights[10];
    char connectingPoints[10];
    char *items[30];
    int startBool;
};


void makeStruct(char str[]){

    struct stopPoints myPoint;
    char *arr[30];
    char * pch;
    pch = strtok (str," ;=,.-");
    arr[0] = pch;
    int i=0;


  for (pch; pch != NULL; i++){
    pch = strtok (NULL, " ;=,.-");
    arr[i+1] = pch;
    //printf("%s
", arr[i]);

  }
  printf("
");
  char letters[10];
  int numbers[10];
  char *strings[10] = {NULL};
  int p, iter=0, iter2=0, iter3=0, val[10];
  for (p=0; arr[p] != NULL; p++){
      //if its a string
      if (isalpha(*arr[p]) && strlen(arr[p]) >=2 ){
        //printf("%s is a string
", arr[p]);
        myPoint.items[iter] = arr[p];
        iter++;
      }
      //if its just a letter
      else if (isalpha(*arr[p]) && strlen(arr[p]) ==1){
        //printf("%s is a letter
", arr[p]);
        letters[iter2] = *arr[p];
        myPoint.connectingPoints[iter2] = letters[iter2];
        iter2++;
        //printf("letter
");
      }
      //if its a number
      else if (isdigit(*arr[p])){
        //printf("%s is a number
", arr[p]);
        val[iter3] = atoi(arr[p]);
        myPoint.weights[iter3] = val [iter3];
        iter3++;
      }
  }



  printf("%s %s
",  myPoint.items[0], myPoint.items[1]);

}


int main ()
{
        char str[] = "a = 2.b, 1.d, 3.d; 4.o; milk cheese";
        makeStruct(str);
  return 0;
}

现在,我希望能够将多个字符串传递给此函数。这就是我的问题所在。我尝试了几种不同的方法,但我不明白我哪里出错了。请看下面的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stopPoints {
    int  weights[10];
    char connectingPoints[10];
    char *items[30];
    int startBool;
};


void makeStruct(char str[]){

    struct stopPoints myPoint;
    char *arr[30];
    char * pch;
    pch = strtok (str," ;=,.-");
    arr[0] = pch;
    int i=0;


  for (pch; pch != NULL; i++){
    pch = strtok (NULL, " ;=,.-");
    arr[i+1] = pch;
    //printf("%s
", arr[i]);

  }
  printf("
");
  char letters[10];
  int numbers[10];
  char *strings[10] = {NULL};
  int p, iter=0, iter2=0, iter3=0, val[10];
  for (p=0; arr[p] != NULL; p++){
      //if its a string
      if (isalpha(*arr[p]) && strlen(arr[p]) >=2 ){
        //printf("%s is a string
", arr[p]);
        myPoint.items[iter] = arr[p];
        iter++;
      }
      //if its just a letter
      else if (isalpha(*arr[p]) && strlen(arr[p]) ==1){
        //printf("%s is a letter
", arr[p]);
        letters[iter2] = *arr[p];
        myPoint.connectingPoints[iter2] = letters[iter2];
        iter2++;
        //printf("letter
");
      }
      //if its a number
      else if (isdigit(*arr[p])){
        //printf("%s is a number
", arr[p]);
        val[iter3] = atoi(arr[p]);
        myPoint.weights[iter3] = val [iter3];
        iter3++;
      }
  }



  printf("%s %s
",  myPoint.items[0], myPoint.items[1]);

}


int main ()
{



    char *str[9];
    str[0] = "a = 2.b, 1.d, 3.d; 4.o; milk cheese";
    str[1] = "b = 2.a, 1.e, 2.c; water juice drinks";
    str[2] = "c = 2.b, 1.f; chips snacks";
    str[3] = "d = 1.a, 1.g; bread cereal pasta";
    str[4] = "e = 1.h, 1.b; meat chicken fish";
    str[5] = "f = 1.i, 1.c; oils sauces condiments";
    str[6] = "g = 1.j, 1.d; soup canned_goods";
    str[7] = "h = 1.k, 1.e; produce";
    str[8] = "i = 1.l, 1.f; beer";

    //char str[] = "a = 2.b, 1.d, 3.d; 4.o; milk cheese";

    int i;
    for (i=0; i<9; i++){
        makeStruct(*str);

    }

  return 0;
}

所以你可以看到,我正在尝试接受str[0],输出我正在打印的语句,然后使用循环重复该过程以传入str[1]str[2], str[3],等等等等。

那么现在,如何正确初始化包含多个字符串的数组,然后将这些字符串传递给我的makeStruct函数?

答案

在原始代码中执行此操作时:

char str[] = "a = 2.b, 1.d, 3.d; 4.o; milk cheese";

您正在创建一个char数组并使用给定字符串常量的内容对其进行初始化。这很好,因为即使无法更改字符串文字,str只包含该字符串文字中的内容的副本。

但是当你这样做时:

char *str[9];
str[0] = "a = 2.b, 1.d, 3.d; 4.o; milk cheese";
str[1] = "b = 2.a, 1.e, 2.c; water juice drinks";
...

您正在创建一个指针数组,并为每个指针指定字符串文字的地址。因此,当您将*str传递给函数时,它会尝试通过strtok函数修改字符串文字,这是不允许的。

您应该创建一个使用字符串常量初始化的char的2D数组:

char str[9][50] = {
    "a = 2.b, 1.d, 3.d; 4.o; milk cheese",
    "b = 2.a, 1.e, 2.c; water juice drinks",
    "c = 2.b, 1.f; chips snacks",
    "d = 1.a, 1.g; bread cereal pasta",
    "e = 1.h, 1.b; meat chicken fish",
    "f = 1.i, 1.c; oils sauces condiments",
    "g = 1.j, 1.d; soup canned_goods",
    "h = 1.k, 1.e; produce",
    "i = 1.l, 1.f; beer"
};

此外,您的循环总是在数组的第一个元素中发送:

for (i=0; i<9; i++){
    makeStruct(*str);
}

索引数组以传递连续的元素:

for (i=0; i<9; i++){
    makeStruct(str[i]);
}
另一答案

尝试

 void makeStruct(char* str[],int number_of_strings){
       ...
    }

然后通过访问每个字符串

 char * a = str[i];

我的范围从0到number_of_strings-1

以上是关于如何将字符串数组传递给C中的函数?的主要内容,如果未能解决你的问题,请参考以下文章

将二维数组指针传递给c ++中的函数[重复]

如何将 C# 中的二维数组传递给 C++ DLL?

通过引用将字符串数组传递给 C 函数

如何将多维数组传递给 C 和 C++ 中的函数

C,如何将多维数组传递给 CLR/类库项目中的函数

C++ 中的高效循环缓冲区,将传递给 C 风格的数组函数参数