Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > easy pointer question...

Reply
Thread Tools

easy pointer question...

 
 
Michael
Guest
Posts: n/a
 
      06-12-2006
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


 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      06-12-2006
-----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-----
 
Reply With Quote
 
 
 
 
Vladimir Oka
Guest
Posts: n/a
 
      06-12-2006

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

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      06-12-2006
In article <448d6668$0$7222$>,
Michael <> 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)
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      06-12-2006
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: <private.php?do=newpm&u=>


 
Reply With Quote
 
lovecreatesbeauty
Guest
Posts: n/a
 
      06-13-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments