Solved

Structure setup with typedef and pointers

Posted on 2011-02-23
9
360 Views
Last Modified: 2012-05-11
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
Comment
Question by:James_h1023
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 63 total points
ID: 34967707
As far as I understand it the typedef statement is really for convenience to help make code more readable.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 62 total points
ID: 34967771
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 34968498
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 34968850
@ssnkumar : I thought that's what I just said ...
0
Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

 
LVL 8

Expert Comment

by:ssnkumar
ID: 34968969
@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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34969006
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 34969435
Which is why I emphasised LESS in my post, to show that I disagreed with what you said ;)
0
 
LVL 4

Author Comment

by:James_h1023
ID: 34972309
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 34972588
>> and I did not know that I was dealing with pointers until I found it typedef'ed as a pointer.

:) Indeed.
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I recover data from my iphone 3g 4 54
Why this code doesn't work? 8 97
Where is my core dump file in Ubuntu? 12 442
How to learn Linux? 10 43
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now