Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Pointer que...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 16 of 18 Topic 26080 of 26972
Post > Topic >>

Re: Pointer question...last for the day? :-)

by =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= <toe@[EMAIL PROTECTED] > May 6, 2008 at 07:29 AM

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.
 




 18 Posts in Topic:
Pointer question...last for the day? :-)
mdh <mdeh@[EMAIL PROTE  2008-05-05 19:52:37 
Re: Pointer question...last for the day? :-)
"Dann Corbit" &  2008-05-05 19:59:26 
Re: Pointer question...last for the day? :-)
Robert Gamble <rgamble  2008-05-05 20:02:07 
Re: Pointer question...last for the day? :-)
CBFalconer <cbfalconer  2008-05-05 23:11:41 
Re: Pointer question...last for the day? :-)
mdh <mdeh@[EMAIL PROTE  2008-05-05 20:06:09 
Re: Pointer question...last for the day? :-)
"Dann Corbit" &  2008-05-05 20:17:38 
Re: Pointer question...last for the day? :-)
rlb@[EMAIL PROTECTED] (R  2008-05-07 08:03:07 
Re: Pointer question...last for the day? :-)
Robert Gamble <rgamble  2008-05-05 20:26:37 
Re: Pointer question...last for the day? :-)
mdh <mdeh@[EMAIL PROTE  2008-05-05 20:31:51 
Re: Pointer question...last for the day? :-)
mdh <mdeh@[EMAIL PROTE  2008-05-05 20:40:28 
Re: Pointer question...last for the day? :-)
Peter Nilsson <airia@[  2008-05-05 21:18:54 
Re: Pointer question...last for the day? :-)
James Dow Allen <jdall  2008-05-05 23:48:32 
Re: Pointer question...last for the day? :-)
Rishi <postrishi@[EMAI  2008-05-06 06:50:30 
Re: Pointer question...last for the day? :-)
roberson@[EMAIL PROTECTED  2008-05-06 14:32:29 
Re: Pointer question...last for the day? :-)
Lowell Gilbert <lgusen  2008-05-06 11:47:01 
Re: Pointer question...last for the day? :-)
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-06 07:29:11 
Re: Pointer question...last for the day? :-)
Keith Thompson <kst-u@  2008-05-06 11:48:10 
Re: Pointer question...last for the day? :-)
=?ISO-8859-1?Q?Tom=E1s_=D  2008-05-06 07:39:42 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:31:41 CDT 2008.