online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere in world. GS www.ElectricRCAircraftGuy.com 24 Sept. 2020 Learn how to pass sized arrays as parameters (via references) in C++! References: 1. https://stackoverflow.com/questions/1328223/when-a-function-has-a-specific-size-array-parameter-why-is-it-replaced-with-a-p/1328246#1328246 1. https://stackoverflow.com/questions/1328223/when-a-function-has-a-specific-size-array-parameter-why-is-it-replaced-with-a-p/1328242#1328242 *******************************************************************************/ #include <cstdint> #include <cstdio> void foo(uint8_t array[100]) { // is ALWAYS sizeof(uint8_t*), which is 8! printf("sizeof(array) = %lu\n", sizeof(array)); } void foo2(uint8_t (&array)[100]) { printf("sizeof(array) = %lu\n", sizeof(array)); } template<size_t N> void foo3(uint8_t (&array)[N]) { printf("sizeof(array) = %lu\n", sizeof(array)); } int main() { printf("Hello World\n"); printf("\n"); uint8_t a1[10]; uint8_t a2[11]; uint8_t a3[12]; // Is `sizeof(array) = 8` for all of these! foo(a1); foo(a2); foo(a3); printf("\n"); // Fails to compile for these 3! Sample error: // > main.cpp:49:12: error: invalid initialization of reference of type ‘uint8_t (&)[100] // > {aka unsigned char (&)[100]}’ from expression of type ‘uint8_t [10] {aka unsigned char [10]}’ // > foo2(a1); // > ^ // foo2(a1); // foo2(a2); // foo2(a3); // ------------------ // Works just fine for this one since the array `a4` has the right length! // Is `sizeof(array) = 100` uint8_t a4[100]; foo2(a4); printf("\n"); foo3(a1); foo3(a2); foo3(a3); foo3(a4); printf("\n"); return 0; } /* SAMPLE OUTPUT: Hello World sizeof(array) = 8 sizeof(array) = 8 sizeof(array) = 8 sizeof(array) = 100 sizeof(array) = 10 sizeof(array) = 11 sizeof(array) = 12 sizeof(array) = 100 */

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue