I'm looking to sort a recipe list. The recipe list is an array that contains recipe names as well as their ingredients, recipe names begin with 0 and ingredients begin with 1. I want to print out the available recipes in a list, but I'm getting a segmentation fault, is this a correct way to sort the list? Additionally, is it possible to print the item in the list without the 0 in front of it? Here is the code for the printing:
#include <stdio.h>
#include "rawRecipes.h"
#include <string.h>
void listRecipes(void){
int n = sizeof(rawRecipes);
int i,j;
//temp place for putting string
char temp[30];
//iterate through recipe list array
for (i=0; i<sizeof(rawRecipes); i++){
//copies each item in recipe list array to temp
strcpy(rawRecipes[i], temp);
for (j=0; j<30; j++){
if (temp[j] == 0){
break;
printf("%s", rawRecipes[i]);
}
}
}
}
int main(void) {
listRecipes();
return 0;
}
and here is the code for the list used in the header file:
char *rawRecipes[]={"0Broccoli Coleslaw","1olive oil","1white vinegar","1broccoli","0Creamy Broccoli Salad","1broccoli","1white sugar","1red onion","1white wine vinegar","0Minnesota Broccoli Salad","1eggs","1broccoli","1red onion",""};
Expected output would look something like: Broccoli Coleslaw \n Creamy Broccoli Salad \n Minnesota Broccoli Salad
edit I've changed the code and it seems to be printing out the correct items, but multiple times, how do I alter the code so it only prints once?
#include <stdio.h>
#include "rawRecipes.h"
#include <string.h>
void listRecipes(void){
int n = sizeof(rawRecipes);
int i,j;
//temp place for putting string
char temp[30];
//iterate through recipe list array
for (i=0; i<14; i++){
//copies each item in recipe list array to temp
strcpy(temp, rawRecipes[i]);
for (j=0; j<30; j++){
if (temp[j] == '1'){
break;
}
printf("%s\n", rawRecipes[i]);
}
//printf("%s", rawRecipes[i]);
}
}
int main(void) {
listRecipes();
return 0;
}
rawRecipes? The arguments instrcpyare backwards, the destination comes first, source is second.sizeof(rawRecipes)returns the size of the array in bytes, not the number of elements.tempin the first place? Just userawRecipes[i][0]printf("%s", &rawRecipes[i][1])