Inline println

Hello,

I want to know if it is possible to print in array in the serial monitor in the form 0 0 0 0 0 0 for a 6 element array.

Currently I am using the following line of code. Combo_try[6] = {0,0,0,0,0,0}

Serial.println(combo_try[6]);

What I am getting is : 362 36 0þ0 0 0 0 ...

Thanks you

M.

Post all of your code, not just snippets. Or better yet, post the minimum amount of code that (1) compiles and (2) demonstrates the problem.

you need to post all of your code,

but one obvious thing to check, is to make sure the baud rate of your serial port, and the baud rate of the monitor match

Hawk_08:
I want to know if it is possible to print in array in the serial monitor in the form 0 0 0 0 0 0 for a 6 element array.

No, the print class (like Serial) can’t print arrays; just numbers and strings. You can easily make a loop though:

int ary[6];
Serial.print('[');
Serial.print(ary[0]);
for (int i=1; i<6; i++) {
  Serial.print(", ");
  Serial.print(ary[i]);
}
Serial.println(']');

Excellent code, ready to be put into a function… :slight_smile:

int someList[6];

#define ARY_LEN(a) (sizeof(a)/sizeof(a[0]))

void printArray(int ary[], int numElements) {
    Serial.print('[');
    if (numElements > 0) {
        Serial.print(ary[0]);
        for (int i=1; i < numElements; i++) {
            Serial.print(", ");
            Serial.print(ary[i]);
        }
    }
    Serial.println(']');
}

void setup() { 
    Serial.begin(115200);
    printArray(someList, ARY_LEN(someList));
}

void loop() {
}
Serial.println(combo_try[6]);

It may by now be obvious to you, but a six element array doesn't have a subscript 6.

AWOL: Serial.println(combo_try[6]);

It may by now be obvious to you, but a six element array doesn't have a subscript 6.

He was probably trying to tell print() that the array had 6 elements.

Thank you everyone,

It was exactly the information I was looking for!

M.

I have two questions on this part of the code : 1. Is this used to calculate the size of the array with the position in memory? 2. What is 'a' is some general form to use when defining a variable or a relation?

tuxduino: ```

define ARY_LEN(a) (sizeof(a)/sizeof(a[0]))

Thx!

M.

Hawk_08:
I have two questions on this part of the code :

  1. Is this used to calculate the size of the array with the position in memory?

It’s used to calculate the number of items in an array.

sizeof(a) returns the number of bytes that the array occupies. So for example int myArray[10] would occupy 20 bytes because an int occupies 2 bytes.
sizeof(a[0]) returns the size of the first element of the array, so in our int myArray[10] example, it would return 2. Meaning the entire macro returns 20 / 2 which is the number of elements in the array.

2. What is 'a' is some general form to use when defining a variable or a relation?

‘a’ is simple the replacing text in the macro. When you call the macro like this:

ARY_LEN(myArray)

The preprocessor converts it to this:

(sizeof(myArray)/sizeof(myArray[0]))