   [ C ] Random segfaults
  I'm doing a project for school, and it was all going well until I decided to move my input code into a separate function. From then, all my world started to crumble.Basically, I have to read a line with a letter and then a random number of ints as input. To do so I've made this:char getInput ( int * args, int ** argv ) {    int i = 0;    // char *input gives a segfault with gets    char input[256], *token;    char command;    // Reads and analizes Input    gets ( input );    *args = countSpaces(input);    *argv = ( int * ) malloc ( *args * ( sizeof ( int ) ) );    printf ( "We are reading %d values\n", *args );    // Gets the command    token = strtok ( input, " ");    command = *token;    while ( i < *args ) {   token = strtok ( NULL, " ");   // segfaults appears here after the first argument ( so when i == 1 )   *argv = atoi ( token );   printf ( "Value N %d read, the value is %d\n", i+1, *argv );   i++;    }    return command;}My problems are 2:1) If I set input as a char*, gets doesn't work. In main, using a char* worked perfectly.2) If I have more than 1 parameter, the first one works but then I get a segfault when I try to read the second one.Now maybe it's not very elegant, but when this was in main it worked. Why am I receiving random segfaults for this?I've to say that I'm really used to program in c++, so maybe I've a completely wrong approach to the problem =PThanks alot in advance for any help! ^_^   
  Can you provide just enough code around that function for us to be able to compile it and run it?   ;#include <stdio.h>#include <stdlib.h>#include <string.h>int countSpaces ( char* string ) {    int i = 0, count = 0;    for ( ; string != '\0'; i++ )   if ( string == ' ' ) count ++;    return count;}char getInput ( int * args, int ** argv ) {    int i = 0;    char input[256], *token;    char command;    // Reads and analizes Input    gets ( input );    *args = countSpaces(input);    *argv = ( int * ) malloc ( *args * ( sizeof ( int ) ) );    printf ( "We are reading %d values\n", *args );    // Gets the command    token = strtok ( input, " ");    command = *token;    while ( i < *args ) {   token = strtok ( NULL, " ");   *argv = atoi ( token );   printf ( "Value N %d read, the value is %d\n", i+1, *argv );   i++;    }    return command;}int main () {char command;int n, *values;do {printf ( "?> ");command = getInput ( &n, &values );switch ( command ) {// blablabla}free ( values );} while ( command != 'q' );return 0;}This should do it ^_^   ;  I had to make a couple of changes inside getInput to work:char getInput ( int * args, int ** argv ) {    int i = 0;    char input[256], *token;    char command;    // Reads and analizes Input    gets ( input );    *args = countSpaces(input);    *argv = ( int * ) malloc ( *args * ( sizeof ( int ) ) );    printf ( "We are reading %d values\n", *args );    // Gets the command    token = strtok ( input, " ");    command = *token;    while ( i < *args ) {   token = strtok ( NULL, " ");   (*argv) = atoi ( token ); /* here */   printf ( "Value N %d read, the value is %d\n", i+1, (*argv) ); /* and here */   i++;    }    return command;}Basically, I changed *argv to (*argv) due to operator order.  I also would suggest to change *args to be countSpaces(input) + 1, otherwise you'll miss the last "part" of the command.Worth mentioning that gets() requires a *buffer*, so a simple char * won't do, unless you also malloc it.  Also, beware that gets() does not check for buffer limits and thus could be dangerous.;  Thanks alot! =DI'll keep the countSpaces as it is because it only counts the arguments, and not the command. Thus, with an input like "c 12 3 4 10" I would count 4 spaces, which are exactly the number of arguments passed.About gets, should I use fgets instead? Could that be a problem when tokenizing the string? ( because the carriage return is included in the string with fgets ).Thanks again, btw ^_^   ;Quote:Original post by SvalorzenI'll keep the countSpaces as it is because it only counts the arguments, and not the command. Thus, with an input like "c 12 3 4 10" I would count 4 spaces, which are exactly the number of arguments passed.And what about the string " c 12 3  4 10 "? Seven spaces, four arguments.   ;  Yes, but thank god I can assume my input is always right, otherwise I'd have made more checks ^_^   ;Quote:Original post by SvalorzenYes, but thank god I can assume my input is always right, otherwise I'd have made more checks ^_^But you see, there's already a standard library function that handles whitespace automatically, and converts to integer as it reads the input, and can operate directly on the input stream instead of reading into a buffer first (BTW, gets() is never safe; for any given buffer size, the user can supply a larger amount of input before the newline, and then you segfault): scanf(). It should already have been shown to you by this point in the course.   ;  I've tried to use scanf to be honest, but I don't know how to use it to input a random amount of integers.. I can't use it to read a whole line with %s because of the whitespaces, and I can't use the %d's because I don't know how many of them I'll need every time.Or.. did I miss something big on the doc?I'm now using fgets btw, since it does work anyway and it is safer =)   ;Quote:Original post by SvalorzenI've tried to use scanf to be honest, but I don't know how to use it to input a random amount of integers.. I can't use it to read a whole line with %s because of the whitespaces, and I can't use the %d's because I don't know how many of them I'll need every time.Or.. did I miss something big on the doc?I'm now using fgets btw, since it does work anyway and it is safer =)With scanf it is very easy. You can't do it all in one call, you have to break it into a loop. Do it like this:int nums[20];  /*there won't be more than 20 right?*/char c;int n = 0;/*If i understand correctly, you want to read a char and then some bunch of integers*//*assuming the next character is the char you want, and the things are sepereated by spaces*/scanf("%c", &c);while(scanf("%d", &nums[n++]) == 1);/*and done, nums has your nums, n has how many, c has your char*/