Thread: strings

  1. #1
    Registered User
    Join Date
    Apr 2008



    So i am trying to improve programs i write. My programs are usually for calculations due to my uni course.

    At the moment my programs take in integers using scanf, but if someone puts a letter in by mistake, they crash so i want to use strings.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
        char *a, *b, *c;
        printf("Please input a, b and c in the form of a b c\n");
        scanf("%s", a);
        scanf("%s", b);
        scanf("%s", c);
        printf ("%s %s %s", a, b, c);
        return 0;
    If i completely remove anything to do with b and c, the program can print a, but once b and c come along, it crashes. What am i doing wrong? I have no idea of the length these variables could be so i cant use arrays i dont think.

    is there a neater way of doing this?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    your a,b,c are uninitialized pointers - you do not know where they point - so you are trying to write to the random memory location.

    You need arrays - to own the memory you'll use

    char buf[100];
    scanf("%99s", buf);
    printf("%s", buf);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2008
    thank you. what does the 99 do?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Quote Originally Posted by a.mlw.walker View Post
    thank you. what does the 99 do?
    Make sure that you don't read in more data than there is space for.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM