CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2005
    Posts
    22

    array declaration question

    my.cpp file:

    Code:
    #define size 55
    
    
    int array1[size];
    int *array2[size];
    size may vary.

    if i add these to my.h file as:

    Code:
    int array1[];
    int *array2[];
    
    i get error: an illegal zero-sized array.
    how do i handle this?.

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: array declaration question

    Quote Originally Posted by %chess%
    how do i handle this?.
    Like this, for example:

    Code:
    int*  array1;
    int** array2;
    But since you are using C++: Why bother about C-style arrays at all, and not std::vector instead?

    Besides that: What's your intention behind declaring a variable in a header file? That's usually not a good idea.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: array declaration question

    If your goal is to have a resizable array, use the std::vector class that has been discussed here many times.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Mar 2005
    Posts
    22

    Re: array declaration question

    int *databuf[arraysize];

    for (int i = 0; i < 10; i++)
    databuf[i] = new int[sizeofdata];

    do i do the above with vectors?.

  5. #5
    Join Date
    Feb 2003
    Posts
    377

    Re: array declaration question

    You can do the equivalent by constructing a vector of vectors of ints using the constructors that take the initial size of the vector:
    Code:
    std::vector<std::vector<int> > databuf(arraysize, std::vector<int>(sizeofdata));
    You can also use typedefs to make the code smaller if the look of it bothers you.

  6. #6
    Join Date
    Mar 2005
    Posts
    22

    Re: array declaration question

    std::vector<std::vector<int> > databuf(m_arraysize, std::vector<int>(m_sizeofdata));

    How do i access this globally within an object from different functions?
    i was getting an error when added it to public member of
    CMy class. need to push to its stack area from one function and
    pop from its stack area from another function.

    unknown m_arraysize.it needs to know m_arraysize?. maybe i cam confused.
    could you illustrate more?.

  7. #7
    Join Date
    Feb 2003
    Posts
    377

    Re: array declaration question

    As part of a class, you declare it in the class and initialize it in the constructor(s)' initializer list(s):
    Code:
    class Example
    {
    public:
      Example(int arraysize, int sizeofdata) : databuf(arraysize, std::vector<int>(sizeofdata))
      { }
    
      int getArraySize() const { return databuf.size(); }
      int getSizeOfData(int index = 0) const { return databuf.at(index).size(); }
    
    private:
      std::vector<std::vector<int> > databuf;
    }
    If you are still unsure, you might want to post more of your code so we know what exactly you are having trouble with. There are more ways to initialize your vectors than the way I used above, but hopefully you will be able to figure out how to get the sizes to the constructor on your own.

  8. #8
    Join Date
    Mar 2005
    Posts
    22

    Re: array declaration question

    sorry, One last thing since i am starting to look at
    vectors more seriously than just a c++ extra thing.

    How do i pass each databuf buffer to PutDataInBuffer function
    to put data into it?. that was my intention. to allocate space
    and pass it on.

    Code:
    for (int i = 0; i < size; i++)
    {
    	BOOL (PutDataInBuffer(dataBuffers[i], Status))
    	{
    		//
    	}
    }

  9. #9
    Join Date
    Mar 2005
    Posts
    22

    Re: array declaration question

    I guess this what should pass databuf[idx].at(idx)....???
    should work...

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: array declaration question

    Quote Originally Posted by %chess%
    int *databuf[arraysize];

    for (int i = 0; i < 10; i++)
    databuf[i] = new int[sizeofdata];

    do i do the above with vectors?.
    Take a look at the following introduction to the 'vector' class...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured