0

I have to make a 2D array that carries the information of a table

I can't find the syntax error in the following

int majorPositions[][]={{0,90,-90},{90,15,90},{-45,-30,30},{0,60,0}};

also, how do I enter last column details as it is a char, not an integer

1
  • 5
    If you're going to stick with integers, encode the on/off as 1/0. Alternatively, create a structure type with elements x, y, z and tool — which need not all be integers. This would have an additional advantage; you'd only need a 1D array (of structures), not a 2D array (of integers). Commented Jun 12, 2019 at 4:26

5 Answers 5

2

You have to set the size of the array like so:

int majorPositions[][3]={{0,90,-90},{90,15,90},{-45,-30,30},{0,60,0}};

or

int majorPositions[4][3]={{0,90,-90},{90,15,90},{-45,-30,30},{0,60,0}};

If your last column is just ON/OFF, cant you just represent it as a 1 or 0?

Sign up to request clarification or add additional context in comments.

3 Comments

no i cant replace it. coz 0 and 1 are TRUE and FALSE in my code
Sorry, my bad...0 is False and 1 is True. i just mistyped it
Like Jonathan said, a structure would be a good choice. You will be able to name the struct variables X,Y,Z,Tool to make it easier for others to understand your code.
1

You have to specify the size of inner array as following.

int majorPositions[][3]={{0,90,-90},{90,15,90},{-45,-30,30},{0,60,0}};

And, I think you can encode ON and OFF into 1 and 0.

7 Comments

0 and 1 are FALSE and TRUE in my code...being used for something else. any other suggestion?
@Gurjotkaur: Since ON and OFF are separate from TRUE and FALSE, there's no problem using 1 and 0 for ON and OFF, as well as using 1 for TRUE and 0 for FALSE (but 0 for TRUE and 1 for FALSE is completely anti-idiomatic C). You could use enum ToolAttached { OFF = 0, ON = 1 } and then enum ToolAttached tool; as part of a structure.
@JonathanLeffler after defining ON AND OFF as 1 and 0. the code: int majorPositions[4][4]={{0,90,-90,OFF},{90,15,90,ON},{-45,-30,30,ON},{0,60,0,OFF}}; is also syntax error
@Gurjotkaur There should be some info displayed along with that error.
@Gurjotkaur you can use 1/0 for ON/OFF AND TRUE/FALSE. There is no issue with that. The context in which the values are used determines whether it is representing TRUE/FALSE or ON/OFF.
|
1

I would use something like (like the other mention above)

#include <stdbool.h>

typedef struct
{
   int x;
   int y;
   int z;
   bool Tool;
} TableEntry_t;

TableEntry_t majorPositions[4];

It´s a clear solution and you can easily extend the table if you want.

4 Comments

If you #include <stdbool.h> you could use bool as a type and true and false as values. I'd never define booleans myself. This header was introduced 20 years ago in C99.
Using a struct will probably lead to less efficient code though. The compiler is going to pad that bool to alignment anyway.
You can avoid this when you add __attribute__ ((packed)). In this case the compiler don´t pad (if the compiler uses a padding). If the compiler doesn´t pad you don´t need this.
The compiler doesn't pad for fun; it does so because the code will be more efficient than if it is unpadded.
0

You need to type out the inner dimension size(s) explicitly. To simplify everything, consider taking advantage of the fact that booleans in C are just glorified integers. You can use an int as bool and vice versa.

#include <stdbool.h>

int majorPositions[][4]=
{
  {  0,  90, -90, false },
  { 90,  15,  90, true  },
  {-45, -30,  30, true  },
  {  0,  60,   0, false },
};

You can get the size of this array at compile-time using sizeof(majorPositions)/sizeof(majorPositions[0]) (which will be 4 in this case).

Alternatively use a struct as proposed in other answers. It may or may not give less efficient code.

Comments

0

In two dimensional arrays, you must specify second dimension of an array during the declaration.

int majorPositions[][3]={{0,90,-90},{90,15,90},{-45,-30,30},{0,60,0}};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.