What is the Role and Placement of Typedef in Struct in C Programming?

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Line
AI Thread Summary
The discussion centers on the use of structures and typedefs in C programming. A structure named "album" is defined to hold information about music albums, including name, artist, and number of tracks. To avoid global variables, instances of the structure can be declared within the main function, either directly or using a typedef for cleaner code. The typedef allows for easier reference to the structure type, enabling the use of custom identifiers like "Album" for the "album" structure. Additionally, examples illustrate how typedefs can simplify data type declarations, such as using USHORT for unsigned short and ULONG for unsigned long. It is emphasized that typedefs should be declared before any function definitions to ensure proper scope and accessibility.
transgalactic
Messages
1,386
Reaction score
0
first i create this structure :

Code:
struct album { 
  char *name; 
  char *artist; 
  int numTracks; 
}

if i want my variables not to be global
i can write this inside main:

struct album CD1, CD2, CD3;

or
"
typedef struct album Album;

... then inside main:

Album CD1, CD2, CD3; "

i can't understand this last part regarding where to write the typedef line

and what is the role of type of typedef?
 
Technology news on Phys.org
typedef allows you to use different identifiers to reference data types.
ie.

Code:
typedef unsigned short USHORT
typedef unsigned long  ULONG
typedef unsigned int    UINT

int main()
{  
    USHORT myShort = 5;
    ULONG   myLong = 10;
    UINT     sum = myShort+myLong;
    return 0;
}

typedefs should be used before the procedure definitions.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top