Go Down

Topic: Error in an example on http://arduino.cc/en/Reference/Array (Read 2826 times) previous topic - next topic

pietrasopra

May 01, 2014, 10:03 pm Last Edit: May 01, 2014, 10:12 pm by pietrasopra Reason: 1
Dear Webmaster,
I think it might be necessary a correction in the example written in web page http://arduino.cc/en/Reference/Array,
section "Creating (Declaring) an Array":
wrong text:
Code: [Select]
int myInts[6];
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[6] = {2, 4, -8, 3, 2};


proper text:
Code: [Select]
int myInts[5];
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[5] = {2, 4, -8, 3, 2};


I suggested the correction here because "Corrections, suggestions, and new documentation should be posted to the Forum".

robtillaart

No, this is correct C although I agree it not "clean code"
int mySensVals[6] = {2, 4, -8, 3, 2};
initializes the first 5 elements of the array of size 6.
Most compilers will initialize the 6th element with zero iff the arra is declared global.

Check this code.

Code: [Select]

int mySensVals[6] = {2, 4, -8, 3, 2};

void setup()
{
  Serial.begin(115200);
  Serial.println("Start ");
 
  for (int i=0; i<6; i++)
  {
    Serial.println(mySensVals[i]);
  }
}

void loop()
{
}

That said, I agree it is not a clean example.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Grumpy_Mike

Why do you think that
Code: [Select]
int myInts[6];
should be
Code: [Select]
int myInts[5];
As this variable is not referenced anywhere there could be any number in the braces.

Besides which we have bee trying to get the real error of the capacitor on the latch pin in the shift out tutorial, fixed for over 6 years and that is a real error that can cause real harm.

Go Up