I am trying to make two dimensional dynamic string arrays with some success but for some reason two int variables, which are in fact the number of the rows (array of pointers) and the size of the strings (how long they can be), change to a mysterious value.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void write (char ***p, int size_r, int size_c)
{
int i;
for (i = 0; i < size_r; i++)
{
printf("%s", p[i]);
}
return;
}
void arr_of_p (char*** p, int size_r, int size_c)
{
int i;
for (i = 0; i < size_r; i++)
{
p[i] = "helo\n";
}
return;
}
int main(void)
{
int string_n; printf("How many strings: "); scanf("%d", &string_n);
int string_l; printf("How long are the strings: "); scanf("%d", &string_l);
char **s_p = (char**) malloc(string_n*sizeof(char*));
int i;
for (i = 0; i < string_n; i++)
{
s_p[i] = (char*) malloc(string_l*sizeof(char));
}
arr_of_p(&s_p, string_n, string_l);
printf("%d\n%d\n", string_n, string_l); // for debugging purpose, add breakpoint here.
//"string_n" and "string_l" will be identical to the value of "i" in "arr_of_p()" for some reason...
write(&s_p, string_n, string_l);
return 0;
}