Thanks for resolving some issues I was having.  I could fund a small 3rd
world army with what I've spent on books, but sometimes just need to ask
that one question of someone "in the know".

Mitch

> -----Original Message-----
> From: Greg Winton [SMTP:[EMAIL PROTECTED]]
> Sent: Wednesday, March 10, 1999 2:19 PM
> To:   [EMAIL PROTECTED]
> Subject:      RE: Variable storage question
> 
> At 01:26 PM 3/10/99 -0500, you wrote:
> >If I understand correctly, char s1[]= "Hello"; puts that array of
> characters
> >(plus the NULL) on the stack. Could I then  execute something like
> >printf("%s", s1) and have it print Hello?  If true, when that printf
> runs,
> >how does it know which bytes to pull off the stack.  I might have added
> >additional things to the stack, so how does it know where to find it if
> >there is no pointer to it? If I could understand that, I'd really be
> getting
> >somewhere.  Thanks.
> 
> Mitch,
> 
> This gets into the nature of 'C' arrays; 'C' arrays *are* pointers.
> 
> Let's say we have the following declarations:
> 
>    char s1[] = "Hello";
>    char* sP = s1;
> 
> sP and s1 are both pointers to the same memory (the 'H').  We can mostly
> use s1 and sP as equivalents:
> 
>    (*s1) = 'G'; s1 [0] = 'G'; (*sP) = 'G'; sP [0] = 'G';
> 
> All these expressions do the exact same thing - they change the first
> letter of the string to 'G'.  In the same way, we can say:
> 
>    *(s1+1) = 'o'; s[1] = 'o'; *(sP+1) = 'o'; sP [1] = '0';  
> 
> Again, here s1 and sP can both be used as "arrays" or "pointers".  We can
> pass s1 to *any* function that takes a string pointer:
> 
>    StrCopy (s1, "Dog");
>    StrCopy (sP, "Cat");
> 
> We can even legally do really bad things:
> 
>    *(s1+20) = '\0';  // This will probably crash the program at some
> point.
>    StrCopy (s1, "Goodbye");  // So will this
> 
> The only thing we can't do with s1 that we can do with sP is to change
> what
> it points to:
> 
>    char s2 = "Goodbye";
> 
>    sP = s2;           // this is legal
>    s1 = s2;           // this is not - the compiler will object.
> 
> Effectively, what s1 really represents is a constant pointer to
> non-constant data.  The fact that the data is not constant is what causes
> it to be allocated on the stack - the application *must* be able to change
> it.  It is the "const-ness" of the data you point to that determines where
> it is stored.
> 
> Hope this helps.
> 
> Regards,
> Greg
> 
> 
> 
> Greg Winton
> Bachmann Software and Services, LLC
> mailto:[EMAIL PROTECTED]
> http://www.bachmannsoftware.com
> Software Development for Handheld & Mobile Computing, Windows and the
> Internet
> Home of Bachmann Print Manager, the only graphical printing solution for
> the Palm Computing Platform
> 

Reply via email to