easy pointer question...

M

Michael

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
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
V

Vladimir Oka

Michael said:
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...
 
W

Walter Roberson

Michael said:
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 "; .
 
K

Kenneth Brody

Vladimir said:
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:[email protected]>
 
L

lovecreatesbeauty

Michael said:
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';
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top