strings question

This is a discussion on strings question within the C Programming forums, part of the General Programming Boards category; hi, i have the following code... im just wondering if what i did was a good way to get the ...

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

    strings question

    hi, i have the following code... im just wondering if what i did was a good way to get the outcome.

    Code:
    /* lets assume userinput = "hello" */
    char address[1000] = {0, '/'};
    strcat(address, userinput); /* address is now /hello */

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    address will be "Hello", since the '/' is after a 0, so the address variable is essentialy the same as "" (empty string).

    --
    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.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    icic so when it detects the 0, it interprets it as end of string.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Correct. To initialize the address array, you can code
    Code:
    char address[1000] = {'\0'};
    or
    char address[1000] = '\0' ; 
    or
    char address[1000] = 0 ;
    They are all the same. The backslash is called an escape character.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    61
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about strings in c
    By gp364481 in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 05:32 PM
  2. Question About Strings
    By spanker in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2008, 05:09 AM
  3. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  4. Strings question
    By kimimaro in forum C Programming
    Replies: 10
    Last Post: 03-14-2005, 11:14 PM
  5. question about arrays of strings
    By lakai02 in forum C Programming
    Replies: 14
    Last Post: 12-25-2002, 08:11 PM

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