0

I can't figure out how to save a user input character string.

I want to ask the user for two separate string variables (a word) and store them for later use as reference and query variables. I thought of using the scanf to obtain the user input and %s for storing the variables.

I either get segmentation fault or wrong output depending on how I modify the code. What am I doing wrong?

#include <stdio.h>
#include <errno.h>
#include <string.h>

int main(void)
{

  char *reference, *query;

  printf("\n Enter reference genome file name:  ");
  scanf("%s", reference);

  printf("\n Enter sequence query file name:  ");
  scanf("%s", query);

  printf("\n\n Reference file used was: %s\n", reference);
  printf("Query sequence file used was: %s\n", query);

  return 0;
}
alk
  • 69,737
  • 10
  • 105
  • 255
bretonics
  • 383
  • 6
  • 18

2 Answers2

2

Instead of having a char* you should allocate with char[] (array of characters).

Declare your strings like this:

char reference[80];
char query[80];

This way, the computer knows how much space to save for the strings. That's why you were getting segmentation faults before, because you had a pointer to point to the beginning of a string, but no memory to store it.

I changed the code to:

#include <stdio.h>

int main() {
    char reference[80];
    char query[80];

    printf("\nEnter reference genome file name:  ");
    scanf("%s", reference);

    printf("\nEnter sequence query file name:  ");
    scanf("%s", query);

    printf("\nReference file used was: %s\n"
           "Query sequence file used was: %s\n", reference, query);

    return 0;
}

I compiled with gcc and a sample run:

Enter reference genome file name:  file1.txt

Enter sequence query file name:  file2.txt

Reference file used was: file1.txt
Query sequence file used was: file2.txt
Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
  • When I do this I get incorrect parsing, meaning, that the name it prints for the reference file is mixed with the query file name. Also, I get segmentation fault: – bretonics Sep 28 '13 at 04:27
  • Thanks! Do you also get this warning when compiling with gcc? I get this warnings for both reference and query variables... `warning: format specifies type 'char *' but the argument has type 'char **' [-Wformat] scanf("%s", reference);` – bretonics Sep 28 '13 at 04:47
  • I am not able to get that warning with `-Wall` or `-Wformat`. I'm using a kinda old version, 4.2.1. – Prashant Kumar Sep 28 '13 at 04:49
  • Ok, thanks! Is't %s pointing to a char type? Why would the storing variables `reference` and `query` char ** type?...do you know? – bretonics Sep 28 '13 at 04:55
  • I'm not quite sure what you mean. To read a string, we use `%s` and then put the string variable into the `scanf`, like this example: http://en.cppreference.com/w/cpp/io/c/fscanf – Prashant Kumar Sep 28 '13 at 05:03
  • This answer suggests to replace one problem with another, which is bad. Don't use `scanf()`. Just don't use it. It's evil. It's insecure. It's quirky. It's counter-intuitive. Use `fgets(buf, sizeof buf, stdin)` instead. –  Sep 28 '13 at 05:09
2

Here:

char *reference, *query;

You have not allocated any storage for the strings. You could do it this way:

char reference[100], query[100];

This assumes you know in advance the maximum input size, and you will need to take additional steps to make sure you don't overflow the buffers, such as by telling scanf the maximum size:

scanf("%99s", reference);

You can check the return value to see if the user typed too much, so you know if it gets truncated.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • What if I don't know the input size? – bretonics Sep 28 '13 at 04:26
  • Ok, this seemed to work, but how come I get a Warning when compiling for both reference and query variables: `warning: format specifies type 'char *' but the argument has type 'char **' [-Wformat] scanf("%s", reference);` – bretonics Sep 28 '13 at 04:32
  • John: please don't answer obvious duplicates, vote for closure instead. –  Sep 28 '13 at 05:06
  • @bretonics: you need to **think** about the code you are using instead of blindly copy-pasting. –  Sep 28 '13 at 05:07
  • @H2CO3: I actually tried to search for a good duplicate for this one but came up short ("char*" doesn't work very well in Google). Anyway, I voted to close now, and surely wish I hadn't answered. ;-) – John Zwinck Sep 28 '13 at 14:01