Thread: Structure with string

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    3

    Structure with string

    I am trying to create a constant structure containing string, which does not seems possible.
    Hopefully someone can help me out by giving me some pointer.

    Here is my code
    Code:
    Constant struct CData{
    	char D1[] = "Data1";
    	char D2[] = "Data2";
    	char D3[] = "Data3";
    };
    Many Thanks!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    A structure is a blue print, think of it as a 'type', types can't hold data (ie, int != some random number, it's a type).
    Code:
    struct CData
    {
        char d1[16], d2[16], d3[16];
    };
    
    struct CData thingy = {"Data1", "Data2", "Data3"};
    Would work.
    Last edited by zacs7; 06-11-2008 at 12:05 AM.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    3
    In this case there will never be a constant structure?

    as in we call only do it as

    const struct Data Somedata;

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    3
    Thanks!

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes you can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. Replies: 12
    Last Post: 10-14-2003, 10:17 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM