locked
How can I define an array at the beginning? RRS feed

  • Question

  • Like define an N-dim array, but it seems VC++ does not allow me to do so.
    Tuesday, July 3, 2012 2:39 PM

Answers

  • just create a global array depends on your usage.

    int array[3] = { 1,2,3};


    Thanks, Renjith V R


    Tuesday, July 3, 2012 6:29 PM
  • If you define the global variable which the dependency 3rd party also define with the same name in global scope, you will get name mangling error. you can use namespace for avoid name mangling.

    // main1.cpp
    // global scope
    int i= 10;
    
    
    //main2.cpp
    //global scope
    int i=20;
    you will get an error. you can use name space for aviod this error



    // main1.cpp
    namespace one {
    // global scope
    int i= 10;
    }
    
    //main2.cpp
    namespace two{
    
    //global scope
    int i=20;
    }
    


    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/

    • Marked as answer by C-Z Friday, July 20, 2012 7:49 PM
    Wednesday, July 4, 2012 4:29 PM

All replies

  • What are you doing? This should do the trick:

    int someArray[5][5];

    Tuesday, July 3, 2012 2:55 PM
  • I want to claim like #define ..., such that it will be used in any function.
    Tuesday, July 3, 2012 2:58 PM
  • I want to claim like #define ..., such that it will be used in any function.

    It's not clear what you mean by this.

    If you want an array of arbitrary dimensions, you should be using one of the STL containers. For example, for a two-dimensional array, one might choose a vector, where each element is yet another vector to the elements of interest. Are you familiar with STL?

    Tuesday, July 3, 2012 3:09 PM
  • I want to claim like #define ..., such that it will be used in any function.

    It's not clear what you mean by this.

    If you want an array of arbitrary dimensions, you should be using one of the STL containers. For example, for a two-dimensional array, one might choose a vector, where each element is yet another vector to the elements of interest. Are you familiar with STL?

    Oh, sorry let me be more clear:

    I want to define some fixed numbers and fixed arrays( with known dimension), and I will give them specific values. No arbitrary problem. Could you show some sample code? Sorry I am not familiar with STL.

    Tuesday, July 3, 2012 3:21 PM
  • Are you looking something like this?

    #define ARRAY_SIZE_ROW 5 #define ARRAY_SIZE_COL 5 int _tmain(int argc, _TCHAR* argv[]) { int a[ARRAY_SIZE_ROW][ARRAY_SIZE_COL];

    // code

    return 0; }



    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/

    Tuesday, July 3, 2012 4:12 PM
  • You can also use const.

    const int ARRAY_SIZE = 5; int _tmain(int argc, _TCHAR* argv[]) { int a[ARRAY_SIZE]; return 0; }

    You cannot use a variable as fixed size array. For using variable you need dynamically allocated array.

    const int ARRAY_SIZE = 5; int _tmain(int argc, _TCHAR* argv[]) { int *a = new int[ARRAY_SIZE];

    // code

    // Release memory after use.

    delete[] a; return 0; }


    Thanks, Renjith V R

    Tuesday, July 3, 2012 4:55 PM
  • Try this as well:

    typedef int MyArray[10][20];

    MyArray arr1;
    MyArray arr2;

    Tuesday, July 3, 2012 5:51 PM
  • Are you looking something like this?

    #define ARRAY_SIZE_ROW 5 #define ARRAY_SIZE_COL 5 int _tmain(int argc, _TCHAR* argv[]) { int a[ARRAY_SIZE_ROW][ARRAY_SIZE_COL];

    // code

    return 0; }



    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/


    Sorry I am going to define an array V={1,1,2,3,4} for example. I do not want to define each entry one by one. Is there a good way to pre-define it before main function?
    Tuesday, July 3, 2012 6:14 PM
  • You can also use const.

    const int ARRAY_SIZE = 5; int _tmain(int argc, _TCHAR* argv[]) { int a[ARRAY_SIZE]; return 0; }

    You cannot use a variable as fixed size array. For using variable you need dynamically allocated array.

    const int ARRAY_SIZE = 5; int _tmain(int argc, _TCHAR* argv[]) { int *a = new int[ARRAY_SIZE];

    // code

    // Release memory after use.

    delete[] a; return 0; }


    Thanks, Renjith V R


    Sorry for confusion, I tried to pre-define an array and gives values. I will use it frequently in main and other functions.
    Tuesday, July 3, 2012 6:15 PM
  • just create a global array depends on your usage.

    int array[3] = { 1,2,3};


    Thanks, Renjith V R


    Tuesday, July 3, 2012 6:29 PM
  • just create a global array depends on your usage.

    int array[3] = { 1,2,3};


    Thanks, Renjith V R



    Oh, before all functions, right? Just as #define command? Thanks.
    Tuesday, July 3, 2012 6:47 PM
  • You define the array before all functions (offically called at file scope but often referred to as a global variable) if you need the array to be available to several different functions.  If you only need the array in one function, then use the exact same syntax but place it inside that function.
    Tuesday, July 3, 2012 11:06 PM
  • I personally think that globals are a very bad practise. Best encapsulate in a static class for example, or at least wrap it in a namespace. The main problem with globals is that it's very hard to troubleshoot changes to the array, as you can do that from everywhere!
    Wednesday, July 4, 2012 6:53 AM
  • I personally think that globals are a very bad practise. Best encapsulate in a static class for example, or at least wrap it in a namespace. The main problem with globals is that it's very hard to troubleshoot changes to the array, as you can do that from everywhere!
    Oh, can you explain why, and could you provide some code?
    Wednesday, July 4, 2012 3:59 PM
  • If you define the global variable which the dependency 3rd party also define with the same name in global scope, you will get name mangling error. you can use namespace for avoid name mangling.

    // main1.cpp
    // global scope
    int i= 10;
    
    
    //main2.cpp
    //global scope
    int i=20;
    you will get an error. you can use name space for aviod this error



    // main1.cpp
    namespace one {
    // global scope
    int i= 10;
    }
    
    //main2.cpp
    namespace two{
    
    //global scope
    int i=20;
    }
    


    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/

    • Marked as answer by C-Z Friday, July 20, 2012 7:49 PM
    Wednesday, July 4, 2012 4:29 PM
  • Thanks, that is clear to me now.
    Friday, July 20, 2012 7:50 PM