Velocity Reviews > Pointers

# Pointers

sake
Guest
Posts: n/a

 01-01-1970
Ian Collins <(E-Mail Removed)> Wrote in message:
> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>> I read the entire FAQ for pointers, and I'm still stuck . What I
>> (think) I understand is that when you declare pointer such as:
>>
>> int *ptr;
>>
>> Then that points the address of a variable and not the value inside
>> the address. What I'm not understanding is, what do these do:

>
> "int*" is a type which is used to store the address of an int. Just
> declaring one does not assign a value to it.
>
> int n 42;
> int *ptr = &n;
>
> declares ptr as a pointer to int and initialises it with the address of n.
>
> printf( "%d %d\n", n, *ptr );
>

hey
this doesn't work in my enviorment.
*ptr is actually the value pointed to and ptr is the pointer.
you verify this implicitly by printing the value with *ptr.

best regards
sake

--

http://www.piaohong.tk/newsgroup

sake
Guest
Posts: n/a

 01-01-1970
Keith Thompson <(E-Mail Removed)> Wrote in message:
> sake <(E-Mail Removed)> writes:
>> Ian Collins <(E-Mail Removed)> Wrote in message:
>>> (E-Mail Removed) wrote:
>>>> I read the entire FAQ for pointers, and I'm still stuck . What I
>>>> (think) I understand is that when you declare pointer such as:
>>>>
>>>> int *ptr;
>>>>
>>>> Then that points the address of a variable and not the value inside
>>>> the address. What I'm not understanding is, what do these do:
>>>
>>> "int*" is a type which is used to store the address of an int. Just
>>> declaring one does not assign a value to it.
>>>
>>> int n 42;
>>> int *ptr = &n;
>>>
>>> declares ptr as a pointer to int and initialises it with the address of n.
>>>
>>> printf( "%d %d\n", n, *ptr );

>>
>> hey
>> this doesn't work in my enviorment.

>
> "doesn't work" is one of the less useful problem descriptions. *How*
> doesn't it work? What behavior did you expect, what did you see, and
> how do they differ?
>
>> *ptr is actually the value pointed to and ptr is the pointer.
>> you verify this implicitly by printing the value with *ptr.

>
> Of course.
>
> Ian did omit the "=" for the initializer, which would cause a
> compile-time error; is that what you mean by "doesn't work"? If not,
> try to compile and execute this program:
>
> #include <stdio.h>
> int main(void) {
> int n = 42;
> int *ptr = &n;
> printf( "%d %d\n", n, *ptr );
> return 0;
> }
>
> The output should be a single line of text, "42 42". If you get
> anything else (including nothing), then we can discuss what's going
> wrong.
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
> Working, but not speaking, for JetHead Development, Inc.
> "We must do something. This is something. Therefore, we must do this."
> -- Antony Jay and Jonathan Lynn, "Yes Minister"
>

yes, my mistake, sorry.

I transcripted the code like that

....
int *val, no=20;
*val=&no ;
....

which didn't work. next time I'll double ckeck
best regards
sake
--

http://www.piaohong.tk/newsgroup

j.wagner1024@gmail.com
Guest
Posts: n/a

 03-13-2013
Hi, I have started programming in C recently and started learning about pointers. Insofar, all the material has been easy to digest and understand. But pointers are really giving me a hard time! I've read quite a few articlesand some pdfs on pointers, but I'm still having trouble. If someone could point me to (no pun intended) some good tutorials or try to explain it, I'dbe indebted! Thanks!

Eric Sosman
Guest
Posts: n/a

 03-13-2013
On 3/12/2013 9:57 PM, (E-Mail Removed) wrote:
> Hi, I have started programming in C recently and started learning about pointers. Insofar, all the material has been easy to digest and understand. But pointers are really giving me a hard time! I've read quite a few articles and some pdfs on pointers, but I'm still having trouble. If someone could point me to (no pun intended) some good tutorials or try to explain it, I'd be indebted! Thanks!

Take a look at Section 4 on the comp.lang.c Frequently Asked
Questions (FAQ) page at <http://www.c-faq.com/ptrs/index.html>, and
post again if you're still puzzled. (The FAQ isn't a tutorial on
pointers, but it touches on a lot of their uses. You may find the

--
Eric Sosman
(E-Mail Removed)d

j.wagner1024@gmail.com
Guest
Posts: n/a

 03-13-2013
Will do, thanks!

j.wagner1024@gmail.com
Guest
Posts: n/a

 03-13-2013
I read the entire FAQ for pointers, and I'm still stuck . What I (think) I understand is that when you declare pointer such as:

int *ptr;

Then that points the address of a variable and not the value inside the address. What I'm not understanding is, what do these do:

1. &ptr
2. null pointers
3.ptr++ vs ++ptr vs (*ptr)++ etc...
4. void *ptr

Ian Collins
Guest
Posts: n/a

 03-13-2013
(E-Mail Removed) wrote:
> I read the entire FAQ for pointers, and I'm still stuck . What I
> (think) I understand is that when you declare pointer such as:
>
> int *ptr;
>
> Then that points the address of a variable and not the value inside
> the address. What I'm not understanding is, what do these do:

"int*" is a type which is used to store the address of an int. Just
declaring one does not assign a value to it.

int n 42;
int *ptr = &n;

declares ptr as a pointer to int and initialises it with the address of n.

printf( "%d %d\n", n, *ptr );

will print "42 42"

> 1. &ptr

This is the address of ptr.

> 2. null pointers

The null pointer is a pointer with a value of 0. Zero is nearly always
an invalid address, so it gets used as an error value for most
operations that return a pointer.

> 3.ptr++ vs ++ptr vs (*ptr)++ etc...

ptr++ and ++ptr increment the value of ptr be the size of the type it is
pointer to "move on to the next item" if p is pointer to something in an
array. (*ptr)++ increments the value pointed to by ptr.

> 4. void *ptr

"void*" is C's generic pointer type.

--
Ian Collins

j.wagner1024@gmail.com
Guest
Posts: n/a

 03-13-2013
That helps a bit, especially the first example, thanks.

Shao Miller
Guest
Posts: n/a

 03-13-2013
On 3/12/2013 22:40, (E-Mail Removed) wrote:
> I read the entire FAQ for pointers, and I'm still stuck . What I (think) I understand is that when you declare pointer such as:
>
> int *ptr;
>
> Then that points the address of a variable and not the value inside the address.

Roughly speaking:

A pointer is an object. An object contains a value. So: A pointer
object contains a pointer value.

> What I'm not understanding is, what do these do:
>
> 1. &ptr

'&identifier' gives a pointer value, which points to the identified
object. Since 'ptr' is a pointer object, '&ptr' points to a pointer
object; a pointer to a pointer to an 'int'.

> 2. null pointers

A null pointer value means "points to no object nor function". A
pointer object can hold a null pointer value.

> 3.ptr++ vs ++ptr vs (*ptr)++ etc...

The first two increment a pointer value, which means "Increment 'ptr' to
point to the next 'int' object in the array of 'int' objects."
(Assuming there _is_ an 'int' array, and that you've pointed to one of
its elements.)

> 4. void *ptr
>

A 'void *' value points to any object, which means that since different
objects have different sizes, you cannot increment a 'void *'; there's
no way to know how to advance to "the next object" if you do not know
the size of each object.

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter

j.wagner1024@gmail.com
Guest
Posts: n/a

 03-13-2013
Could someone recommend some material that I could study?