This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Lookup Table Using Array

Hi,

This is my code:

#define NUMBER_OF_ROWS 50
#define NUMBER_OF_COLS 2
unsigned int Array[NUMBER_OF_ROWS] [NUMBER_OF_COLS] = {
{100,50},
{20 ,10 },
{16 , 8 },
{14 , 7 },
{12 , 6 },
{11 , 5.5 },
{10 , 5 },
{9.1, 4.55},
{2 , 1 } and so on until 50 rows..};

The problem is I have 50 elements needed for a lookup table, but the following error occur: Array.h(57): error C242: 'array[]': too many initializers

When I initialize number of rows to 45, no error prompt. I need to use all the 50 elements. Is this the only way for me to create a lookup table? Can someone guide me on using more simpler method? Please help me. Thanks.

  • Interesting - you store floating point values in an unsigned int array. Does this work well for you?

    Different compilers have different limits for how much nesting or max size of arrays etc.

    And it can also matter where you store the array - why not store the array in the code since I assume you will not change any values in the array?

  • Yea, the floating values work with my code. I don't want to store inside the code because I might change the value according to the table. That is why I need to create lookup table. The problem is, I can't store more than 45 elements. Is there anything I can do or perhaps other than array?

  • Floating point values can be stored - and then retrieved - from an unsigned int??? Are you really, really sure? Note that the compiler has to "downgrade" from float to int when assigning to an int. So having the value 9.1 in the source code will just trick a developer into believing you have an array with that value when in reality you haven't.

    Have you considered looking at the memory map of your processor? Exactly which memory region do you want the compiler to store your 50*2*2 = 200 byte large array in? Are you using a memory model where all variables are stored in XRAM?

  • It isn't impossible for a compiler to complain if a data structure is too large compared to the intended use. After all - it's the compiler who needs to consider instructions to address the individual data of the data structures.

  • But it's not complaining about size - it's complaining specifically about number of initialisers...

  • Thanks for the reply guys. I have solve the problem. It is in the setting. the 8051 have 64K of memory, where when you create a project, it will automatically set the limitations to 2K. What I did is change the 2K to large data setting in Keil.

    About the floating value, I store the floating value inside an array, but there is a formula calculation which I created will take the floating value and it works well.

    Thanks Guys! :D

  • Guys, another one. How can I create .dll file in Keil from my existing project?

  • I store the floating value inside an array, but there is a formula calculation which I created will take the floating value and it works well.
    Better have a check.
    Why not have array of 'float' type instead of 'int'?

  • "I store the floating value inside an array"

    No, you don't!

    You're using an int array. An int array does not store floating-point values!

    "a formula calculation which I created will take the floating value"

    But you have lost the floating value - it has been truncated to int

    "it works well"

    As it should - with values that always have a zero fractional part!!

  • If that fixes the problem, then I think you cited the wrong error message in your original post!

    error C242: 'array[]': too many initializers
    

    That error is specifically about the number of initialisers - "too many" - not the overall size of the memory consumed (which would have been some sort of "overflow" message).

    However, you must have fixed both the problems now - so it's all OK in the end!

    :-)

  • Except that the solution should seldom be to switch memory model - it is normally way better to just force large data structures to a suitable memory region while keeping the memory model for efficiency.

    Forget about his float discussions. He has been told but just doesn't want to listen. Just because the code gets through the compiler he thinks it's ok, and never realizes that his lookup table computes slightly incorrect results. He just have never written any test harness to validate the output of his code - he's happy with "close enough" as "must be working perfectly".

  • Guys,

    I know if put in floating values inside an int's, I will not get my code to run or calculate the way I want it to. I had previously done it and it was a failure. Now, after I went into research, I found out that storing any floating values is okay as long as the calculation is indeed in the form of float or double. My calculation are based on double values and not int. I have test it and got the confirmation. I am not just simply doing it so it works. I am doing it to get the perfection as possible. These are all the formulas I created and the code I posted above is not in full form. The compiler doesn't confirm if my code are working with the actual hardware.

  • Wrong.

    The code will run; it will just be using integer values in its floating calculations - so you just lose the fractional parts of all the values that go via an integral type.

  • Can you compute the value in output variable in following cases?

    case 1:

    float output;
    output = 22/7;
    

    case 2:

    float output,a=22,b=7;
    
    output = a/b;
    

    case 3:

    float a=22,b=7;
    int output;
    
    output = a/b;
    

  • So - why (?) are you so stubborn?

    Haven't you realized that there are people here who have 10, 20 or even more years of experience and actually do know for a fact that your code will produce the wrong result because your floating point values will be converted to integer values - and losing the decimal part?

    Why do you think the language have both integer and floating point data types of an integer data type can store a floating point value?

    Your code is a failure as written. You could rewrite it to use fixed-point integer arithmetic and get great results. But that demands that you (!) do spend some time learning. Right now, you are just spending your time assuming. Well ass-u-me isn't a good way to write software. Now remove the blinders and start to learn. 9.1, after storing it in an int is no more 9.1. That should be 100% obvious if you decided to actually think about it.