2

My code passes around pointers to scalar types that represent different things that are easily confused. I thought the compiler could help me with this.

Here is a test program:

typedef int type_a;
typedef int type_b;

type_a do_something_with_a(type_a* pa)
{
    return *pa + 3;
}

int main(void)
{
    type_b b = 0;
    do_something_with_a(&b);  /* This is a bug. */
    return 0;
}

I am passing a B to a function that is only supposed to do things with As, and yet gcc does not even throw a warning:

$ gcc -Wall typesafe2.c 
$ 

Is there an elegant way I can write equivalent code but have the compiler throw errors when I make such a terrible mistake?

4 Answers 4

3

instead of typedef int type_a; etc., use typedef struct { int value; } type_a;

1
  • Thanks. I also need to make changes wherever I dereference the pointers because the values are now members of a struct. But this approach gets me what I want.
    – Rob Fisher
    Mar 4, 2013 at 17:53
3

typedef does not define a new type, but rather an alias for an exisiting type. As both your types are int there is no bug! See here.

1
  • Thanks. This explains why it does not work, but not how I can get what I want. However the other answers give me something to try.
    – Rob Fisher
    Mar 4, 2013 at 16:25
2

Your typedefs are synonymous, per the standard:

C99 §6.7.8,p3:

In a declaration whose storage-class specifier is typedef, each declarator defines an identifier to be a typedef name that denotes the type specified for the identifier in the way described in 6.7.6. Any array size expressions associated with variable length array declarators are evaluated each time the declaration of the typedef name is reached in the order of execution. A typedef declaration does not introduce a new type, only a synonym for the type so specified. That is, in the following declarations:

typedef T type_ident;
type_ident D;

type_ident is defined as a typedef name with the type specified by the declaration specifiers in T (known as T ), and the identifier in D has the type ‘‘derived-declarator- type-list T ’’ where the derived-declarator-type-list is specified by the declarators of D. A typedef name shares the same name space as other identifiers declared in ordinary declarators.

So unless you declare unique types to drill through (such as a distinct structure type) you're likely not going to get what you want.

0

instead of using typedef on basic datatype...use struct type

e.g.

typedef struct { int a; } type_1;
typedef struct { int b; } type_2;

this will probably solve the issue and compiler will throw warning/error

2
  • Did you not like the answer I provided? ;)
    – mah
    Mar 4, 2013 at 14:24
  • arrrrrghhh...i didnt see that u suggested the same...i generally dont look into answers..simply read the question and write my answer....look like i have to chage my style a bit Mar 5, 2013 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.