Thread: Not sure what I'm doing here....structures and chars a bad mix?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    30

    Not sure what I'm doing here....structures and chars a bad mix?

    Any ideas here? I've spent the past hour trying to fix this, but can't quite get it to work. (very new to C)

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "player.h"
    
    
    int board[3][3];
    int maxInput = 3; /* this represents the maximum int they can specify when choosing a position */
    
    
    struct players player[2];
    int isEven();
    
    
    int main()
    {
        player[0].name = "john";
        printf("%s", player[0].name); //hmmm
        getchar();
    }
    players.h
    Code:
    #ifndef PLAYER_H_INCLUDED
    #define PLAYER_H_INCLUDED
    
    
    struct players {
        char name[256];
        char type;
    };
    
    
    #endif // PLAYER_H_INCLUDED
    Returns the error:

    Code:
    C:\Users\Justin\Desktop\Project\Tutorials\main.c|13|error: incompatible types when assigning to type 'char[256]' from type 'char *'|

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You've just discovered why the string functions exist.
    C string handling - Wikipedia, the free encyclopedia

    I would use something like
    Code:
    player[0].name[0] = '\0';
    strncat(player[0].name, "john", 254);
    but it's up to you.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If i were you i would have as member of the struct char *name;
    Then when i do this the pointer name would be set to the string "john"(without the double quotes).
    player[0].name = "john";

    Notice also the following example

    Code:
    char player1[] = "Kahn";
    char *player2 = "Samaras";
    
    player2 = "Papadopoulos"; /* ok */
    player1 = "Karagounis"; /*error!*/
    what happens here is that the char pointer can be handled as a pointer(of course :P ) ,which means that we can set it to another string.
    You can not say to the array to go point in another string that Kahn.
    On the other hand,
    with the array you can modify the contents of the strings ,where with the char pointer you can not modify the contents of the string hope this helps

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by whiteflags View Post
    You've just discovered why the string functions exist.
    C string handling - Wikipedia, the free encyclopedia

    I would use something like
    Code:
    player[0].name[0] = '\0';
    strncat(player[0].name, "john", 254);
    but it's up to you.
    That's even greater!This library has some powerful functions like strcmp,strcpy,strlen etc...

    If you use this you could do (with member of struct the char name[256])
    Code:
    strcpy(player[0].name , "john");

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    30
    Thanks for the quick & helpful response! I tested the code and it worked perfectly.

    Just to make sure I understand what you did there, you:

    Set name to an empty string.
    You then concatenated john and \0, forming: john\0 - which just outputs john

    I just want to make sure I know what's going on instead of just agreeing and moving on. ^.^

    Quote Originally Posted by std10093 View Post
    If i were you i would have as member of the struct char *name;
    Then when i do this the pointer name would be set to the string "john"(without the double quotes).
    player[0].name = "john";

    Notice also the following example

    Code:
    char player1[] = "Kahn";
    char *player2 = "Samaras";
    
    player2 = "Papadopoulos"; /* ok */
    player1 = "Karagounis"; /*error!*/
    what happens here is that the char pointer can be handled as a pointer(of course :P ) ,which means that we can set it to another string.
    You can not say to the array to go point in another string that Kahn.
    On the other hand,
    with the array you can modify the contents of the strings ,where with the char pointer you can not modify the contents of the string hope this helps
    So you recommend

    Code:
    char *string;
    vs

    Code:
    char mystring[length];
    Correct?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Justin H View Post
    Thanks for the quick & helpful response! I tested the code and it worked perfectly.

    Just to make sure I understand what you did there, you:

    Set name to an empty string.
    You then concatenated john and \0, forming: john\0 - which just outputs john

    I just want to make sure I know what's going on instead of just agreeing and moving on. ^.^
    Correct.But i would also suggest you to take a loop at strcpy function as well

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    30
    Quote Originally Posted by std10093 View Post
    Correct.But i would also suggest you to take a loop at strcpy function as well
    Yeah, I wasn't aware of all the string functions C had available. I'll definitely be looking into them.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Justin H View Post
    Yeah, I wasn't aware of all the string functions C had available. I'll definitely be looking into them.
    A general tip.Don't edit the posts without letting the others know especially when posts are created one after other

    Because you are new i suggest you stick with the your approach(static array) . When you feel ok with them then go for the other

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Justin H View Post
    Thanks for the quick & helpful response! I tested the code and it worked perfectly.

    Just to make sure I understand what you did there, you:

    Set name to an empty string.
    You then concatenated john and \0, forming: john\0 - which just outputs john

    I just want to make sure I know what's going on instead of just agreeing and moving on. ^.^



    So you recommend

    Code:
    char *string;
    vs

    Code:
    char mystring[length];
    Correct?
    Yes you understand me correctly. There are two reasons why I like the code I gave you. strncat always terminates the destination buffer with zero, making a string. And also because strncat, when called correctly, will not access a buffer out of bounds. Some functions, like strcpy and strcat will happily process until they encounter a zero in the source string, which may cause out of bounds access when you attempt to store too much data in your array, and likely segmentation faults. Segmentation faults cause an abort, so you want to avoid the possibility. You will have to deal with the possibility of truncated data, but it's better than some of the alternatives.

    Also be careful when people suggest replacing arrays with pointers. A string like this one
    Code:
    char *p = "john";
    it can be read but not modified. I consider such recommendations irresponsible unless the person is willing to explain all the necessary code to let you use these kinds of variables responsibly.
    Last edited by whiteflags; 10-18-2012 at 12:30 PM.

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    A way of making sure that you don't modify sting literals is to declare them "const" - That way your compiler can see if you are trying to modify them at any point and declare it as an error


    Code:
    const char *p = "John";
    ...
    *p = 'j'; // This will be an error when compiling using the "const" identifier, rather than a seg fault when running and not using the "const" identifier.
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    And I was thinking more along the lines of when you do want to be able to use the pointer to modify the string, in which case, a pointer to a string literal can't do what you want at all. You have to get dynamic memory, and properly copy it, and manage the variable's lifetime. (Know when to and be able to call free()) It gets even more complicated if the string's memory block needs to be resized. So you really can be careless with some C tips.

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Allocating a static array (like you have done) then using string functions to manipulate it is a sensible route. I'd guess that it'd cause you the least amount of problems!

    You can also do:
    Code:
    char string[] = "hello there";
    strcpy(string, "allo");  //ok
    
    
    char another[];
    another[] = "lalal"; // NOT allowed, have to initialise with a value
    Which gives you a writable array of the size of whatever string you initialise it with. I hardly ever use this syntax -- I find it quite rare that I want to initialise and later write to a string.
    For any string that you want to write to -- e.g. accept input from the user and save it there, you should allocate memory (somehow).
    For strings that don't change at run time (e.g. messages for the user) you can use the char* string literal way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures as members of structures & offset
    By trish in forum C Programming
    Replies: 24
    Last Post: 07-16-2011, 05:46 PM
  2. pointers of chars and arrays of chars Question
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2010, 10:42 AM
  3. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  4. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM