Re: pesky Pointers !!
From: Jonathan Mcdougall (jonathanmcdougall_at_DELyahoo.ca)
Date: 12/21/04
- Next message: Andrey Tarasevich: "Re: Meaning of this declaration?"
- Previous message: Jonathan Mcdougall: "Re: Where to find code."
- In reply to: Rich: "Re: pesky Pointers !!"
- Next in thread: Edd: "Re: pesky Pointers !!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 20 Dec 2004 19:22:54 -0500
Rich wrote:
>>'*pTest' is type 'string'
>>Your function needs an argument of type 'string*' ('pointer to string').
>>'pTest' is type 'string*', and contains the address of 'sTest'.
>>
>>Write:
>>
>>StringTest(pTest);
>>
>
> Hi Thanks that worked, but I still dont understand why it worked, sorry :(
>
> pTest looks just like a normal variable, which I thought was what a a
> reference was used for ???
I don't understand that.
> A reference as a parameter in a definition allows the programmer to pass
> a reference without really knowing, i.e. no need to do &x or *x just x
> and the function takes it as a reference instead of a copy.
Yes but you are not using references, you are using pointers. See my
other post for an example with references.
> From what I have kearnt about pointers and syntax using pointers it is
> fairly straightforward you read from right to left e.g
>
> int* px // px is a pointer to an int
Yes it is.
> *px dereferences the pointer to get the value
Yes.
> &px gives the address that *px points to
No. Just 'px' itself yields the address. &px returns the address of
the pointer itself, not what's pointed at. Taking arbitrary values:
int main()
{
int x = 10; // x contains 10
std::cout << &x; // print 0x123456, which is the address of x
int *px = &x; // px now contains 0x123456
std::cout << px; // also prints 0x123456, which is the address of x
std::cout << *px; // prints 10
std::cout << &px; // prints 0x789abc, which is the address of px
}
> hopefully I have the above right?
Almost :)
> So I thought I needed to pass the pointer as an argument thus requiring
> either the use of the * or & before the pointer to show that it is a
> pointer I am passing.
> by passing pTest just like that it looks like a normal variable name ??
You do no understand correctly the meaning of these operators. Don't
you get some books at the school you are studying at?
> I notice a couple of different C++ styles one which out tutor insists we
> use but I dislike
>
> she insists on this type of style for parenthesis
>
> a [ 10 ] // array
> func ( 10, 20 ) // function call
That's quite ugly.
> I have also seen
> a[10]
> func(10,20)
That's ok.
> I prefer an in between like
> a[ 10 ]
> func( 10 ,20 )
That's also ok. the style does not matter as long as it is readable and
that you stick with it.
> I know style is a matter of preference, but sometimes I think too many
> spaces makes it more difficukt to read than no spaces.
Yes it does.
Jonathan
- Next message: Andrey Tarasevich: "Re: Meaning of this declaration?"
- Previous message: Jonathan Mcdougall: "Re: Where to find code."
- In reply to: Rich: "Re: pesky Pointers !!"
- Next in thread: Edd: "Re: pesky Pointers !!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|