Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

Structure setup with typedef and pointers

Hi *,

Currently I define my structures as in the first attached example.

What are the advantages of using the second example? I understand that it may be for convenience so that then when passing this structure through functions, one only need put name, instead of *name and &name.

Cheers,
James
typedef struct {
    int x;
    int y[10][10];
} name;

typedef struct {
    int x;
    int y[10][10];
} name_t;
typedef name_t *name;

Open in new window

0
James_h1023
Asked:
James_h1023
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
AndyAinscowCommented:
As far as I understand it the typedef statement is really for convenience to help make code more readable.
0
 
Infinity08Commented:
Actually, I would say that the second makes it LESS readable. Especially if the point is to save typing one character (the * character).

Seeing the * character in code is very important to know that something is a pointer, and not a full object. Obscuring that information (for sake of "convenience") is sure to lead to confusion.

Some people typedef a pointer-to-struct with a name like NamePtr (ie. with "Ptr" in there) to indicate that it's a pointer, but then I fail to see the advantage - you now have to type three characters (Ptr) instead of one (*), and have not made things more readable (in fact, you've made it less readable).
0
 
ssnkumarCommented:
The only thing that you achieve from the 2nd typedef is, the usage of pointer (that is * character) will be hidden!
But, I don't see that as a great help. Actually that hinders the code readability and also gives problem while debugging the code.
The problem for debugging might occur in this scenario.
Suppose you have declared a variable:
 name n;
Now while debugging, you don't know that n is a pointer.
If you have 1000 lines of code and there are many such variables, then it will become very difficult to know which is pointer, which is not a pointer etc!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Infinity08Commented:
@ssnkumar : I thought that's what I just said ...
0
 
ssnkumarCommented:
@Infinity08: The main point that I am trying to stress is about the problem faced during debugging.
May be the point was implicit in your comment about readability.
I am trying to make it more explicit.
0
 
AndyAinscowCommented:
Ho hum.  To repeat myself with an emphasis put in.  (Maybe I should have done so in the original comment)

As far as I understand it the typedef statement is really for convenience to help make code more readable.
0
 
Infinity08Commented:
Which is why I emphasised LESS in my post, to show that I disagreed with what you said ;)
0
 
James_h1023Author Commented:
Many thanks for your comments.

I think I have to agree that it can add to the confusion. I found it in some libraries written by someone else, and I did not know that I was dealing with pointers until I found it typedef'ed as a pointer.

Cheers,
James
0
 
Infinity08Commented:
>> and I did not know that I was dealing with pointers until I found it typedef'ed as a pointer.

:) Indeed.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now