Thread: array subscript is not an integer

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

    array subscript is not an integer

    im getting the error when compiling the code...
    Code:
    float array[20];
    int last;
    array[last[array]];
    how to get red of this

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You have
    Code:
    array[last[array]];
    In general,if you are sure you want something like this,imagine it with numbers and arrays.It would be something like this
    Code:
    array[array[19]]
    if you want to access the index described by the last element of the array.
    So maybe you want to say
    Code:
    array[array[last]]

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    still the same error,i think may be its due to types of the integer variable and float arrays

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Have you initialized variable last? Can you please post the code you complied?

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    im just checking the syntax errors
    i have initialised int last = n-1;
    int *l ;
    l = &last;

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What is n?Where is the array you were talking about? :P
    Just post all the code you have (i consider it is small from what you say )
    Also it would be very kind of you if you could wrap your code in code tags.It is easy
    [key]/*Your code here*/[/key] Replace key with code in order this to work.

    EDIT : it is ok to say
    Code:
    int a=5;
    int* p;
    p=&a;
    Last edited by std10093; 11-06-2012 at 05:17 AM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by prathiksa View Post
    still the same error,i think may be its due to types of the integer variable and float arrays
    Yes it is.

    Arrays can only be indexed using values that have integral type (char, int, long, unsigned, etc). They cannot be indexed using non-integral values (float as in your case, struct types, etc).

    There is also an obscure corner of C that causes "array[index]" and "index[array]" to be equivalent (assuming array is (well....) an array or a pointer, and index has integral type). Because of that, your expression "array[last[array]]" is equivalent to "array[array[last]]" which attempts to use array[last] as an index. In your code, array[last] is of type float so is not a valid index. Hence the compiler error.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What i thought you wanted to do
    Code:
    #include <stdio.h>
    
    int main( int argc, char *argv[])
    {
      float array[5];
      int last = 4;
      array[0]=1.2;
      array[1]=1.3;
      array[2]=1.4;
      array[3]=1.5;
      array[4]=1.9;
    
      printf("%f\n",array[last]);
    
      return(0);
    }
    grumpy is more experienced,so he is sure to have it better in his mind

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You get rid of the error by fixing or removing the nonsense code.
    Explain what you really want to do and we'll show you how to write code for it that makes sense.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading subscript operator for multi-dimensional array.
    By minesweeper in forum C++ Programming
    Replies: 3
    Last Post: 05-12-2011, 10:57 PM
  2. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  3. Array subscript
    By poovizhipanpa in forum C Programming
    Replies: 6
    Last Post: 01-08-2009, 12:45 PM
  4. warning: array subscript has type `char'
    By pc_doctor in forum C Programming
    Replies: 5
    Last Post: 12-08-2008, 03:03 PM
  5. Replies: 4
    Last Post: 11-09-2002, 01:16 PM

Tags for this Thread