cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert array to int

meriarajagukguk
Associate

halo!

i have a adc_value as array and i hava this for potentiometer, i want to call the adc_value as int, but i can't thingking for this now, can anyone help me?

7 REPLIES 7
Andrew Neil
Evangelist III

Your question doesn't make sense - please clarify.


@meriarajagukguk wrote:

halo!

i have a adc_value as array and i hava this for potentiometer


So what does this "array" represent - a sequence of samples of the potentiometer values?

What type is the array?

 


@meriarajagukguk wrote:

i want to call the adc_value as int, 


What do you mean by that?

Surely, each element in the array can be accessed in the normal way? Cast it to an int if it isn't already an int.

 

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

 

I have an ADC value that I store in uint32_t BUFFER_DAC[4];
and I want to use that value in 'int input' but I don't know how to convert/call it to be int.
Can anyone help me?

Conversion from uint32_t to int is automatic. Shouldn't require anything more than using the variable name.

int x = BUFFER_DAC[0];

Buffer should probably be uint16_t, not uint32_t.

If you feel a post has answered your question, please click "Accept as Solution".

but the result for int x = BUFFER_DAC[0]; is :

 ../Core/Src/main.c(39): warning: array index 4 is past the end of the array (that has type 'uint32_t[4]' (aka 'unsigned int[4]')) [-Warray-bounds] why?

and for uint32_t AD_RES_BUFFER[4]; i'm a beginner and to be honestly i used that from tutorial


@meriarajagukguk wrote:

I have an ADC value that I store in uint32_t BUFFER_DAC[4];


still unclear what you mean by that.

Is that four successive samples, each 32 bits?  Or is one sample 128 bits (4x32) ???

 


@meriarajagukguk wrote:

but the result for int x = BUFFER_DAC[0]; is :

 ../Core/Src/main.c(39): warning: array index 4 is past the end of the array (that has type 'uint32_t[4]' (aka 'unsigned int[4]')) [-Warray-bounds] why?


Clearly, that error does not relate to the line of code you showed.

 

int x = BUFFER_DAC[0]; 

 

That should work perfectly fine - the array index there is 0 - not 4 !

(but, if the array is uint32_t, then you should also declare x as uint32_t).

You need to show the actual code which is producing that error.

See the tips for how to properly post source code:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

Also post the full build output - using the same </>  button.

 


@meriarajagukguk wrote:

i'm a beginner and to be honestly i used that from tutorial


Please give a link to that tutorial.

It sounds like you need to take a step back, and spend some time on the basics of the C programming language

https://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/ 

 

 


@meriarajagukguk wrote:

warning: array index 4 is past the end of the array (that has type 'uint32_t[4]'rial


Remember array indexes in C start from zero:

uint32_t array[4];  // Declares an array of four elements
                    // Because indexes start at zero, 
                    // those elements are accessed as:
                    //   array[0] - the first element;
                    //   array[1] - the second element;
                    //   array[2] - the third element;
                    //   array[3] - the fourth & final element.