array question

This is a discussion on array question within the C Programming forums, part of the General Programming Boards category; I am trying to figure out why this array assignment is invalid. Can someone please explain it to me? Code: ...

  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.

  4. #4
    moi
    moi is offline
    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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

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, 06: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, 05:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21