Why this function works
#include <stdio.h>
#include <string.h>
void print_array(char **array, size_t size)
{
for (int i = 0; i < size; i++) {
printf("%s\n",array[i]);
}
}
int main()
{
char *base_data[] = {
"sample 1",
"sample 2",
"sample 3"
};
print_array(base_data,sizeof(base_data)/sizeof(char*));
return 0;
}
But this code does not
#include <stdio.h>
#include <string.h>
void print_array(char **array)
{
for (int i = 0; i < sizeof(array)/sizeof(char*); i++) {
printf("%s\n",array[i]);
}
}
int main()
{
char *base_data[] = {
"sample 1",
"sample 2",
"sample 3"
};
print_array(base_data);
return 0;
}
The program crashes after printing first value. No matter what I try, I am not able to find the size of the array of pointers to strings inside a function call. Most of the stackoverflow examples also pass the size of array to the functions. What happens to an array of pointers to strings when its passed to functions?