Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > typedef and pointers.

Reply
Thread Tools

typedef and pointers.

 
 
srikar2097
Guest
Posts: n/a
 
      12-12-2008
I came across this this piece of code while trying to understand
someone elses code.

#include <stdio.h>

int main()
{
typedef long LG, *LGP;
extern LGP lptr;
return 0;
}

Here LG is a typedef for "long" and LGP is a pointer to a "long". My
doubt here is what is the purpose of having a pointer to a "long" i.e.
a data type. it is not a variable right? I know technically this is
correct. But my doubt is more functional (use of this) than syntactic.

Thanks...
 
Reply With Quote
 
 
 
 
viza
Guest
Posts: n/a
 
      12-12-2008
On Fri, 12 Dec 2008 05:41:23 -0800, srikar2097 wrote:

> I came across this this piece of code while trying to understand someone
> elses code.
>
> #include <stdio.h>
>
> int main()
> {
> typedef long LG, *LGP;
> extern LGP lptr;
> return 0;
> }
>
> Here LG is a typedef for "long" and LGP is a pointer to a "long". My
> doubt here is what is the purpose of having a pointer to a "long" i.e. a
> data type. it is not a variable right?


LG and LGP are types, lptr is a variable. It has type "pointer to
long". It can be used to point to a variable of type long":

LG foo= 42;
lptr= & foo;

HTH
viza
 
Reply With Quote
 
 
 
 
srikar2097
Guest
Posts: n/a
 
      12-12-2008
I get it now, so lptr becomes a pointer of type long. Is that it?
But why would anyone want to do it this way? Does it have any
advantage?

To change it a bit -

long foo = 42;
long *lptr = &foo;

doesn't this suffice?



On Dec 12, 6:51*pm, viza <tom.v...@gm-il.com.obviouschange.invalid>
wrote:
> On Fri, 12 Dec 2008 05:41:23 -0800, srikar2097 wrote:
> > I came across this this piece of code while trying to understand someone
> > elses code.

>
> > #include <stdio.h>

>
> > int main()
> > {
> > * * typedef long LG, *LGP;
> > * * extern LGP lptr;
> > return 0;
> > }

>
> > Here LG is a typedef for "long" and LGP is a pointer to a "long". My
> > doubt here is what is the purpose of having a pointer to a "long" i.e. a
> > data type. it is not a variable right?

