I had a basic clarification . Here is the code snippet I tried.
void fill(char s[])
{
strcpy(s,"temp");
}
int main()
{
char line[100];
fill(line);
printf("%s",line);
return 0;
}
As expected, the output is temp.line is a local array but it can be modified in fill and is reflected back in the caller. My understanding is ,this works because when we say fill(line) we are actually passing the address of start of an array.
Am I correct ?
Yes you are correct.
Note that this is dangerous code – it is very easy with this type of code that
filloverwrites the end of the passed-in buffer. Fill should have knowledge of the size of the buffer and should not write past the end of that size.For example, if instead of writing in “temp” you wrote in the contents of War and Peace, the program would happily overwrite lots and lots of other memory locations not associated with the variable
line.