473,503 Members | 12,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

easy pointer question...

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
5 1539

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
4946
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
20
6524
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
2
1205
by: Bonj | last post by:
I have got the starting bits of a game that I am trying to write. I did start to write it in VC++6, but found the basic windows GDI to be of general poor performance for animation. So I decided to...
0
1278
by: Alex | last post by:
Hi, an LPCTSTR is a long pointer to a const string. This is a windows typicall variable type like UINT, DWORD and so on. You can use it the same way like you would use standard variable types...
3
2137
by: Michael | last post by:
Hi, I have had a look in the FAQ but cannot see anything related, of course that doesn't mean it's not there. In any case: I have the following: void function(){ struct tm *time2;
17
2698
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
6
2277
by: Martin Bootsma | last post by:
I have a C question, which looks very easy, but no one here seems to know an easy answer. I have a function "powell" (from Numerical Recipes) which takes an argument of the type "double...
9
3185
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had...
2
2464
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
0
7212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.