[英]How to pass an array of strings to a function in 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\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]);
}
嘗試
void makeStruct(char* str[],int number_of_strings){
...
}
然后通過訪問每個字符串
char * a = str[i];
我的范圍從0到number_of_strings-1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.