0

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;  
}
3
  • 1
    compilers don't return segfaults. that's a runtime error. but you're almost certainly exceeding the size of your arrays, e.g. trying to use resourceCost[100], which would only support indexes 0->99. Commented Oct 15, 2014 at 20:28
  • You'd better check return value of scanf always, because "funny" things will happen when parsing fails but you ignore the error. Commented Oct 15, 2014 at 20:33
  • A mistake exists in taking the value of amount aswell. I decided to make the program print it, when I type 5 for the value of amount, it prints 2130567168.... Commented Oct 15, 2014 at 20:43

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

how would I be able to fix this? Will char productname[100] work?
Hint: Store the address returned by malloc at each index before taking input.
Thanks for your solution. I will try this instead. The idea of this exercise however is to store a string, instead of a pointer. But when I initialize the char array without the * , the inputProducts line can't pass the char array (error: incompatible pointer type)
0
scanf("%d", &amount);   
for (i = 0; i < amount; i++)    {
     productName[i] = (char *)malloc(sizeof(char));
     printf("product: \n");     
     scanf("%s", productName[i]);

you are not allocating any space for the strings to be stored in productName.. try adding this line of code inside your for loop. It will make space on the heap for each of your strings

productName[i] = (char *)malloc(sizeof(char));

3 Comments

I will try that, however , running the program causes it to crash after typing and entering the amount... Is anything wrong there?
**corrected my answer. I just ran your program with the correction using malloc() and it worked fine.
Thankyou. it works for me too now. The only problem is that I'm storing pointer, as where I would like to store actual values..

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.