so, if i understood correctly, this code:
Code:
void movefunction (char m1[], char m2[], int n){
char temp[1000];
int i;
if(((m1-m2)<0)||((m1-m2)<n)){
for (i=0;i!=n;i++){
temp[i] = m2[i];
}
for (i=0;i!=n;i++){
m1[i] = temp[i];
}
}
else {
for (i=0;i!=n;i++){
m1[i] = m2[i];
}
}
}
only doesn't work if the arrays are not of the same type? But since they are both char arrays then the pointers can be subtracted, right? So for strings this code is correct and it even works with your example (abcdefghij, it returns abcddefhij)
also with my other code:
Code:
void movefunction (char m1[], char m2[], int n){
char temp[n];
int i;
for(i=0;m2[i]!='\0';i++){
temp[i]=m2[i];
}
for (i=0;i!=n;i++){
m1[i] = temp[i];
}
}
that example is also correct here, the only problem seems to be when the source is too big (that problem doesnt exist on the previous code). Also when i compile this with gcc -ansi -Wall -pedantic -O2 testar.c it gives the following warning:
testar.c:49:5: warning: ISO C90 forbids variable length array ‘temp’ [-Wvla]
char temp[n];
^
so iguess they don't want me to use variables as the length of the string.