>
> LG and LGP are types, lptr is a variable. *It has type "pointer to
> long". *It can be used to point to a variable of type long":
>
> * LG foo= 42;
> * lptr= & foo;
>
> HTH
> viza


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-12-2008
[Don't top post.]

srikar2097 <> writes:
> On Dec 12, 6:51Â*pm, viza <tom.v...@gm-il.com.obviouschange.invalid>
> wrote:
>> On Fri, 12 Dec 2008 05:41:23 -0800, srikar2097 wrote:

<snip>
>> > int main()
>> > {
>> > Â* Â* typedef long LG, *LGP;
>> > Â* Â* extern LGP lptr;
>> > return 0;
>> > }

>>
>> > Here LG is a typedef for "long" and LGP is a pointer to a "long". My
>> > doubt here is what is the purpose of having a pointer to a "long" i.e. a
>> > data type. it is not a variable right?

>>
>> LG and LGP are types, lptr is a variable. Â*It has type "pointer to
>> long". Â*It can be used to point to a variable of type long":
>>
>> Â* LG foo= 42;
>> Â* lptr= & foo;

>
> I get it now, so lptr becomes a pointer of type long. Is that it?
> But why would anyone want to do it this way? Does it have any
> advantage?


If you mean "why use typedefs for such simple types?" then the answer
is probably just bad programming style. Sometimes you see code that
can be changed to work with some configurable integer type (int or
long int or long long int for example) but then I'd expect a better
name:

typedef long long int integer_type; /* Change this is you need to */

An argument can be made that hiding the fact that a type is a pointer
by using a typedef is always wrong. Some people would permit it if
the type were being used as an opaque type:

typedef struct network_interface *net_interface;

> To change it a bit -
>
> long foo = 42;
> long *lptr = &foo;
>
> doesn't this suffice?


Yes, that is quite enough.

--
Ben.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-12-2008
srikar2097 wrote:
> I get it now, so lptr becomes a pointer of type long. Is that it?
> But why would anyone want to do it this way? Does it have any
> advantage?
>
> To change it a bit -
>
> long foo = 42;
> long *lptr = &foo;
>
> doesn't this suffice?


Sure, but the people who use this approach consider it convenient to be
able to write LGP rather than long*. However, if you consider the need
to use the shift key, most of the convenience argument disappears. A
more important problem is that this typedef hides the pointer nature of
the variable, which is considered by many people to be dangerous. I
wouldn't use this typedef.
 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      12-12-2008
On Dec 12, 6:50*am, James Kuyper <jameskuy...@verizon.net> wrote:
> srikar2097 wrote:
> > I get it now, so lptr becomes a pointer of type long. Is that it?
> > But why would anyone want to do it this way? Does it have any
> > advantage?

>
> > To change it a bit -

>
> > long foo = 42;
> > long *lptr = &foo;

>
> > doesn't this suffice?

>
> Sure, but the people who use this approach consider it convenient to be
> able to write LGP rather than long*. However, if you consider the need
> to use the shift key, most of the convenience argument disappears. A
> more important problem is that this typedef hides the pointer nature of
> the variable, which is considered by many people to be dangerous. I
> wouldn't use this typedef.


maybe the program needs different types under different circumstances
or on different platforms. Using a typedef then makes it simple to
change between types, changing only one line instead of everywhere
that type is used.

#ifdef CIRCUMSTANCE_ONE
typedef long LG, *LGP;
#else
typedef long long LG, *LGP;
#endif

--
Fred K
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      12-12-2008
srikar2097 <> wrote:

[ Please do not top-post. Corrected. ]

> On Dec 12, 6:51=A0pm, viza <tom.v...@gm-il.com.obviouschange.invalid>
> > On Fri, 12 Dec 2008 05:41:23 -0800, srikar2097 wrote:


> > > int main()
> > > {
> > > typedef long LG, *LGP;
> > > extern LGP lptr;
> > > return 0;
> > > }

> >
> > > Here LG is a typedef for "long" and LGP is a pointer to a "long". My
> > > doubt here is what is the purpose of having a pointer to a "long" i.e. =
> > > data type. it is not a variable right?

> >
> > LG and LGP are types, lptr is a variable. =A0It has type "pointer to
> > long". =A0It can be used to point to a variable of type long":


> I get it now, so lptr becomes a pointer of type long. Is that it?
> But why would anyone want to do it this way? Does it have any
> advantage?


No. Basically, the person who wrote that code had probably been reading
a bit too much Microsoft system code. Their headers abound with such
silliness. It's the habit of someone who has heard of abstract data
types and the separation of interface and implementation, but has not
the faintest idea what he is actually doing, let alone what he's
supposed to be doing.

Richard
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      12-12-2008
On Dec 12, 8:02*am, srikar2097 <srikar2...@gmail.com> wrote:
> I get it now, so lptr becomes a pointer of type long. Is that it?
> But why would anyone want to do it this way? Does it have any
> advantage?
>
> To change it a bit -
>
> long foo = 42;
> long *lptr = &foo;
>
> doesn't this suffice?
>


IME, people who use such a typedef are the kind of people who get bit
by the

long* p1, p2;

mistake a lot (thinking that p2 is also being declared a pointer, even
though it isn't).

Personally, I've found that hiding the pointerness of a type behind a
typedef causes more problems than it solves.
 
Reply With Quote
 
jameskuyper
Guest
Posts: n/a
 
      12-12-2008
Fred wrote:
....
> maybe the program needs different types under different circumstances
> or on different platforms. Using a typedef then makes it simple to
> change between types, changing only one line instead of everywhere
> that type is used.
>
> #ifdef CIRCUMSTANCE_ONE
> typedef long LG, *LGP;
> #else
> typedef long long LG, *LGP;
> #endif


With a different name, that might be plausible, and perfectly
legitimate use of a typedef. However, the name looks like an
abbreviation for LonG Pointer, which doesn't sound like something that
would be used in that way.
 
Reply With Quote
 
srikar2097
Guest
Posts: n/a
 
      12-13-2008
Thanks you guys!! I am learning a lot here.

Srikar
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
function template overloading and typedef in GCC Arkadiy Vertleyb C++ 5 09-27-2003 12:28 PM
A question about typedef and classes and templates.. JustSomeGuy C++ 1 09-20-2003 04:33 AM
typename, typedef, and resolution Gina Yarmel C++ 4 08-13-2003 04:06 PM
typedef and #ifdef F. Edward Boas C++ 1 08-11-2003 05:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57