Created
February 25, 2015 22:49
-
-
Save odeke-em/a756f57d6b92c7df3d0d to your computer and use it in GitHub Desktop.
const+spillover: A trivial reproduction of what tripped me out with using const within a for loop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
int main() { | |
char *lang = "c_language"; | |
size_t len = strlen(lang); | |
size_t i; | |
for (i = 0; i < len; ++i) { | |
const char c = lang[i]; | |
printf("%c", c); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var lang = 'javascript'; | |
for (var i = 0, len = lang.length; i < len; ++i) { | |
var c = lang[i]; | |
console.log(c); // Should iterate through all the letters | |
} | |
// Spill over expected | |
console.log(c); // Expecting lang[i] | |
for (var i = 0, len = lang.length; i < len; ++i) { | |
const c = lang[i]; | |
console.log(c); // Should always give 'j' | |
} | |
// Spill over still expected | |
console.log(c); // Should give 'j' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment