1

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?

0

2 Answers 2

3

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. 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.

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.

You should instead create a 2D array of char initialized with the string constants:

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]);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.