1

I have a question in regards to typedef struct's that I believe I know the answer to but would like some clarification.

Lets say I have:

typedef struct Scanner{
    //information
}myScanner;

Would that have the same meaning as:

typedef struct Scanner *myScanner; 
struct Scanner {
    //information
};
Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
JoeCode
  • 11
  • 4
  • 5
    That is not the same. The second is a pointer. typedefing pointers is discouraged! – Paul Ogilvie Oct 24 '18 at 13:15
  • Note [Is it a good idea to typedef pointers](https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) — TL;DR "No", except perhaps function pointers. – Jonathan Leffler Oct 24 '18 at 13:52

3 Answers3

0

It will be easy to see the difference if you use the same format for both typedef definitions:

typedef struct Scanner myScanner;

versus

typedef struct Scanner *myScanner;

Defining a type-alias using typedef is very similar do declaring variables. For typedef the asterisk means "define as pointer", just like it does for variables.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

The type definitions are usually in a header file where no vars are created. In your main(), you are supposed to use this type to create vars. In our example, the newly minted type is 'Scanner'.


#include <stdio.h>
#include <stdlib.h>

struct _scanner
{       // example
    int a;
    double b;
    char c;
};
typedef struct _scanner Scanner;


int main()
{
    Scanner s;
    s.a = 10;
    s.b = 3.14;
    s.c = 'N';
    // -------------------
    // If you want to have a
    //  pointer p to scanner, then

    Scanner* p = &s;

    p->a = 20;
    p->b = 2.72;
    p->c = 'Y';

    printf("%d, %.2lf, %c\n",
           s.a, s.b, s.c);

    return EXIT_SUCCESS;
}

Output:

20, 2.72, Y

Once the variable is created, you can make a pointer to it, as a pointer must be able to point to something. The output shows that values have been successfully manipulated.

  • 2
    Note that you should not, in general, create function, variable, tag or macro names that start with an underscore. Part of [C11 §7.1.3 Reserved identifiers](https://port70.net/~nsz/c/c11/n1570.html#7.1.3) says: — _All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use._ — _All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces._ See also [What does double underscore (`__const`) mean in C?](https://stackoverflow.com/a/1449301) – Jonathan Leffler Oct 24 '18 at 13:54
0

typedef struct Scanner *myScanner; — here you are declaring type pointer to a struct Scanner.

typedef struct Scanner myScanner; — is a declaration of a new type.

What this means is, in the first case, you define a pointer to struct Scanner, for arithmetic calculations etc, the type of element that is pointed to. The size of myScanner will equal size of void* (pointer size on your os).

For the second case, you are defining a new type of element; it's size is
sizeof(struct Scanner) == size of(myScanner).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
H.cohen
  • 517
  • 3
  • 9