I'm just curious what is the correct output? You leave no space for the terminating null character in your array. When you give enough space it only reads the first column. With out enough space to hold the null character it keeps reading to the next block of memory until the null character.
Code:
#include<stdio.h>
#define ROWSIZE 2
#define COLSIZE 11
void copy(char (*ptr)[COLSIZE], char (*ptr2)[COLSIZE]);
void CPY(char a[][COLSIZE], char b[][COLSIZE]);
int main(void)
{
char array1[ROWSIZE][COLSIZE]={{'a','b','c','d','e','f','g','h','i','j'},
{'1','2','3','4','5','6','7','8','9','\0'}};
char array2[ROWSIZE][COLSIZE]={{'\0'},{'\0'}};
char (*ptr1)[COLSIZE];
char (*ptr2)[COLSIZE];
ptr1=array1;
ptr2=array2;
copy(ptr2, ptr1);
printf("%s\n", *ptr2);
array2[0][0]='\0';
printf("%s\n", *array2);
CPY(array1, array2);
printf("%s\n", *array2);
return 0;
}
void copy(char (*copyto)[COLSIZE], char (*copyfrom)[COLSIZE])
{
int row, col;
for(row=0; row<ROWSIZE; ++row){
for(col=0; col<COLSIZE; ++col)
copyto[row][col] = copyfrom[row][col];
}
return;
}
void CPY(char a[][COLSIZE], char b[][COLSIZE]){
int row, col;
for(row=0; row<ROWSIZE; ++row){
for(col=0; col<COLSIZE; ++col)
*(*(b+row)+col)=*(*(a+row)+col);
}
return;
}
ROWSIZE 11
Code:
abcdefghij
abcdefghij
ROWSIZE 10
Code:
abcdefghij123456789
abcdefghij123456789