Thursday 15 January 2015

storing Strings into Char array in C & segmentation fault -



storing Strings into Char array in C & segmentation fault -

i'm trying read input & storing strings in char array. however, segmentation fault returned compiler. in addition, storing string not work , causes execution file crash. here 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); (i = 0; < amount; i++) { printf("product: \n"); scanf("%s", productname[i]); printf("resource cost %s: \n", productname[i]); scanf("%d", &resourcecost[i]); printf("profit %s: \n", productname[i]); scanf("%d", &profit[i]); } homecoming amount; } int main(int argc, char *argv[]) { int amount; int resourcecost[100],profit[100]; char *productname[100]; amount = inputproducts(resourcecost, profit, productname); homecoming 0; }

char *productname[100];

productname array of pointers , not initialised point valid memory locations.

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

and taking input here causing segmentation fault.

c arrays string char segmentation-fault

No comments:

Post a Comment