Hi All,
The following code prints out values of a 2 D array in main as well as in the function printvalues. However, fd->mychar[i] = newptr[i]; throws an error. I understand that a ** myarray is not the same as myarray [][] and I am trying to figure out a solution to this. My problem is, in my real program (I reproduce the same problem below with minimal code), I am modifying pieces of pre-existing code of a 10,000+ lines, so the argument to printvalues(char **) cannot be changed, it has to be a double pointer, as this is how its used in several places in the code (and I dont want to break anything), and, structure mystruct is large with multiple 2D arrays such as mychar, and since I want to always ensure a contiguous block of memory is allocated to mystruct, I cannot use pointers like so: char *mychar [20].
With these restrictions, is it still possible to make this work? How? Thanks!
Code:
typedef struct mystruct{
char mychar [20][20];
}mystruct_t;
void printvalues ( char ** newptr){
int i;
mystruct_t * fd;
for (i=0;i<3;i++){
printf("My value is %s\n", newptr[i]); // prints fine if following 2 lines commented
fd->mychar[i] = newptr[i]; // error: incompatible types in assignment
printf("My value is %s and in struct %s\n", newptr[i], fd->mychar[i]);
}
}
int main (int argc, char **argv){
int i;
char * abc[5] = {"123", "456", "789"};
for (i=0;i<3;i++){
printf("My value is %s\n", abc[i]);
}
printvalues(abc);
}