Thread: array declarartion in c++

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    33

    array declarartion in c++

    it showing error when i declare an static array
    Code:
    fun(int array1[],int array2[])
    {
    array1 [] = { 12,3,5,3,2,7,3,
                     7,6,4,3,2,1,1};
    
    array2 [] ={ 2,3,5,3,2,7,3,
                     9,6,4,3,2,1,1};
    it showing missing primary expression ] operator

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Your arrays are already declared so you can't use the initialization list { ... } to initialize these arrays.

    Jim

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    how to do initialisation then

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You will either need to set each individual element to it's proper value:
    Code:
    array[0] = 12;
    array[1] = 3;
    // etc.
    Do the initialization when you declare the arrays using the initialization list:
    Code:
    int array1 [] = { 12,3,5,3,2,7,3,
                     7,6,4,3,2,1,1};
    Or use another array:
    Code:
    void fun(int *array1)
    int array3 [] ={ 2,3,5,3,2,7,3,
                     9,6,4,3,2,1,1};
    
    *array1 = *array3;
    Jim

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by jimblumberg View Post
    Or use another array:
    Code:
    void fun(int *array1)
    int array3 [] ={ 2,3,5,3,2,7,3,
                     9,6,4,3,2,1,1};
    
    *array1 = *array3;
    Um, that wont work. It only copies one int.
    If you want to copy the array, use std::copy.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    wat about C,the same prodecure like c++ or we can initialise

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-09-2012, 02:29 PM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread