So I have this function (makeStruct) that is able to take in ONE string and prints out the elements of a struct. 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. 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.
So now, how does one properly initialize an array containing multiple strings, and then pass those strings into my makeStruct function?