Thread: Array trouble

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    Array trouble

    Very simple code, but I have no idea why it won't compile. Compiler 'expected expression before '{' token'.

    Code:
    #include <stdio.h>
    
    int particles[3][6];
    void new_particle(int);
    
    int main(void)
    {
    	new_particle(2);
    	
    	return 0;
    }
    
    void new_particle(int i)
    {
    	particles[i] = {1,2,3,4,5,6};
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you cannot assign to array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by thealmightyone View Post
    Very simple code, but I have no idea why it won't compile. Compiler 'expected expression before '{' token'.

    Code:
    #include <stdio.h>
    
    int particles[3][6];
    void new_particle(int);
    
    int main(void)
    {
    	new_particle(2);
    	
    	return 0;
    }
    
    void new_particle(int i)
    {
    	particles[i] = {1,2,3,4,5,6};
    }
    You can only do that when you're initialising.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    Gotcha. Thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble moving lines up in 2D array
    By powell5487 in forum C Programming
    Replies: 5
    Last Post: 03-03-2009, 12:54 PM
  2. Array trouble
    By Cpro in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2006, 01:55 PM
  3. trouble creating an array
    By s_ny33 in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 11:18 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM