Can someone tell me why I'm seg faulting in this simple C program?

Posted by user299648 on Stack Overflow See other posts from Stack Overflow or by user299648
Published on 2010-03-23T06:49:34Z Indexed on 2010/03/23 6:53 UTC
Read the original article Hit count: 251

Filed under:
|
|
|
|

I keep on getting seg faulted, and for the life of me I dont why. The file I'm scanning is just 18 strings in 18 lines. I thinks the problem is the way I'm mallocing the double pointer called picks, but I dont know exactly why. I'm am only trying to scanf strings that are less than 15 chars long, so I don't see the problem. Can someone please help.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 100

int main( int argc,char *argv[] )
{

   char* string = malloc( sizeof(char) );
   char** picks = malloc(15*sizeof(char));
   FILE* pick_file = fopen( argv[l], "r" );
   int num_picks;

   for( num_picks=0 ; fgets( string, MAX_LENGTH, pick_file ) != NULL ; num_picks++ )
     {
       printf("pick a/an %s ", string );
       scanf( "%s", picks+num_picks );
     }

   int x;
   for(x=0; x<num_picks;x++)
     printf("s\n", picks+x);
}

© Stack Overflow or respective owner

Related posts about c

    Related posts about malloc