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 -
typedef float Rot_matrix[3][3];
-
typedef float Vector[3];
-
-
int main(void){
-
Rot_matrix Rot[9];
-
float *rot = (float *)Rot[4];
-
Vector j = {1, 2, 3};
-
Vector *i = (Vector *) j;
-
(*i)[0] = rot[0][0];
-
return 0;
-
}
-
8 25409 Banfa 9,065
Recognized Expert Moderator Expert
What is the type of rot[0]?
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
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 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.
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: -
typedef float Rot_matrix[3][3];
-
-
int main(void){
-
Rot_matrix Rot[9];
-
-
float **rot = (float **) Rot[2];
-
rot[1][1] = 1.4;
-
}
-
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
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: -
int main(void){
-
float Rot[3][3];
-
-
float **rot = (float **) Rot[2];
-
Rot[1][1] = 1.4;
-
return 0;
-
}
-
I don't understand this line either; what's it supposed to do?
float **rot = (float **) Rot[2];
Thanks.
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 *?
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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;...
|
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
|
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...
|
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...
|
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...
|
by: aneuryzma |
last post by:
Hi,
this is my code:
IplImage *image = 0;
....
if( !image ) {
image = cvCreateImage( cvGetSize(frame), 8, 3 );
|
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,...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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 ...
| |