[英]How to pass an array of strings to a function in C?
So I have this function (makeStruct) that is able to take in ONE string and prints out the elements of a struct. 所以我有这个函数(makeStruct)能够接受一个字符串并打印出结构的元素。 For example, my string that I pass in is
"a = 2.b, 1.d, 3.d; 4.o; milk cheese"
and it goes through my function that stores every number, letter, and word into the appropriate struct element that I have created. 例如,我传入的字符串是
"a = 2.b, 1.d, 3.d; 4.o; milk cheese"
,它通过我的函数将每个数字,字母和单词存储到相应的中我创建的struct元素。 This works perfectly fine but only with one string: 这非常好,但只有一个字符串:
#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;
}
Now, I want to be able to pass multiple strings into this function. 现在,我希望能够将多个字符串传递给此函数。 This is where my problem is.
这就是我的问题所在。 I've tried several different approaches but I am not understanding where I'm going wrong.
我尝试了几种不同的方法,但我不明白我哪里出错了。 Please take a look at the code below:
请看下面的代码:
#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;
}
So as you can see, I'm trying to take in str[0]
, output the statement that I am printing, and then repeat the process using a loop to pass in str[1]
, str[2], str[3],
etc. and so on and so forth. 所以你可以看到,我正在尝试接受
str[0]
,输出我正在打印的语句,然后使用循环重复该过程以传入str[1]
, str[2], str[3],
等等等等等等。
So now, how does one properly initialize an array containing multiple strings, and then pass those strings into my makeStruct function? 那么现在,如何正确初始化包含多个字符串的数组,然后将这些字符串传递给我的makeStruct函数?
When you do this in your original code: 在原始代码中执行此操作时:
char str[] = "a = 2.b, 1.d, 3.d; 4.o; milk cheese";
You're creating a char
array and initializing it with the contents of the given string constant. 您正在创建一个
char
数组并使用给定字符串常量的内容对其进行初始化。 This is fine because even though a string literal can't be changed str
just contains a copy of what's in that string literal. 这很好,因为即使无法更改字符串文字,
str
只包含该字符串文字中的内容的副本。
But when you do this: 但是当你这样做时:
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";
...
You're creating an array of pointers and assigning each of those pointers the address of a string literal. 您正在创建一个指针数组,并为每个指针指定字符串文字的地址 。 So when you pass
*str
to your function, it attempts to modify the string literal via the strtok
function which is not allowed. 因此,当您将
*str
传递给函数时,它会尝试通过strtok
函数修改字符串文字,这是不允许的。
You should instead create a 2D array of char
initialized with the string constants: 您应该创建一个使用字符串常量初始化的二维
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"
};
Also, your loop is always sending in the first element of the array: 此外,您的循环总是在数组的第一个元素中发送:
for (i=0; i<9; i++){
makeStruct(*str);
}
Index the array to pass in successive elements: 索引数组以传递连续的元素:
for (i=0; i<9; i++){
makeStruct(str[i]);
}
Try 尝试
void makeStruct(char* str[],int number_of_strings){
...
}
then access each string by 然后通过访问每个字符串
char * a = str[i];
i ranging from 0 to number_of_strings-1 我的范围从0到number_of_strings-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.