store strings in the Char array in C & amp; segmentation error

advertisements

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;
}


 char *productName[100];

productName is an array of pointers and they are not initialised to point any valid memory locations.

scanf("%s", productName[i]);

And taking input here is causing you segmentation fault.