Hello every one I have made a small function which takes a pointer to 2D array and fill it with word linearly i.e. I fill my 2D array row by row with the give word.I think its working fine but i got segmentation fault when I tries to print my array.Can any one help me where am I going wrong.
Thanks
void create_table(char *key ,char (*table)[5]){
int row=0, col=0;
while(*key){
*(table + row++)[col++] = *key++;
printf("%c" , table[0][0]); // Here I got seg fault
if(col == 4){
col=0;
row++;
}
}
}
//===================================
int main(){
char table[5][5];
create_table("monarchy" ,table);
return 0;
}
Should not be “row++”, just “row”. You’re incrementing “row” elsewhere already.