469,306 Members | 1,977 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

error: subscripted value is neither array nor pointer

Hello all,

I'am stuck on the following error:
test.c:9: error: subscripted value is neither array nor pointer

I think I am doing something wrong with the variable rot[0][0], but I can't quite figure out what or why. I reduced my code to the code below. I am using "gcc test.c -o test" to compile the code. Any help would be appriciated.

Regards,
Harry


Expand|Select|Wrap|Line Numbers
  1. typedef float Rot_matrix[3][3];
  2. typedef float Vector[3];
  3.  
  4. int main(void){
  5.   Rot_matrix Rot[9];
  6.   float *rot = (float *)Rot[4];
  7.   Vector j = {1, 2, 3};
  8.   Vector *i = (Vector *) j;
  9.   (*i)[0] = rot[0][0];
  10.   return 0;
  11. }
  12.  
Oct 21 '08 #1
8 25083
Banfa
9,064 Expert Mod 8TB
What is the type of rot[0]?
Oct 21 '08 #2
What is the type of rot[0]?
rot points to the fourth Rot_matrix in the array Rot, therefore rot[0][0] is a float, and rot[0] should be an array of 3 floats. At least, that is what I am trying to do, but for some reason rot[0][0] is not accepted by the compiler. I guess I am missing something here.

Thanks for responding.
Harry
Oct 21 '08 #3
donbock
2,422 Expert 2GB
rot points to the fourth Rot_matrix in the array Rot, therefore rot[0][0] is a float, and rot[0] should be an array of 3 floats. At least, that is what I am trying to do, but for some reason rot[0][0] is not accepted by the compiler.
But rot is declared as ...
... float *rot = (float *)Rot[4];
that is, it is declared as a pointer to one or more floats. That's entirely different from "an array of 3 floats" as you wanted it to be.

Take a look at Dynamically Allocating Multidimensional Arrays
Oct 21 '08 #4
Banfa
9,064 Expert Mod 8TB
rot points to the fourth Rot_matrix in the array Rot, therefore rot[0][0] is a float, and rot[0] should be an array of 3 floats.
The important point when determining the type of a pointer or subscripted variable is not what you pointed it to but how you declared it.
Oct 21 '08 #5
Thanks a lot for the help. I've been studying what you said and I thought I understood after playing around with the code. Unfortunately, I am not quite there yet. I reduced my test code to the following:

Expand|Select|Wrap|Line Numbers
  1. typedef float Rot_matrix[3][3];
  2.  
  3. int main(void){
  4.   Rot_matrix Rot[9];
  5.  
  6.   float **rot = (float **) Rot[2];
  7.   rot[1][1] = 1.4;
  8. }
  9.  
As I understood pointers this should work, and it does compile, however the program ends with a "Segmentation fault" when it tries to assign the 1.4 to rot[1][1]. Any clues to the cause of this would be highly appreciated as I am running out of ideas.

Regards,
Harry
Oct 22 '08 #6
boxfish
469 Expert 256MB
Why do you have the typedef? I don't know why your code doesn't work, but the following code compiles and runs fine for me:
Expand|Select|Wrap|Line Numbers
  1. int main(void){
  2.   float Rot[3][3];
  3.  
  4.   float **rot = (float **) Rot[2];
  5.   Rot[1][1] = 1.4;
  6.   return 0;
  7. }
  8.  
I don't understand this line either; what's it supposed to do?
float **rot = (float **) Rot[2];
Thanks.
Oct 22 '08 #7
Banfa
9,064 Expert Mod 8TB
boxfish, the code produces undefined behaviour, which means that it compiling and running for you means nothing. Always visually check the code for undefined behaviour before trying someone else's example :-)

Harry8888, examining these 2 lines

float **rot = (float **) Rot[2];
rot[1][1] = 1.4;

Firstly on casting in general. If you are having to put a cast into your code then you probably have a problem. You should never cast just to get rid of a compiler error or warning, you will certainly cause yourself headaches and cover up problems that would later be hard to find and fix.

If the types are correct you will be able to assign without a cast.

What errors do you get if you remove the cast and compile?

In the second line of code again what is the type of rot[1]? How will the computer interpret it? What about rot[1][1] how will the computer interpret that?

You really should try to get your head round these questions, it will increase your understanding of pointers.


Is there a reason you have not declared rot with a type of Rot_matrix *?
Oct 23 '08 #8
I am trying to answer your questions, but it will take some time I think before I get it, which is something I lack at the moment. However, I'll will get back to this. Unfortunately I will not have access to the Internet next week, so it will be somewhere next weekend. Thanks for all the help uptil now

Regards,
Harry
Oct 23 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Sam Collett | last post: by
26 posts views Thread by =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post: by
4 posts views Thread by aneuryzma | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.