I'm trying to read input & storing strings in a char array. However, a segmentation fault is returned by the compiler. In addition, storing the string does not work and causes the execution file to crash. Here is my code:
#include <stdlib.h>
#include <math.h>
/*scan functie*/
int inputProducts(int *resourceCost, int *profit, char **productName) {
int amount, i;
printf("number of products: \n");
scanf("%d", amount);
for (i = 0; i < amount; i++) {
printf("product: \n");
scanf("%s", productName[i]);
printf("resource cost for %s: \n", productName[i]);
scanf("%d", &resourceCost[i]);
printf("profit for %s: \n", productName[i]);
scanf("%d", &profit[i]);
}
return amount;
}
int main(int argc, char *argv[]) {
int amount;
int resourceCost[100],profit[100];
char *productName[100];
amount = inputProducts(resourceCost, profit, productName);
return 0;
}
resourceCost[100], which would only support indexes 0->99.scanfalways, because "funny" things will happen when parsing fails but you ignore the error.