By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

easy pointer question...

P: n/a
Hi all,

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)?

Thanks for your help

Michael
Jun 12 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

Michael wrote:
Hi all,

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)?


This is a FAQ (1.32 in fact):

http://c-faq.com/decl/strlitinit.html

C FAQ should have been your first stop anyway...

Jun 12 '06 #2

P: n/a
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)
Jun 12 '06 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Michael wrote:
Hi all,

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)?


Think of what your assignment statement does. Hint: it modifies
something. What sort of something does it modify?

- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEjWb7agVFX4UWr64RAk5kAJ9/cmAPUaFL+64clxYTrCYBZ7xZlQCeKRqo
cVQCjYwDrv/zckdca6Mx3oM=
=NAXf
-----END PGP SIGNATURE-----
Jun 12 '06 #4

P: n/a
Vladimir Oka wrote:

Michael wrote:

[...]
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)?


This is a FAQ (1.32 in fact):

http://c-faq.com/decl/strlitinit.html

C FAQ should have been your first stop anyway...


Not to mention the fact that out is pointing to an 11-character array,
and he's trying to modify the 12th character.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Jun 12 '06 #5

P: n/a

Michael wrote:
char output[100];
function(output);

void function(char *out){
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)?


Both may fail.

void function(char *out)
{
/* It fails if `out' receives an array */
out = "result is ";

/* It may fail if `out' receives an argument of type char *. It's
undefined. */
out[strlen("result is ") + 1] = '\0';
}
--
lovecreatesbeauty

Jun 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.