Thread: struct accessing member question

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    4

    struct accessing member question

    Hello.
    I have jumped into this C stuff and I'm trying to grasp
    an understanding of structures and pointers and all that fun.
    I'm on my second month of this stuff. I feel I'm moving very slow but
    I'm not giving up. At any rate hello and it's nice to meet everyone.
    I couldn't find help on the FAQ's and if it's there then I reckon it still didn't click.
    My question is:
    why doesn't this work: (should be enough code for the discussion here)

    struct woman {
    int Height;
    int tits;
    char hair[7];
    };

    struct woman wife = {
    1,
    8,
    "black"
    };

    wife.hair ="green";

    structs_learn.c:39: error: incompatible types in assignment

    I can access and change types int but not types of char.

    ty.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You cannot assign to arrays. If you want to copy strings, use strcpy.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    4
    That's right.
    For the record here's the fix

    strcpy(wife.hair, "green");

    cheers!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Alternatively, you can change hair to const char* and you'll be able to assign string literals. It has its disadvantages however, as you'll notice if you experiment.
    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. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. struct member parse error
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 05-08-2006, 08:50 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM