invalid subscript error

This is a discussion on invalid subscript error within the C++ Programming forums, part of the General Programming Boards category; Iam getting error while compiling the following code.... I know the mistake i have done is a silly one, but ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    5

    invalid subscript error

    Iam getting error while compiling the following code.... I know the mistake i have done is a silly one, but could not find it after repeated parsing.. Please help;

    Code:
    short CAppWnd::ProcessData(short *pbuff, unsigned int nParseCount)
    {
      unsigned short naaLeadBuffer[12][nParseCount];
      unsigned int nBuffCount = nParseCount / 500;
      unsigned int nPos = 0;
    
      for(unsigned int nLeadCount = 0; nLeadCount < 12; nLeadCount++)
      {
        for(unsigned int nCount = 0; nCount <= nBuffCount; nCount
          += 500)
        {
          for(unsigned int nSampleCount = 0; nSampleCount < 500; nSampleCount
            += 10)
          {
            naaLeadBuffer[nLeadCount][nPos++] = (*pbuff[nLeadCount][nSampleCount
              + 2] + *pbuff[nLeadCount][nSampleCount + 3]) / 2;
            naaLeadBuffer[nLeadCount][nPos++]
              = *pbuff[nLeadCount][nSampleCount + 5];
            naaLeadBuffer[nLeadCount][nPos++] = (*pbuff[nLeadCount][nSampleCount
              + 7] + *pbuff[nLeadCount][nSampleCount + 8]) / 2;
            naaLeadBuffer[nLeadCount][nPos++] = *pbuff[nLeadCount][nSampleCount
              + 10];
          }
        }
      }
    return nParsecount;
    }

    error log ==>
    ./AppWnd.cpp: In member function 'short int CAppWnd::ProcessData(short int*, unsigned int)':
    ../AppWnd.cpp:491:14: error: invalid types 'short int[unsigned int]' for array subscript
    ../AppWnd.cpp:491:53: error: invalid types 'short int[unsigned int]' for array subscript
    ../AppWnd.cpp:493:48: error: invalid types 'short int[unsigned int]' for array subscript
    ../AppWnd.cpp:495:14: error: invalid types 'short int[unsigned int]' for array subscript
    ../AppWnd.cpp:495:53: error: invalid types 'short int[unsigned int]' for array subscript
    ../AppWnd.cpp:497:15: error: invalid types 'short int[unsigned int]' for array subscript
    mingw32-make: *** [AppWnd.o] Error 1

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    You cannot create the array naaLeadBuffer with the size nParseCount. The size in the [] brackets has to be known at compile time.
    Try using the new operator instead and allocating the memory dynamically.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by threahdead View Post
    You cannot create the array naaLeadBuffer with the size nParseCount. The size in the [] brackets has to be known at compile time.
    Try using the new operator instead and allocating the memory dynamically.

    Thanks for the reply....
    Request you to give a small code snippet !!! Dynamic allocation has always been a problem area for me...
    Last edited by MrDanny; 05-02-2011 at 03:56 AM.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by MrDanny View Post
    Thanks for the reply....
    Request you to give a small code snippet !!! Dynamic allocation has always been a problem area for me...
    @threahdead can you please help me....

  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    You will need an array of 12 x nParseCount, which basically is a

    Code:
    const int bufferSize = 12;
    unsigned short **naaLeadBuffer;
    naaLeadBuffer = new unsigned short *[bufferSize];
    for(int i = 0; i < bufferSize; i++){
        naaLeadBuffer[i] = new unsigned short[nParseCount];
    }
    naaLeadBuffer will have space for 12 pointers to an unsigned short. Every pointer must point to valid data, which is ensured in the for loop. The data it points to is nParseCount shorts long.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by threahdead View Post
    You will need an array of 12 x nParseCount, which basically is a

    Code:
    const int bufferSize = 12;
    unsigned short **naaLeadBuffer;
    naaLeadBuffer = new unsigned short *[bufferSize];
    for(int i = 0; i < bufferSize; i++){
        naaLeadBuffer[i] = new unsigned short[nParseCount];
    }
    naaLeadBuffer will have space for 12 pointers to an unsigned short. Every pointer must point to valid data, which is ensured in the for loop. The data it points to is nParseCount shorts long.
    Am getting the same error !!!

    ../AppWnd.cpp:497:14: error: invalid types 'short int[unsigned int]' for array subscript
    ../AppWnd.cpp:497:53: error: invalid types 'short int[unsigned int]' for array subscript
    ../AppWnd.cpp:499:48: error: invalid types 'short int[unsigned int]' for array subscript
    ../AppWnd.cpp:501:14: error: invalid types 'short int[unsigned int]' for array subscript
    ../AppWnd.cpp:501:53: error: invalid types 'short int[unsigned int]' for array subscript
    ../AppWnd.cpp:503:15: error: invalid types 'short int[unsigned int]' for array subscript
    mingw32-make: *** [AppWnd.o] Error 1

    the 499:48 points to
    naaLeadBuffer[nLeadCount][nPos++] = (*pbuff[nLeadCount][nSampleCount
    + 2] + *pbuff[nLeadCount][nSampleCount + 3]) / 2;

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    The problems aren't actually to do with memory allocation in this case. Look at the lines like this in the loop body
    Code:
           naaLeadBuffer[nLeadCount][nPos++] = (*pbuff[nLeadCount][nSampleCount
              + 2] + *pbuff[nLeadCount][nSampleCount + 3]) / 2;
    Note the two bits I've highlighted in red and orange. Each of those is triple-dereferencing pbuff (for example, acting as if it is a two-dimensional array of pointers). However, pbuff is just a humble pointer to short, and cannot be triple-dereferenced.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by grumpy View Post
    The problems aren't actually to do with memory allocation in this case. Look at the lines like this in the loop body
    Code:
           naaLeadBuffer[nLeadCount][nPos++] = (*pbuff[nLeadCount][nSampleCount
              + 2] + *pbuff[nLeadCount][nSampleCount + 3]) / 2;
    Note the two bits I've highlighted in red and orange. Each of those is triple-dereferencing pbuff (for example, acting as if it is a two-dimensional array of pointers). However, pbuff is just a humble pointer to short, and cannot be triple-dereferenced.
    pbuff actually is a pointer to a 2d short buffer...

    Code:
    short naaParseBuffer[12][buffsize];
    {
    ----
    -------
    ------
    
    naaParseBuffer[10][nParseCount] = naFiltered[6]; naaParseBuffer[11][nParseCount] = naFiltered[7];
    -------- ----- } ProcessData(&naaParseBuffer[0][0], nParseCount);
    Code:
    short CAppWnd::ProcessData(short *pbuff, unsigned int nParseCount)
    {
      unsigned short naaLeadBuffer[12][nParseCount];
      unsigned int nBuffCount = nParseCount / 500;
      unsigned int nPos = 0;
    
      for(unsigned int nLeadCount = 0; nLeadCount < 12; nLeadCount++)
      {
        for(unsigned int nCount = 0; nCount <= nBuffCount; nCount
          += 500)
        {
          for(unsigned int nSampleCount = 0; nSampleCount < 500; nSampleCount
            += 10)
          {
            naaLeadBuffer[nLeadCount][nPos++] = (*pbuff[nLeadCount][nSampleCount
              + 2] + *pbuff[nLeadCount][nSampleCount + 3]) / 2;
            naaLeadBuffer[nLeadCount][nPos++]
              = *pbuff[nLeadCount][nSampleCount + 5];
            naaLeadBuffer[nLeadCount][nPos++] = (*pbuff[nLeadCount][nSampleCount
              + 7] + *pbuff[nLeadCount][nSampleCount + 8]) / 2;
            naaLeadBuffer[nLeadCount][nPos++] = *pbuff[nLeadCount][nSampleCount
              + 10];
          }
        }
      }
    return nParsecount;
    }

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Quote Originally Posted by MrDanny View Post
    pbuff actually is a pointer to a 2d short buffer...
    That is consistent with my description of pbuff as a pointer to short. However, the way you are using pbuff in the loop is inconsistent with that, hence the compiler diagnostics.
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Quote Originally Posted by threahdead View Post
    You will need an array of 12 x nParseCount, which basically is a

    Code:
    const int bufferSize = 12;
    unsigned short **naaLeadBuffer;
    naaLeadBuffer = new unsigned short *[bufferSize];
    for(int i = 0; i < bufferSize; i++){
        naaLeadBuffer[i] = new unsigned short[nParseCount];
    }
    naaLeadBuffer will have space for 12 pointers to an unsigned short. Every pointer must point to valid data, which is ensured in the for loop. The data it points to is nParseCount shorts long.
    Suggest you use std::vector instead of error-prone manual memory allocation which you have to free later.
    std::vector<std::vector<T>> vec(inner_dim, std::vector(outer_dim));
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 11:57 AM
  2. Vector subscript out of error. Please help!
    By pantera in forum C++ Programming
    Replies: 9
    Last Post: 04-29-2010, 09:01 AM
  3. error: invalid initializer
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 09-18-2009, 08:28 AM
  4. error C2087: '<Unknown>' : missing subscript
    By shardin in forum C Programming
    Replies: 8
    Last Post: 09-12-2007, 08:56 AM
  5. Replies: 4
    Last Post: 11-09-2002, 12:16 PM

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