【问题标题】:How to pass an array of strings to a function in C?如何将字符串数组传递给C中的函数?
【发布时间】:2019-03-18 00:23:06
【问题描述】:

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

#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]str[2], str[3], 等等。等等。

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

【问题讨论】:

    标签: c arrays string function struct


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      试试

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

      然后通过

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

      i 范围从 0 到 number_of_strings-1

      【讨论】:

        猜你喜欢
        • 2017-04-05
        • 2015-09-09
        • 2013-06-27
        • 1970-01-01
        • 2017-06-03
        • 1970-01-01
        • 1970-01-01
        • 2016-12-08
        相关资源
        最近更新 更多