Jump to content

Talk:Typedef

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Decltype (talk | contribs) at 10:00, 29 April 2010 (banners). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

WikiProject iconComputing Stub‑class Low‑importance
WikiProject iconThis article is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
StubThis article has been rated as Stub-class on Wikipedia's content assessment scale.
LowThis article has been rated as Low-importance on the project's importance scale.
WikiProject iconC/C++ Unassessed High‑importance
WikiProject iconThis article is within the scope of WikiProject C/C++, a collaborative effort to improve the coverage of C and C++ topics on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
???This article has not yet received a rating on Wikipedia's content assessment scale.
HighThis article has been rated as High-importance on the importance scale.

typedef uint32 A;

Can anyone tell me what typedef uint32 A; would do  ?


It would create a new type named 'A' that would essentially just be an alias for uint32. You could then create variables like this: 'A newvariables = 3;' Jaddle 17:06, 14 May 2007 (UTC)[reply]

= 3 --193.136.128.14 13:12, 17 May 2007 (UTC)[reply]


It would be nice to have an explanation of the syntax of recursive typedefs

For example, in C:

int main() {

typedef struct { int key; struct MyType* next; } MyType;

MyType a; MyType b;

MyType* aptr; MyType* bptr;

aptr = &a; bptr = &b;

aptr->key = 3; aptr->next = bptr;

}

The word "struct" is required before the phrase "mytype* next;" The GNU C compiler (3.4.6) warns that "aptr->next = bptr;" is an assignment of incompatible pointer types. —Preceding unsigned comment added by 155.148.36.138 (talk) 14:31, 6 September 2007 (UTC)[reply]


Typedefs are confusing

Note that the example given in the page is an excellent example of everything that can go wrong with typedefs. First, your typedef is usually in a separate file, so all the coder will see is:

apple coxes;

Now, ok, what can I do with coxes that are of type apple? Eat them? No I can increment them:

coxes += 1

So what exactly happens when you increment an apple? Especially in C++ with type overloading? All you've done is obscure the type of the variable. The data type should specify the variables _data_ _type_. Not describe it. That's what the name is for. The following code would be much, much clearer:

int coxe_apple_cnt = 0;

Now we have a variable that is a count of the number of coxe apples (coxe is a type of apple, I presume). It's type is int. Now we have a much clearer notion of what:

coxe_apple_cnt++;

Does.

So what's good about typedefs again? — Preceding unsigned comment added by 12.155.58.181 (talkcontribs)

Wikipedia is not a programming tutorial, but just for the record, you're doing it wrong. If you want a type to hold an apple, make it behave like an apple — that's the problem object-oriented programming and C++'s operator overloading are trying to solve. C's typedefs are trying to solve a different problem: I've got a type that holds, let's say, a count of how many apples I have — namely, unsigned int. (Assuming I can't have negative apples, and I don't much care about integer overflow.) But I also want to use unsigned int to hold some bitmasks in the same program, and I want to make sure that I don't accidentally add one of those bitmasks to my apple count, or pass one of my apple counts as a parameter to a function expecting a bitmask. The solution (that's relevant to this article) is
typedef unsigned int AppleCount_t;
typedef unsigned int BitMask_t;
extern void eat_apples_and_chew_bits(AppleCount_t, BitMask_t);

That's not a cure-all, but it's certainly one step up in self-documenting-ness from
extern void eat_apples_and_chew_bits(unsigned int, unsigned int);

--Quuxplusone (talk) 05:48, 26 August 2008 (UTC)[reply]

Thanks for the tip on the wikipedia rules!. Right about that. However, you're wrong about how typedefs are handled. You stated:

"I want to make sure that I don't accidentally add one of those bitmasks to my apple count, or pass one of my apple counts as a parameter to a function expecting a bitmask"

But! this code compiled and ran just fine with g++ 4.2.4.

#include <iostream>

typedef unsigned int apple;
typedef unsigned int orange;

using namespace std;

void apples_to_oranges( apple a, orange o )
{
    if( a > o )
    {
        cout << "Typedefs let you compare apples to oranges!" << endl;
    }
}

int main()
{
    apple a;
    orange o;
    apples_to_oranges( a, o );
    apples_to_oranges( o, a );
}

I assume correcting factual mistakes is OK on wikipedia, right? Sorry if not. I didn't see anything in the article about what you said either, so not sure if any of our comments are relevant. The article itself is excellent. Concise, and gives both sides. No issues with that. .



The first example is indeed stupid. I should be replace it by something realistic like

typedef unsigned char Byte;
typedef unsigned int Address;
typedef unsigned long Handle;


if you want the compiler to do the job for you go try java, typedefs are great for those who know how to use. 187.37.58.35 (talk) 16:22, 21 October 2009 (UTC)[reply]

Typedef for subroutines?

As from the libslack project:

typedef int hsort_cmp_t(const void *a, const void *b);

So this is a typedef for a subroutine? --Abdull (talk) 21:27, 9 December 2009 (UTC)[reply]

Yes, it defines the type hsort_cmp_t as (int *)(const void *, const void *), a pointer to a function returning an int and taking two const void pointers as arguments.
If you then declare such a variable (as a function argument, or as a local variable), you can just declare it like "hsort_cmp_t pf_cmp_char". I assume that in the example, it is a pointer to comparison function which is passed to a sort function.

-- Paul Richter (talk) 13:00, 5 April 2010 (UTC)[reply]

Use of casting notation?

Instead of

typedef int (*ImagePointer)[3];

is it legal and equivalent to write

typedef int (*)[3] ImagePointer;

If I haven't screwed it up, the latter is the notation that would be used in C-style casting. I think the article should mention whether or not this is legal and equivalent. Thank you. Quantling (talk) 15:32, 14 April 2010 (UTC)[reply]