segmentation fault(coredump)
I think that I have the definition of segmentation fault covered and why other people are receiving this error when I checked on past posts. However, I have no idea why Im getting it....can someone help! Thanks in advance!!!
Code:
program7.c
#include "program7.h"
int main(int argc, char *argv[])
{
char **StringList;
int i, num, length;
if (argc != 3)
printf("Insufficient arguments\n");
num = atoi(argv[1]);
length = atoi(argv[2]);
StringList = (char **)calloc(num, sizeof(char *));
srand(time(NULL));
for (i = 0; i < num; i++){
StringList[i] = RandomString(length);
}
printf("Unsorted array:%s\n", StringList[i]);
/*StringSort(num, StringList)*/
/*printf("Sorted array:%s\n", StringList[i];
/*4*/
return 0;
}
****************************************************
RandomString.c
#include "program7.h"
char *RandomString(int length){
int i, size;
char *string;
size = 1 + rand() % length;
string = (char *)calloc(size + 1, sizeof(char));
for(i = 0; i < size; i++){
string[size] = (char)((rand() % 26) + 65);
}
string[size] = '\0';
return string;
} /* End of RandomString function */
Re: segmentation fault(coredump)
Quote:
Originally posted by chauncey005
Code:
if (argc != 3)
printf("Insufficient arguments\n");
If you are testing for "Insufficient arguments", why do you continue merrily on your way and process the code anyway? You should test for the error and exit, shouldn't you?