Possible Duplicate:
Is array name a pointer in C?
Array Type – Rules for assignment/use as function parameter
This is the code that i wrote for an exercise in K&R C. The task is pretty simple, to replace ‘\t’ with \ and t and not the tab character
the following is the code
char* escape(char s[], char t[]){
int i = 0, j = 0;
for(i = 0; t[i] != '\0'; i++){
switch(t[i]){
case '\t':{
s[j++] = '\\';
s[j++] = 't';
break;
}
default:{
s[j++] = t[i];
break;
}
}
}
s[j] = t[i];
return s;
}
int main(){
char t[10] = "what \t is";
char s[50];
s = escape(s,t);
printf("%s",s);
return 0;
}
It returns an error saying inappropriate type assignment betweenn char[50] and char* but isn’t the name of the array supposed to be the pointer to the first element?
In C arrays aren’t writable lvalues. You can’t assign to them. In your code you don’t actually need to return anything from the function since it changes
sin place. But if you really want to:This means you will later have to free it etc. In short, don’t return anything from the function and it’s going to be ok.
That’s an over-simplification. Thing is arrays typically decay into pointers to the first element in certain contexts.