Initializing a C String

This is a discussion on Initializing a C String within the C++ Programming forums, part of the General Programming Boards category; As much as I've read on this one issue, it still confuses the heck out of me. If I've got ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    9

    Initializing a C String

    As much as I've read on this one issue, it still confuses the heck out of me.

    If I've got a C String as follows:

    char testString[6];

    Can this be initialized as follows:

    testString = "testin";

    ???

    Can pointers also be used to initialize C Strings?

    Thanks!!

  2. #2
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    You can do that sure just go:
    Code:
    char C_String[15] = "Look at me!"
    Hint-Use code tages


    You can have a pointer to a string, that is how they are passed to functions.

    You could dynamically allocate memory for a string:
    Code:
    char point*
    point = new char[12];
    Hope that helps some.
    Last edited by CheesyMoo; 04-09-2003 at 07:02 PM.
    If you ever need a hug, just ask.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

    Re: Initializing a C String

    Originally posted by cpluspluser
    As much as I've read on this one issue, it still confuses the heck out of me.

    If I've got a C String as follows:

    char testString[6];

    Can this be initialized as follows:

    testString = "testin";

    ???

    Can pointers also be used to initialize C Strings?

    Thanks!!
    actually, if you wanted to do that, you should declare it as
    Code:
    char teststring[7];
    because the c-string should really be null terminated.

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    another way to declare/initialize a c-string is without including the size when you define it right away:
    Code:
    char myString[] = "hello world!";
    this will make myString 13 elements long with myString[12] being NULL, which is automatically put in the c-string.

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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