Thread: strings question

  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
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  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, 06: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-15-2005, 12:14 AM
  5. question about arrays of strings
    By lakai02 in forum C Programming
    Replies: 14
    Last Post: 12-25-2002, 09:11 PM