Type for for() variable and strange results

Hello.
I have a problem with my sketch on Arduino Nano (ATmega328P). Arduino IDE 1.8.7.
Here is my simple sketch code:

void func1(uint8_t *arr)
{
  char tstr[200];
  Serial.print("arr: ");
  for (uint16_t byte_num = 0; byte_num < 16; ++byte_num)
  {
    Serial.print(arr[byte_num]);
    Serial.print(" ");
  }
  Serial.print("\n-------\n");

  uint32_t k[4];
  for (uint32_t i = 0; i < 4; ++i)  //Try another types: uint32_t, uint16_t, uint8_t
  {
    k[i] = arr[i];
    sprintf(tstr, "k[%d] = %lu\n", i, k[i]);
    Serial.print(tstr);
  }
  Serial.println();
}

void setup() {
  Serial.begin(115200);

  uint8_t myarr[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
  func1(myarr);
}

void loop() {}

Output for type uint8_t is:

arr: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
-------
k[0] = 0
k[1] = 1
k[2] = 2
k[3] = 3

Output for type uint16_t is:

arr: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
-------
k[0] = 0
k[1] = 1
k[2] = 2
k[3] = 3

Output for type uint32_t is:

arr: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
-------
k[0] = 0
k[1] = 65536
k[2] = 131072
k[3] = 196608

Can somebody explain why output not same?

The error is in line 32 of your code. Oh...wait...you didn't show us any code.

econjack:
The error is in line 32 of your code. Oh...wait...you didn't show us any code.

29 lines of code total.
Can you see the code? I can duplicate it here without "code" tags:

void func1(uint8_t *arr)
{
char tstr[200];
Serial.print("arr: ");
for (uint16_t byte_num = 0; byte_num < 16; ++byte_num)
{
Serial.print(arr[byte_num]);
Serial.print(" ");
}
Serial.print("\n-------\n");

uint32_t k[4];
for (uint32_t i = 0; i < 4; ++i) //Try another types: uint32_t, uint16_t, uint8_t
{
k = arr*;*
_ sprintf(tstr, "k[%d] = %lu\n", i, k*);
Serial.print(tstr);
}
Serial.println();
}
void setup() {
Serial.begin(115200);_
uint8_t myarr[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
_ func1(myarr);
}*
void loop() {}_

Use the correct size for i and it will work as expected.

   sprintf(tstr, "k[%ld] = %lu\n", i, k[i]);

Easy to catch if you had activated warnings:

Somewhere\strangefor\strangefor.ino:17:43: warning: format '%d' expects argument of type 'int', but argument 3 has type 'uint32_t {aka long unsigned int}' [-Wformat=]

     sprintf(tstr, "k[%d] = %lu\n", i, k[i]);

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

The opening post has correctly posted code. Post was not modified, so not sure why some people could not see it.

sterretje:
The opening post has correctly posted code. Post was not modified, so not sure why some people could not see it.

He either edited the original post or something weird is going on because there was no code in the original post.

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Sorry. Will not happen again. I duplicate code, because econjack can't see it in original post, maybe some browser incompatibility.

Whandall:
Use the correct size for i and it will work as expected.

   sprintf(tstr, "k[%ld] = %lu\n", i, k[i]);

Easy to catch if you had activated warnings:

Somewhere\strangefor\strangefor.ino:17:43: warning: format '%d' expects argument of type 'int', but argument 3 has type 'uint32_t {aka long unsigned int}' [-Wformat=]

sprintf(tstr, "k[%d] = %lu\n", i, k[i]);

Many thanks! It's my coding error. It's a shame.
I tried to simplify my program to show error. Now I think it is bad idea. The real error was hiding behind the wrong debugging output.