Re: pesky Pointers !!

From: Rich (Someone_at_somewhere.com)
Date: 12/21/04


Date: Mon, 20 Dec 2004 23:51:03 +0000 (UTC)


> '*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 ???

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.

 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

*px dereferences the pointer to get the value

&px gives the address that *px points to

hopefully I have the above right?

So then if a function expects a parameter string* s

This is saying I need a pointer to a string passed to me??

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 ??

e.g if I just called the pointer Test then it would be confusing.

Now I am completely confused again doh!!

BTW

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

I have also seen
a[10]
func(10,20)

I prefer an in between like
a[ 10 ]
func( 10 ,20 )

I use this because having the parenthesis next to the variable or
function name makes it look like it is part of it rather having the
variable part then a space then parenthesis looks kind of odd to me, I
can understand the spaces after first parenthesis and before last
parenthesis but why do some people insist on having a space before the
first parenthesis as well???

I know style is a matter of preference, but sometimes I think too many
spaces makes it more difficukt to read than no spaces.

I try to voice this with our tutor but she insists on the all spaces
style, this style I know is popular with Java programmers, but when did
it become a style in C++??

TIA



Relevant Pages

  • Re: TERPRI
    ... > I thought the whole problem with Lisp was the parenthesis and that ... declare x as array 3 of pointer to function returning pointer to array 5 of char ...
    (comp.lang.lisp)
  • int (*daytab)[13] vs int *daytab[13]
    ... is a pointer to an array of 13 integers. ... The parenthesis are necessary ... I should parenthesize it as "int ...
    (comp.lang.c)
  • Re: Function parameters: trying to pass a control
    ... Thanks for the pointer. ... and used the CALL keyword (I like the look of the parenthesis - makes ... it cleaner I think). ...
    (microsoft.public.excel.programming)
  • 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)