Thread: assignment error in struct

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    assignment error in struct

    In the following why do the integer assignments work OK but the char assignment gives an error 'invalid array assignment'?

    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    struct Player
    {
            char surname[5];
            char initial[2];
            char firstname[5];
            char club[9];
            int normalgrade;
            int rapidgrade;
            char gradingnumber[9];
            int dmno;
      };
    
    int main ()
    {
    Player Reed;
    Reed.surname = "Reed";
    //Reed.initial = "F";
    //Reed.firstname = "Mike";
    //Reed.club = "Fakenham";
    Reed.normalgrade=200;
    Reed.rapidgrade=199;
    //Reed.gradingnumber="107456D";
    Reed.dmno=14171;
    
    cout<<Reed.surname<<endl;
    cout<<Reed.rapidgrade<<endl;
    cout<<Reed.dmno<<endl;
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You can not use the assignment operator= with C-strings, maybe you should consider using std::strings instead.

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    I tried changing the char [5] to string [5] but that gave another error of incompatible types.

  4. #4
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Either use char* instead of char[] so you can use operator= or, use std::strings like this:

    std::string Surname;
    Surname = "Reed";

    string[5] creates an array of 5 strings, not a string with a maximum length of 5 characters.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Why would you need an array of strings? You should just use
    Code:
    string surname;
    Jim

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    I see the light - I didn't realise you could use string in a struct declaration as all the samples I looked at used char x[] - one more thing to poke into the brain.

  7. #7
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    You can use anything in a string. It's just a container. (like a class, but less complicated (therefore less powerful) and for C)
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    What?? A string is a C++ class. If you meant a struct, then in C++ there is little difference between a struct and a class. The only difference is that a struct defaults to public access an a class defaults to private access. In C++ you can have both member functions and variables in either a class or a struct.

    Jim

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    the only time you might want to use char arrays in a struct is if you're writing it to a file, or sending it over a network connection (these are examples - there are other similar situations). there are better ways to do this than with a raw struct, but all of them require much more code.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    the only time you might want to use char arrays in a struct is if you're writing it to a file...
    I disagree. I'd serialize the string itself (the >> operator!).

    ...or sending it over a network connection (these are examples - there are other similar situations)...
    Then you are using the Win32 API or similar, which you need to move away from. Quickly.
    Boost offers an alternative, but damn, it's complicated (but powerful).
    This is clearly one area where Java has the advantage (I'm actually shamed to admit that).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you obviously didn't read the second half of my post, regarding better ways to write to a file than just a simple memory dump of a struct.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I did. I merely pointed out that I would not consider what you suggested as a "one time you might want to do it." Ergo, you never want to do that in C++. Strings can easily be serialized. Char arrays, not so easily.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    I did. I merely pointed out that I would not consider what you suggested as a "one time you might want to do it." Ergo, you never want to do that in C++. Strings can easily be serialized. Char arrays, not so easily.
    Well, if the record in a record based binary file is guaranteed to be a certain size, then you can do random file I/O instead of working sequentially. If that's your goal then there really isn't much difference this time, because you would have to store the string in a field of some certain size, anyway. And random file I/O is one very common reason to use binary. Your serialization strategy really depends on what the goals are, and for other parts of the code, a std::string is a lot better.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Good point there. I'll just leave it at that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compilation error on array assignment.
    By sanddune008 in forum C Programming
    Replies: 2
    Last Post: 07-26-2010, 02:50 AM
  2. Help with assignment error
    By waterborne in forum C Programming
    Replies: 5
    Last Post: 02-17-2010, 02:04 PM
  3. struct member assignment (c-only)
    By emorrp1 in forum C Programming
    Replies: 8
    Last Post: 06-25-2008, 05:30 AM
  4. CS2 Assignment: Error
    By RoD in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2003, 01:30 PM
  5. Replies: 2
    Last Post: 08-05-2002, 04:52 PM