In article <44**********************@per-qv1-newsreader-01.iinet.net.au>,
Michael <mi*********@yahoo.com> wrote:
I have the following in main:
char output[100];
function(output);
void function(char *out){
.......lots of good stuff....
out = "result is ";
out[strlen("result is ") + 1] = '\0';
}
Why does the assignment of 'out' work, but the out[] not work (access
violation at run time)?
Because the C standards say that string literals may be read-only.
In the statement out = "result is "; you are copying the -pointer-
to the string literal, but in the next statement you attempt to modify
what is stored at the pointer, and that's not allowed for string literals.
This is probably all described in the C faq.
Note by the way that when you call function() you are passing in
the address of the array output[], and that what you are really
passing is a -copy- of the address. When you then do the
out = "result is "; then you are overwriting the copy of the address,
and you are NOT affecting the array that is passed in. The array
output[] will NOT have any characters set in it by your statement
out = "result is "; .
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)