0

I am trying to teach myself C language. My transition from C has been rough so far. I am doing a problem from the Book "Intro to C language" and there is a problem that should read a line from the user and then ask the user for an int. My job was to find that particular word corresponding to the int.

Example

Hello World, How are you all doing. // This is the line being read

2 // the word to be printed to the console

Expected Output

World

Below is a snippet of my code that i think is giving me the bus error.

void corrspndToInt (char *theLine, int word, char *result ) {

    int start;
    int end;
    int spaces = 1;
    result = "";

    //int i;

    //search for the nth non-blank character

    for(start = 0; start < strlen(theLine) && (spaces < word); start++ ) {

        if( isspace ( theLine[ start ] ) ) {

            spaces++;
        } // end if

    } // end for loop

    //only need to continue if we haven't gone past the end of the string

    if( start < strlen(theLine) ) {

        //the next blank character is the end

        for ( end = start; end < strlen(theLine) && ( !isspace ( theLine[ end ] ) ) ; end++ ) {

        ;

        } // end for loop

        //we now have the word

        result = strncpy ( result, theLine+start, end - start); 

     } // end if

}

This is how i am printing the result in main function.

printf("%s\n", result );

Now that i have been teaching C language to myself and through SO i learned that returning char arrays should not be done. There were two methods dynamic allocation using function malloc and there was another method which is the one i implemented in my code. I have checked my other method and there seems to be nothing wrong with that. The problem here is with this method as it is not getting the correct word.

I have been trying to debug my program but apparently Mac doesn't accept the gdb command. I have installed the command line tools and everything else but it still says

Command not found

Can anyone please guide me in the right direction here? I have been overthinking this problem from the last 1 hour now.

Saad
  • 399
  • 8
  • 25
  • It would be easier to get help if you post a [mcve]. – Spikatrix Feb 09 '17 at 04:11
  • 1
    `result = "";` : I think this is bad idea. – BLUEPIXY Feb 09 '17 at 04:19
  • Should i use a for loop to manually assign every index a space? Also what could be the reason for bus error. I have checked my other method and it is working fine. The problem seems to be with this one. @BLUEPIXY – Saad Feb 09 '17 at 04:27
  • In this case, such initialization is not necessary. `result = "";` change the address pointed to by the pointer. This same such as `corrspndToInt (line, 2, "")`. – BLUEPIXY Feb 09 '17 at 04:32
  • So you mean that they are the same. Okay. i'll change that. I get the idea that every time we call a char array we are technically calling it like this `&array[0]` so by your logic, the first element will be space. Correct me if i am wrong with my basics? @BLUEPIXY – Saad Feb 09 '17 at 04:38
  • 1
    If you know the length and location of a word, do the following. `strncpy(result, position_of_top_of_word/* theLine+start */, length_of_word/* end - start */); result[length_of_word] = '\0';` – BLUEPIXY Feb 09 '17 at 04:45
  • Also see [Mysterious crash or “segmentation fault” when data is copied/scanned to an uninitialized pointer](http://stackoverflow.com/questions/37549594/mysterious-crash-or-segmentation-fault-when-data-is-copied-scanned-to-an-unini). – Lundin Feb 09 '17 at 08:00

1 Answers1

0

First, you must allocate enough space for result to hold the word, meaning

result = malloc (sizeof(char) * (length_of_the_word+1));

given that you have found out the length of the word you want to copy. One extra because we want to append terminating \0 character at the end of the string when you use strncpy

From the function it is unclear that you have allocated space for result or not. I hope this should help you complete the task.

Rishi
  • 1,387
  • 10
  • 14
  • 1
    I have allocated space in my `main` method. I don't want to use `malloc` function that is the reason i implemented the above way of returning a char array. – Saad Feb 09 '17 at 04:24