Thread: struct containing arrays.

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    struct containing arrays.

    Firstly sorry and apologize for this silly question

    lets say I have this example(it's spontaneously) :
    Code:
    #include<stdio.h>
    #include<string.h>
    struct student {
            char name[15];}jay;
    int main()
    {
          jay.name="David";
          return 0;
    }
    I know there're errors in this code, and that's why I came for
    Well, to put the value "david" into the struct's name , I must access it and I accessed the struct by using operator "."(see in line 5), and now why I must write
    Code:
    jay.name
    , and not a
    Code:
    jay.name[15]
    , I'm saying that because the variable here not name alone it's name[15], so I must define that while trying to accessing... , and that's all..thanks.
    Last edited by RyanC; 08-13-2015 at 08:31 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You cannot assign to a char array that way. You need to use strcpy().
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Elkvis View Post
    You cannot assign to a char array that way. You need to use strcpy().
    So you're trying to say if I could assign, then I could use the statement I've talked about?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by RyanC View Post
    So you're trying to say if I could assign, then I could use the statement I've talked about?
    Try modifying the program so the variable in the struct is an integer instead of a character array. Then assign a value to it, and print the result. I think that should answer your question.

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Matticus View Post
    Try modifying the program so the variable in the struct is an integer instead of a character array. Then assign a value to it, and print the result. I think that should answer your question.
    You didn't get me, I know I must write
    Code:
    strcpy(jay.name, "David")
    but why we cant write like jay.name[15]="David" ? isn't
    the variable in the struct called name[15]? or we just forgetting always from the object "[length]" and name itself is the variable!
    Last edited by RyanC; 08-13-2015 at 08:53 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,416
    Quote Originally Posted by RyanC
    but why we cant write like jay.name[15]="David" ? isn't
    the variable in the struct called name[15]?
    No, the member is named name, not name[15].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    There are 3 different thing.

    1. You've defined struct called student
    2. Inside the struct you have a "member variable" and your asking compiler to allocate 15 bytes.
    3. And to access that 15 bytes of memory you use a name and you named it as "name"

    When you want to copy something to that memory you need to specify where to copy rhs too. And the way you do that is specifying the name in this case. Does that make sense?
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by RyanC
    but why we cant write like jay.name[15]="David" ? isn't
    the variable in the struct called name[15]? or we just forgetting always from the object "[length]" and name itself is the variable!
    It wouldn't work the way you would want it to without a special case for strings written into the standard for the assignment operator (I think). The type on the right hand side of the assignment is a pointer to const char, which would need to be deep copied into the array on the left, or else it would just be undefined behavior. You can do a shallow copy of a string literal like this:

    Code:
    const char* a = "Hello";
    const char* b = a;
    Basically just make sure you use strcpy or similar function to make a copy of a string literal when assigning it to non-const memory and you should be fine.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,416
    Quote Originally Posted by Alpo
    It wouldn't work the way you would want it to without a special case for strings written into the standard for the assignment operator (I think). The type on the right hand side of the assignment is a pointer to const char, which would need to be deep copied into the array on the left, or else it would just be undefined behavior.
    The problem is that an array is not a modifiable lvalue, but the left hand operand of an assignment operator is required to be a modifiable lvalue.

    Quote Originally Posted by Alpo
    The type on the right hand side of the assignment is a pointer to const char
    Not quite: the type of the string literal is actually char[6] that is not an lvalue, so it is effectively equivalent to const char[6], and since arrays are converted to pointers to their first elements, it effectively amounts to const char*, yet strictly speaking it isn't.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a litle help with Struct arrays
    By Crisapx in forum C Programming
    Replies: 2
    Last Post: 05-02-2015, 02:07 PM
  2. Struct Arrays
    By Jdo300 in forum C Programming
    Replies: 17
    Last Post: 03-09-2012, 03:16 PM
  3. arrays and struct !!
    By abood1190 in forum C Programming
    Replies: 9
    Last Post: 10-19-2011, 10:26 AM
  4. struct arrays
    By silentkarma in forum C++ Programming
    Replies: 9
    Last Post: 07-23-2008, 03:24 AM
  5. Problem with struct arrays...
    By Bizmark in forum C Programming
    Replies: 5
    Last Post: 03-27-2008, 07:46 PM