It is more question than a problem. I make my header file with a function declaration like this:
void fw_iteracja_wsk_rows_a(float (*tab)[COLUMNS], int ROWS);
How can I adjust/change the COLUMNS value depending on the array size in other files?
Currently, no matter what I try, COLUMNS takes the value from the header file, making the function not universal.
To be clear im using VS2022 with my travel to learn C. This is a definition of the function:
void fw_iteracja_wsk_rows_a(float (*tab)[COLUMNS], int ROWS)
{
float (*p_rows)[COLUMNS];
float* p_columns;
p_rows = tab;
p_columns = *p_rows;
for (p_rows; p_rows < tab + ROWS; p_rows++)
{
for (p_columns; p_columns < *p_rows + COLUMNS; p_columns++)
{
printf("%.1f, ", *p_columns);
}
printf("\n");
}
}
Declaration is in file: C_PATMY_2DARRAY.h Also in this file is: #define COLUMNS 12. Definition is in file: C_PATMY_2DARRAY.c
When I #include "C_PATMY_2DARRAY.h in other .c file and try to use this function with array with diffrent columns number. I cant becouse function is getting COLUMNS value form header file. I did try #ifndef #undef but it is not working.
Grateful for the tip.
@babon - i can agree with overcomplicating. But, did it becouse i need this pointer on array: float (*tab)[COLUMNS] for being able to make iteration with pointers.
Maybe someone can tell me how to declare universal functions using a pointer to an array. When I need to specify the number of columns in the declaration and definition.
void fw_iteracja_wsk_rows_a(float *tab, int COLUMNS, int ROWS). Let the caller pass the number of columns and rows. Insidefw_iteracja_wsk_rows_amanually calculate the offset of each element in the array and print it.tabparameter to be a pointer to a variably modified array (VMA) like this:void fw_iteracja_wsk_rows_a(int ROWS, int COLUMNS, float (*tab)[COLUMNS]);. However note that support for pointers to VMAs is optional in some versions of the C standard. You may need to rename theCOLUMNSparameter if you have a macro of the same name in scope.