Thread: array question

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    7

    array question

    I am trying to figure out why this array assignment is invalid. Can someone please explain it to me?


    Code:
    #include <stdio.h>
    #define SIZE 5
    int main(void)
    {
         int win[SIZE] = {5, 3, 2, 1}
         int loss[SIZE]
    
         win[SIZE] = loss[SIZE] // invalid ?

    Is the assignment invalid because I am assigning one array to another as one whole unit?

    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You cannot assign arrays that way. You have to loop through the entire array, assigning one element at a time. Even if you use something like memcpy, you have to loop (because that's what it does, it just wraps it in a function for you).

    Furthermore, your use of win[SIZE] is invalid, because SIZE would actually point past the valid useable elements of the array itself. Remember, array indexes go from win[0] through win[SIZE - 1].

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    aside from the typographical errors i'm ignoring, and the puzzling intiialization in your first array - the short answer is yes. you can't assign an array to another array in C.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    both arrays are of size 5, which means you have elements from 0 to 4. element 5 is out of range, and your assignment tries to assign the value of loss[5] (out of range) to win[5] (also out of range)

    recall from ansi, in array of size n, a pointer may point to array[n], but may not dereference it.
    hello, internet!

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If this is the cause of your confusion with SIZE, remember that only arrays of type char will have the null character at the end taking up one element.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sean_mackrory
    If this is the cause of your confusion with SIZE, remember that only arrays of type char will have the null character at the end taking up one element.
    This is wrong. Character arrays are not required to have a null character at the end. C strings require a null character, not character arrays. There is a difference.

    Character arrays are not required to be C strings.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by caroundw5h
    aside from the typographical errors i'm ignoring, and the puzzling intiialization in your first array - the short answer is yes. you can't assign an array to another array in C.
    Assuming the origional poster fixes their semi-colon key, which seems to be not working, the array initialization is perfectly legal. It assigns the first four values, as listed, and by default assigns zero to the last uninitialized element.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by quzah
    Assuming the origional poster fixes their semi-colon key, which seems to be not working, the array initialization is perfectly legal. It assigns the first four values, as listed, and by default assigns zero to the last uninitialized element.

    Quzah.
    knew that. just wondering why he did it.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM