-1

I use this code, but it's not working.

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

int main() {
    char c, *strx = 0;
    c = getchar();
    while(c != '\n') {
        strx = strcat(strx, &c);
        c = getchar();
    }
    printf("%s\n", *strx);
    return 0;
}

How can I put a word into a string?

stavros
  • 23
  • 6
  • 2
    Can you expand on "not working"? –  Jun 12 '13 at 23:22
  • 1
    there are several examples in this question http://stackoverflow.com/questions/7709452/how-to-read-string-from-keyboard-using-c – Osama Javed Jun 12 '13 at 23:23
  • you need to allocate memory to *strx first. Try strx[somevalue] or try using malloc – priteshbaviskar Jun 12 '13 at 23:25
  • `strcat()` will not allocate memory for you. You need a big enough buffer for the string first. Also, it's easier to use [`fgets()`](http://linux.die.net/man/3/fgets) – millimoose Jun 12 '13 at 23:26
  • It stops working! I give as input "ab", press enter and then it stops. – stavros Jun 12 '13 at 23:26
  • I strongly recommend looking at something like [GLib's IO channels](https://developer.gnome.org/glib/2.36/glib-IO-Channels.html) if you want to get anything done. The library provides, amongst other creature comforts, utilities that will automatically read a line of input into a dynamically allocated string without you having to mess with managing the character buffer – millimoose Jun 12 '13 at 23:30
  • @millimoose probably not the greatest suggestion for someone trying to learn the language. – Marlon Jun 12 '13 at 23:39
  • @Marlon Depends on what the OP is trying to ultimately accomplish in C - if they need to read strings of arbitrary length the trivial answers provided here and in the duplicate won't help either. In practice one should be using appropriate libraries for tasks like this anyway, they're certainly a better suggestion than "write code with a trivial buffer overflow vulnerability", and I'm not going to withhold information about them - the OP is free to jump through low-level hoops as he sees fit. – millimoose Jun 13 '13 at 00:02

2 Answers2

1

Change

char *strx = 0

to

char strx [256];

For example:

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

int main()
{
    char c, strx[256] = "";
    int i = 0;
    c = getchar();
    while (c != '\n') {
        strx[i++] = c;
        c = getchar();
    }
    printf("%s\n", strx);
    return 0;
}
Zzz
  • 2,927
  • 5
  • 36
  • 58
1

You need to allocate space for strx (here on the stack, you can also you malloc)

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

int main()
{
    char c, strx[100] = "";
    int i = 0;
    c = getchar();
    while (c != '\n') {
        strx[i++] = c;
        if (i == 99)
            break;
        c = getchar();
    }
    strx[i] = '\0';
    printf("%s\n", strx);
    return 0;
}

strcat doesn't work because it expects a null char at the end but it will probably find garbage after the char.

More simply, you can use scanf:

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

int main()
{
    char strx[100] = "";
    scanf("%99s", strx);
    printf("%s\n", strx);
    return 0;
}
ctn
  • 2,887
  • 13
  • 23