Thread: C Strings in Structures.

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079

    C Strings in Structures.

    Hi, I'm sorry I don't have my actual code to show give an example, at this time, but I'll try to recreate my problem for you.

    Using Dev C++ as a compiler, I found that if I define a character array in a structure and initialized the array inside of main I would get an "Invalid conversion from 'const char*' to 'char'" error.

    Can anyone tell me why this is and how it could be fixed?

    Here is some example code:

    Code:
    #include <iostream.h>
    
    struct foo {
           char foofoo[10];   // I also tried " char foofoo[] "
           };
    
    int main() {
        foo footwo;
        
        footwo.foofoo[4] = "Foo";   // I also tried just " footwo.foofoo " without
                                      // the array.    
        cout << footwo.foofoo;
        
        cin.get();
        
        return 0;   
    }
    Code:
    10 |  C:\Documents and Settings\Owner\My Documents\Source Code\RPG.cpp  |  invalid conversion from `const char*' to `char'
    Sent from my iPadŽ

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    You can't copy C-strings like this:

    Code:
        footwo.foofoo[4] = "Foo";   // I also tried just " footwo.foofoo " without
                                      // the array.
    They do not work like std::string. Instead, try strcpy.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Also note that footwo.foofoo[4] is a single character, not a string. Here is an example of how to use strcpy():
    http://www.cplusplus.com/ref/cstring/strcpy.html

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #include <iostream.h>
    
    struct foo {
           char foofoo[10];
           };
    
    int main() {
        foo footwo;
        
        strcpy (footwo.foofoo, "Foo");
          
        cout << footwo.foofoo;
        
        cin.get();
        
        return 0;   
    }
    This worked perfectly, thanks guys.

    Also, I do understand that footwo.foofoo[4] is just the fourth value of the array. The logic just didn't hit me, at first. Thanks for pointing it out.
    Last edited by SlyMaelstrom; 10-12-2005 at 01:34 PM.
    Sent from my iPadŽ

  5. #5
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Also, I do understand that footwo.foofoo[4] is just the fourth value of the array. The logic just didn't hit me, at first. Thanks for pointing it out.
    It's the 5th.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No nitpicking, my brain isn't functioning correctly.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Sorry, more nitpicking. Has anybody told you yet that you are using an out of date and non-standard header that doesn't exist on some modern compilers? You should be using <iostream> instead of <iostream.h>.

    Also, why did you decide against the C++ string class? Your original code would have worked as expected.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Because I want it to output the text one letter at a time using a timer.

    for instance:

    Code:
    int x = 0;
    
    while ( footwo.foofoo[x] != '\0' ) {
        cout << footwo.foofoo[x];
        x = x + 1;
    
        timer();
    }
    ...and I know iostream.h is antiquated, but I'm not really posting this code for anyone to compile, and it compiles fine on mine, so I wasn't really worried.
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can do that with the string class as well:
    Code:
    int x = 0;
    
    while ( x < footwo.foofoo.length() ) {
        cout << footwo.foofoo[x];
        x = x + 1;
    
        timer();
    }

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Cool, then, it works good. Thank you.
    Last edited by SlyMaelstrom; 10-12-2005 at 03:47 PM.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You mean this line?
    Code:
    cout << footwo.foofoo[x];
    The [x] there is necessary to only write out a single letter at a time. It behaves the same way for the string class as it does for a character array- outputting the character at position x.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The string type allows square brackets:
    Code:
    string s = "hippo";
    
    cout << s[1];  // prints 'i'
    [edit] ugh, beaten again [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    *cough*
    Code:
    struct foo {
           char foofoo[10];   // I also tried " char foofoo[] "
           };
    
    int main() {
        foo footwo = { "Foo" };
        
        cout << footwo.foofoo;
        
        cin.get();
        
        return 0;   
    }

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    *cough*
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Meh I just copied an modified his code. Plus the standard clause is in effect: If one posts a code to illustrate a point but doesn't include headers or silly little using declarations, it is the reader's responsiblity to add them in.

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. Replies: 2
    Last Post: 04-13-2003, 08:40 PM
  3. Strings and Structures
    By Kinasz in forum C Programming
    Replies: 3
    Last Post: 02-23-2003, 03:46 AM
  4. Structures Arrays and Char Strings
    By Crocksmyworld in forum C Programming
    Replies: 5
    Last Post: 01-19-2002, 11:56 PM
  5. Comparing strings w/in structures
    By vehl in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2001, 10:59 AM