On May 6, 3:52=A0am, mdh <m...@[EMAIL PROTECTED]
> wrote:
> foo( char *s){
>
> char *t =3D s;
>
> .....
>
> s++; =A0<<--- a few arbitrary times
>
> }
>
> Now...my question is this.
>
> Is "t" independent of s, in the sense of:
>
> 1) Will it still point to the beginning of s, after s++ ?
>
> 2) I guess related to 1, if I now evaluate *t, will it be the first
> character in s?
You have to think of things in more concrete terms. There's nothing
magical about a pointer. A pointer stores data, just like any other
kind of variable.
When you want to store data, you have to pick the most suitable data
type.
* If you want to store an integer, use "int"
* If you want to store a floating point number, use "double".
* If you want to store a memory address, use a pointer.
A memory address is a very concrete thing. It's basically a number. It
could 1756, or 345, or 6852.
In your function, let's say that the memory address passed to it is
6852, so the value of s is 6852.
Now you make an object called t, and you give it the value of s. So
now t's value is 6852.
Now you increment s, so s becomes 6853. Then you increment it again to
6854. And again to 6855.
You're changing s, not t. t still keeps the value of 6852.
This is no different from:
void Func(int i)
{
int j =3D i;
++i;
++i;
++i;
/* j retains the original value of i */
}
Just remember, a pointer holds a memory address, nothing magical.


|