473,471 Members | 4,650 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

error: subscripted value is neither array nor pointer

4 New Member
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 25409
Banfa
9,065 Recognized Expert Moderator Expert
What is the type of rot[0]?
Oct 21 '08 #2
Harry8888
4 New Member
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,426 Recognized Expert Top Contributor
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,065 Recognized Expert Moderator Expert
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
Harry8888
4 New Member
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 Recognized Expert Contributor
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,065 Recognized Expert Moderator Expert
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
Harry8888
4 New Member
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

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

Similar topics

12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
4
by: Kobu | last post by:
I've read the FAQ and several posts on multidimensional arrays and how their names decay to pointer to arrays (not pointer to pointers). If this is so, why does the following code fragment...
5
by: s88 | last post by:
Howdy: the follows is my program, I wanna change my structure array pointer in the function "testfunc", but I fail..., I also try to call the testfunc by reference, but the compiler says...
8
by: Ben | last post by:
Hi, I am having trouble debugging a segmentation fault...here's my data structure: typedef struct CELL *pCELL; /* Pointers to cells */ struct CELL { SYMBOL symbol; pCELL prev_in_block;...
2
by: Ros | last post by:
Peeps, I need help with trying to find a value in array of arrays. Public Module myModule Private Site_Access() As Array Property Set_SA() Get Return Site_Access End Get
13
by: stephen b | last post by:
(apologies for cross posting from the moderated group..i'm sure you understand) Hello, I'm passing an array into a Constructor and hoping to use it as a pointer and store it as a class member...
1
by: andyIOM | last post by:
okay, i have tried,googled and read but havn't managed to figure out where the error is. bascically,my file reads an in put file which looks like this: Is 2 1 50 R1 2 9 500 R2 1 0 100 R3 2...
26
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Do you think we can reach any kind of consensus on whether the following code's behaviour is undefined by the Standard? int my_array; int const *const pend = *(&my_array + 1); Considering...
4
by: aneuryzma | last post by:
Hi, this is my code: IplImage *image = 0; .... if( !image ) { image = cvCreateImage( cvGetSize(frame), 8, 3 );
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.