Re: pesky Pointers !!

From: Jonathan Mcdougall (jonathanmcdougall_at_DELyahoo.ca)
Date: 12/21/04


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



Relevant Pages

  • Re: pesky Pointers !!
    ... > and the function takes it as a reference instead of a copy. ... function may access the string passed directly, ... > *px dereferences the pointer to get the value ... If pTest is a pointer-to-string, *pTest is the string it points to ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Question on LSP
    ... Sure, the developer must keep track of which type has been assigned to each pointer to manage complexity in creating the design, which is why the 'T' is in T*. ... Subtyping is a relation which does not ... Those object types are completely orthogonal to the semantics of being an object reference. ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)
  • Re: Question on LSP
    ... Because construction paradigms have different goals. ... But that has nothing to do with what a pointer type ... But that does not mean that the semantics of an object reference is ... aggregate references, is the aggregate of referenced objects or target ...
    (comp.object)
  • Re: Question on LSP
    ... equivalent addresses in the same context. ... Note that the language allows us to use a name like 'T' on the reference as a mnemonic so that the developer can keep track of what is happening with the indirection. ... Modern languages have proper pointer types. ...
    (comp.object)
  • Re: Nothing Keyword Destories Objects rather than just resetting the variable to an empty variable
    ... there is no difference between using ByVal or ByRef. ... as the C1 pointer was the sole reference to the class object. ... Yes, an object variable is a pointer to an object, but that is the only ... the keyword "NOTHING" should only destroy the ...
    (microsoft.public.excel.programming)