/******************************************************************************
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
gabrielstaples.com
9 Nov. 2020
Array as input param tests.
Add these build flags (see here, a few bullets down:
https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world#additional-c-and-c-build-notes-ex-wgcc-or-clang-compilers):
-Wall -Wextra
References:
1. [my own ans] https://stackoverflow.com/questions/6567742/passing-an-array-as-an-argument-to-a-function-in-c/51527502#51527502
OnlineGDB Share link: https://onlinegdb.com/ryzqS4vKv
*******************************************************************************/
#include <stdio.h>
void arraytest1(int a[])
{
// Same as `sizeof(int*)`: 8
// warning: ‘sizeof’ on array function parameter ‘a’ will return size of ‘int *’ [-Wsizeof-array-argument]
printf("arraytest1: sizeof(a) = %lu\n", sizeof(a));
}
void arraytest2(int a[0])
{
// Same as `sizeof(int*)`: 8
// warning: ‘sizeof’ on array function parameter ‘a’ will return size of ‘int *’ [-Wsizeof-array-argument]
printf("arraytest1: sizeof(a) = %lu\n", sizeof(a));
}
void arraytest3(int a[2])
{
// Same as `sizeof(int*)`: 8
// warning: ‘sizeof’ on array function parameter ‘a’ will return size of ‘int *’ [-Wsizeof-array-argument]
printf("arraytest1: sizeof(a) = %lu\n", sizeof(a));
}
// Requires as an input: pointer to an array with two elements of `int`--ie: of type ===> `int (*)[2]` <===
// NB: https://cdecl.org/ tells us that `int (*a)[2]` means: `declare a as pointer to array 2 of int`
void arraytest4(int (*a)[2])
{
printf("arraytest1: sizeof(a) = %lu\n", sizeof(a));
}
int main()
{
printf("Hello World\n");
int array1[2];
// works fine
arraytest1(array1);
// main.c:46:16: warning: passing argument 1 of ‘arraytest1’ from incompatible pointer type [-Wincompatible-pointer-types]
// main.c:25:6: note: expected ‘int *’ but argument is of type ‘int (*)[2]’
arraytest1(&array1);
printf("\n");
// works fine
arraytest2(array1);
// main.c:46:16: warning: passing argument 1 of ‘arraytest1’ from incompatible pointer type [-Wincompatible-pointer-types]
// main.c:25:6: note: expected ‘int *’ but argument is of type ‘int (*)[2]’
arraytest2(&array1);
printf("\n");
// works fine
arraytest3(array1);
// main.c:46:16: warning: passing argument 1 of ‘arraytest1’ from incompatible pointer type [-Wincompatible-pointer-types]
// main.c:25:6: note: expected ‘int *’ but argument is of type ‘int (*)[2]’
arraytest3(&array1);
printf("\n");
// REVERSED WARNINGS ON THIS ONE!
// main.c:81:16: warning: passing argument 1 of ‘arraytest4’ from incompatible pointer type [-Wincompatible-pointer-types]
// main.c:47:6: note: expected ‘int (*)[2]’ but argument is of type ‘int *’
arraytest4(array1);
// works fine
arraytest4(&array1);
printf("\n");
int array2[100];
// main.c:81:16: warning: passing argument 1 of ‘arraytest4’ from incompatible pointer type [-Wincompatible-pointer-types]
// main.c:47:6: note: expected ‘int (*)[2]’ but argument is of type ‘int *’
arraytest4(array2);
// main.c:94:16: warning: passing argument 1 of ‘arraytest4’ from incompatible pointer type [-Wincompatible-pointer-types]
// main.c:48:6: note: expected ‘int (*)[2]’ but argument is of type ‘int (*)[100]’
arraytest4(&array2);
printf("\n");
// Back to `arraytest3()`, passing in explicit ptr to int:
int *array3 = array2;
// int *array4 = &array2; // warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
int *array4 = (int *)(&array2); // works fine!
int (*array5)[2] = &array1; // works fine <======= INTERESTING "TYPE SAFE" PTR TO ARRAY OF SIZE 2 OF TYPE INT" =========
arraytest3(array3); // works fine!
// arraytest3(&array3);
arraytest3(array4); // works fine!
arraytest4(array5); // works fine <======= INTERESTING USAGE OF "TYPE SAFE" PTR TO ARRAY OF SIZE 2 OF TYPE INT" =========
printf("\n");
return 0;
}