所以我有这个函数 (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 函数中?
最佳答案
当您在原始代码中执行此操作时:
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
的二维数组,并用字符串常量初始化:
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]);
}
关于c - 如何将字符串数组传递给 C 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52785133/