3

I have the following structure

typedef struct _LSHFunctionT 
{
    double *a;
    double b;
} LSHFunctionT, *PLSHFunctionT;

My question is; is there a difference between these two declarations

PLSHFunctionT myPointer1;

and

LSHFunctionT *myPointer2;

and if not, then why do people explicitly use two of them (LSHFunctionT and *PLSHFunctionT). Why not just use LSHFunctionT.

Does it go the same way for the following two declarations

PLSHFunctionT *myPointer3;

and

LSHFunctionT **myPointer3;
Roronoa Zoro
  • 1,013
  • 2
  • 15
  • 25
  • And to your question "why do people explicitly use two of them (LSHFunctionT and *PLSHFunctionT)", I think some people just find it "cleaner" to write `Pwhatever ptr` than `whatever* ptr`, because they don't like asterisks or something. ::shrug:: – Mr Lister Feb 06 '12 at 18:30

7 Answers7

2
typedef struct _LSHFunctionT 
{
    double *a;
    double b;
} LSHFunctionT, *PLSHFunctionT;

Yes, PLSHFunctionT x; is equal to LSHFunctionT* x;

And yes, PLSHFunctionT* x; is equal to LSHFunctionT** x;

The purpose of typedef is to assign new names to existing types. You can define typedef int lol; and declare variable lol i;, but compiler will consider it int anyway.

You should also check these questions:
When should I use typedef in C++?
Why should structure names have a typedef?

Hope this helps.

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
2

Yes, they are identical.

One good reason to define a pointer type is for complicated expressions. If for example you have a function that takes a reference to a pointer, which do you find easier to understand?

void foo(PLSHFunctionT & ref);

void foo(LSHFunctionT * (&ref));

I'm not even sure I got the syntax correct for the second one!

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • `void foo(LSHFunctionT * & ref)` is fine. No need to use extra-braces. – Nawaz Feb 06 '12 at 18:31
  • @Nawaz, thanks. But you see my point - it's hard to remember if it's not something you do every day. And I did have it incorrect at first but I did a quick edit. – Mark Ransom Feb 06 '12 at 18:32
  • Yup. Its easy to make mistake. One may try `void foo(LSHFunctionT & * ref)` which is a compilation error. But it is equally possible when you use `typedef`. e.g `typedef T& RT;void foo(RT * ptr)` which is compilation error. – Nawaz Feb 06 '12 at 18:36
1

The difference is in emphasis. Generally not explicitly writing the * may indicate that the PLSHFunctionT is designed to be used as a handle (without knowing/accessing structure elements ). If * is explicitly written, as in LSHFunctionT *myPointer, it might indicate an array or a structure that is to be used to access the values.

perreal
  • 94,503
  • 21
  • 155
  • 181
1

There's no difference between the two on the surface. However, with the pointer typedef there's no way to declare it as pointer-to-const, just const-pointer-to-non-const.

For example you can say

const LSHFunctionT* const_ptr; but const PLSHFunctionT const_ptr2; makes the pointer const, NOT the pointee.

Finally note that in C++ the whole thing is of questionable legality because names starting with _<capital> are reserved for the implementation, and that typedefs are almost never used in such a way.

Mark B
  • 95,107
  • 10
  • 109
  • 188
0

I seem there is no difference. There are different programming styles.

mikithskegg
  • 806
  • 6
  • 10
0

Yes. They are exactly same, even in the second case.

I personally prefer using * explicitly if I want to declare a pointer. It makes the code readable, in most cases. Usage of typedef of pointer-type reduces readability usually, though sometimes it may increases readability especially when you work with, say, Windows API.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Ah yes, the Windows API, with its PSTR, LPSTR, PCSTR, LPCSTR, LPCTSTR, LPUTSTR, LPCUTSTR etcetera. Lovely. – Mr Lister Feb 06 '12 at 18:41
0

Those declarations are the same. The pointer-typedef approach improves code readability in some cases. One could argue that this:

PLSHFunctionT calls[];

is easier to read than this:

LSHFunctionT *calls[];
Krzysztof Kozielczyk
  • 5,887
  • 37
  • 28