-2

I want to have an array of pointers to strings,and just get the space necessary for each string. I know malloc and getc are required but not familiar with the use of them.

Here is part of my code. It gives the error message "Segmentation fault" ....

char **allstrs;
char *one_str;
int totstrs=0,current_size= INITIALSIZE;

allstrs = (char **)malloc(current_size*sizeof(char*)); //dynamic array of strings
while(getstr(one_str)!=EOF){
    if(totstrs == current_size){
        current_size *=2;
        allstrs = realloc(allstrs, current_size*sizeof(char*));
    }
    strcpy(allstrs[totstrs],one_str);
    printf("String[%d] is : %s\n",totstrs,allstrs[totstrs]);
    totstrs ++;

}
free(allstrs);   //deallocate the segment of memory

return 0;

and the function getstr called

char c; 
int totchars=0, current_size=INITIALCHARS;

str = (char*)malloc(sizeof(char));
while(c!='\n'){
    c = getc(stdin);     
    if(c==EOF){
        return EOF;
    }

    if(totchars == current_size){
        current_size *=2;
            str = (char*)realloc(str,current_size*sizeof(char));
    }
        str[totchars] = c;  //store the newly-read character    
    totchars++;
}
str[totchars]='\0';   //at the end append null character to mark end of string

return 0;

}

  • 2
    `getstr(char *)` change to `getstr(char **)`, and `strcpy(allstrs[totstrs],one_str);` to `allstrs[totstrs] = one_str;`, `char c;` to `int c;`, `totchars == current_size` to `totchars == current_size-1` at `getstr` – BLUEPIXY Aug 05 '15 at 14:03
  • 2
    Standard warning: Do not cast `void *` as returned by `malloc` & friends! C is not C++. – too honest for this site Aug 05 '15 at 14:18
  • 1
    As you said, you have two questions. One for malloc and one for getc. You should study both of those and ask questions on their use. As your question stands, it's almost as if you are asking us to rewrite your code for you. – Rob Aug 05 '15 at 14:18

1 Answers1

2

You're not allocating any memory for the destination string here:

strcpy(allstrs[totstrs],one_str);

At this point allstrs[totstrs] is just a wild pointer.

Allocate some memory for the destination string first:

allstrs[totstrs] = malloc(strlen(one_str) + 1);
strcpy(allstrs[totstrs],one_str);

And don't forget to free all these strings later when you're done, of course (prior to free(allstrs)).

(Alternatively take @BLUEPIXY's advice and just assign the pointer you allocated in getstr instead of using strcpy - this is probably a better solution, depending on what else you might be trying to do).


Note also that you have a few bugs in your getstr function:

(1)

char c;

should be initialised, e.g.:

char c = '\0'; 

(or some other suitable character).

(2)

str = (char*)malloc(sizeof(char));

should be:

str = malloc(current_size);

(3) It looks like you're not allocating the addition char necessary for storing the terminating '\0'.


General note: never cast the result of malloc in C.
Community
  • 1
  • 1
Paul R
  • 208,748
  • 37
  • 389
  • 560