Can some tell me why I am 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-24T05:04:01Z
Indexed on
2010/03/24
5:13 UTC
Read the original article
Hit count: 249
I keep on getting seg faulted after I end my first for loop, and for the life of me I don't 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 don't 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( 15*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++ )
{
scanf( "%s", picks+num_picks );
}
//this is where i seg fault
int x;
for(x=0; x<num_picks;x++)
printf("s\n", picks+x);
}
© Stack Overflow or respective owner