strings

This is a discussion on strings within the C Programming forums, part of the General Programming Boards category; Hi, So i am trying to improve programs i write. My programs are usually for calculations due to my uni ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    192

    strings

    Hi,

    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.

    Code:
    #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?
    thanks

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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

    Code:
    char buf[100];
    scanf("%99s", buf);
    printf("%s", buf);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    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, 07:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21