-2

After confirming input Segmentation fault pops up and I can't see why. I was told to use fgets and sscanf in loop to read an undefined number of floats from terminal input and this is what i came up with..

Code

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define EMPTY 3.141592
#define BUFFERSIZE 100

void removeSubstring(char *s,const char *toremove){
  while( (s=strstr(s,toremove)) )
    memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
}

int main(){

    int x=0;
    int y=0;
    float a[BUFFERSIZE] = {EMPTY};
    char buffer[BUFFERSIZE] = {EMPTY};
    char *pos;
    char *start = 0;
    int space = ' ';

    printf("Input: ");
    fgets(buffer, BUFFERSIZE, stdin);

    while(x< BUFFERSIZE){
            sscanf(buffer, "%f ", &a[x]);
            pos = strchr(buffer, space);
            removeSubstring(start, pos);
            x++;
            }

    printf("Saved input: ");
    while(y<BUFFERSIZE && a[y] != EMPTY){
            printf("%.2f", a[y]);
            y++;
            }

    printf("\n");

    return 0;
}

Edit: Increased both array sizes and removed feof

  • 2
    You might like to read this: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – alk Nov 13 '17 at 08:23
  • 2
    Also have a look at this: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Fang Nov 13 '17 at 08:24
  • 1
    GDB says: `#1 0x00000000004008a7 in removeSubstring (s=0x0, toremove=0x0) at so.c:9 ` – danglingpointer Nov 13 '17 at 08:26
  • Looks like you forget to point `start` pointer to a valid memory location. Also, if `strchr()` does not find space in `buffer` then it will return `NULL`. You need to validate input arguments in `removeSubstring()`. – H.S. Nov 13 '17 at 08:33
  • Thank you alk, debugging seems like something i should really get into, Ive been working in c for only a short time so an article like the one you linked is incredibly helpful. –  Nov 13 '17 at 08:40
  • Don't forget the important conversations with the duck... They help. – David C. Rankin Nov 13 '17 at 08:46
  • 1
    You have been told to "read an undefined number of float", but Your program is only trying to read up to 10 numbers from 1 sigle line of input. Is that expected? – Bob__ Nov 13 '17 at 08:48
  • An undefined number of float type inputs from a single line in terminal, e.g. 0.1 0.2 1 2 10 20 etc., separated by whitespace characters and confirmed with Enter. I am only using the 10 for test purposes, example input would be 1 2 3 for now, i can't get past the fault so i am trying to keep it short –  Nov 13 '17 at 09:14
  • You use `BUFFERSIZE` as dimension for both the array of floats and the number of characters in the line you read. The line should probably be much longer than 10 chars. – M Oehm Nov 13 '17 at 09:19
  • #define EMPTY 3.141592 // uff can this be #define PI 3.141592 – asio_guy Nov 13 '17 at 10:35
  • Welcome to Stack Overflow! [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Nov 13 '17 at 10:48
  • Program received signal SIGSEGV, Segmentation fault. __strstr_sse42 (s1=0x0, s2=0xbffffb8b " 2 3 \n") at ../sysdeps/x86_64/multiarch/strstr.c:179 179 ../sysdeps/x86_64/multiarch/strstr.c: No such file or directory. (After Edit debug) cant really grasp it.. does the "no such file or directory" mean i am trying to access or write into a place that does not exist? –  Nov 13 '17 at 12:03
  • No, the "no such file or directory" just means that you don't have the file ../sysdeps/x86_64/multiarch/strstr.c - which you don't need. – Armali Nov 13 '17 at 12:36

1 Answers1

1

Almost certainly the problem is in the line pos = strchr(buffer, space);. If the input does not contain a space character, then pos is set equal to NULL. Passing a NULL to strstr() will likely result in a SEGV.

One Guy Hacking
  • 1,176
  • 11
  • 16
  • Yes, thank you for pointing that out, I managed to see that in debugger. Although I am using a new, rewritten version of code to solve the terminal input reading, your answer prompt me to reconsider my approach as well as the code itself. –  Nov 17 '17 at 15:04