Thread: strings of ASCII

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    strings of ASCII

    As a newbie I was trying to declare an ASCII string ending with a "0", but got a compiler error.

    Code:
    constant char STRING[7] = {"string",0};
    What is wrong?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Because when you initialize a string, you need to do it one character at a time.

    Like this:
    Code:
    const char STRING[] = {'s', 't', 'r', 'i', 'n', 'g', '0'};
    or you can do this...
    Code:
    //Also, include <string.h>  
      char STRING[10];
      strcpy(STRING,"string");
      strcat(STRING,"0");
    Last edited by SlyMaelstrom; 10-30-2005 at 02:09 AM.
    Sent from my iPadŽ

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SlyMaelstrom
    Because when you initialize a string, you need to do it one character at a time.
    Code:
    const char astringy[] = "You do?";

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ok you don't... but you could.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Andy_P
    As a newbie I was trying to declare an ASCII string ending with a "0", but got a compiler error.

    Code:
    constant char STRING[7] = {"string",0};
    What is wrong?
    you don't have to explicitly put the terminating 0 there, the compiler will do that for you. You also don't need the braces, nor specify the string size unless you want the character array to be larger than the initialization string.

    Code:
    constant char STRING[] = "string";

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Ancient Dragon
    Code:
    constant char STRING[] = "string";
    And it's const, not constant.

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. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  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