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

时间:2018-10-12 18:23:06

标签: c arrays string function struct

所以我有这个函数(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\n", arr[i]);

  }
  printf("\n");
  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\n", 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\n", arr[p]);
        letters[iter2] = *arr[p];
        myPoint.connectingPoints[iter2] = letters[iter2];
        iter2++;
        //printf("letter\n");
      }
      //if its a number
      else if (isdigit(*arr[p])){
        //printf("%s is a number\n", arr[p]);
        val[iter3] = atoi(arr[p]);
        myPoint.weights[iter3] = val [iter3];
        iter3++;
      }
  }



  printf("%s %s\n",  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\n", arr[i]);

  }
  printf("\n");
  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\n", 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\n", arr[p]);
        letters[iter2] = *arr[p];
        myPoint.connectingPoints[iter2] = letters[iter2];
        iter2++;
        //printf("letter\n");
      }
      //if its a number
      else if (isdigit(*arr[p])){
        //printf("%s is a number\n", arr[p]);
        val[iter3] = atoi(arr[p]);
        myPoint.weights[iter3] = val [iter3];
        iter3++;
      }
  }



  printf("%s %s\n",  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],{{1 }}等,依此类推。

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

2 个答案:

答案 0 :(得分:3)

使用原始代码执行此操作时:

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";
...

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

您应该改为创建char的二维数组,并使用字符串常量对其进行初始化:

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]);
}

答案 1 :(得分:0)

尝试

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

然后通过

访问每个字符串
 char * a = str[i];

i的范围为0到number_of_strings-1