Unions Enum Pointers
Unions Enum Pointers
C Union is also like structure, i.e. collection of different data types which are
grouped together. Each element in a union is called member.
Below table will help you how to form a C union, declare a union, initializing
and accessing the members of the union.
Structure Union
1. The keyword struct is used to 1. The keyword union is used to define
define a structure a union.
2. When a variable is associated with 2. When a variable is associated with a
a structure, the compiler allocates union, the compiler allocates
the memory for each member. the memory by considering the size
of the largest memory.
3. The size of structure is greater than 3. Size of union is equal to the size of
or equal to the sum of sizes of its largest member.
members.
4. Each member within a structure is 4. Memory allocated is shared
assigned unique storage area of by individual members of union.
location.
5. The address of each member will 5. The address is same for all the
be in ascending order. This members of a union. This
indicates that memory for each indicates that every member begins
member will start at different offset at the same offset value.
values.
6. Altering the value of a member will 6. Altering the value of any of the
not affect other members of member will alter other member
the structure. values.
7. Individual member can be accessed 7. Only one member can be accessed at
at a time a time.
8. Several members of a structure can 8. Only the first member of a union can
initialize at once. be initialized.
Enumerated Data type in C:
In C programming, An Enumerated Data type (also called enum) is a user defined
data type that consists of integral constants.
e.g:
enum week {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}
You can change default values of enum elements during declaration (if necessary).
Enum suit {
club = 0,
diamonds = 10,
hearts = 20,
spades = 3,
};
When you define an enum type, the blueprint for the variable is created. Here's how
you can create variables of enum types.
#include <stdio.h>
int main()
today = Wednesday;
printf("Day %d",today+1);
return 0;
Output: Day 4
Example2:
// Another example program to demonstrate working of enum in C
#include<stdio.h>
enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
int main()
int i;
return 0;
Output:
0 1 2 3 4 5 6 7 8 9 10 11
Pointers in C:
Variable Address in C:
Consider a variable is declared as int num = 10; the compiler allocates memory.
So, &num will give you its address in the memory.
While using scanf("%d", &num); ,the input value 10 is stored at the address of num.
When the following statements are used we will get the output as:
Output
num: 10
address of num: 8280
Pointer variable:
A pointer is a variable that stores the address of another variable.
Unlike other variables that hold values, pointer holds the address of a variable.
For example, an integer variable holds (or you can say stores) an integer value,
however an integer pointer holds the address of a integer variable. I
7. Pointers are helpful in traversing through arrays and character strings. The
strings are also arrays of characters terminated by the null character ('\O').
10. Pointers are used to construct different data structures such as linked lists,
queues, stacks, etc.
Declaration of C Pointer variable:
General syntax of pointer declaration is,
datatype *pointer_name;
Data type of a pointer must be same as the data type of the variable to which the
pointer variable is pointing.
Here are a few examples:
int *ip // pointer to integer variable
float *fp; // pointer to float variable
double *dp; // pointer to double variable
char *cp; // pointer to char variable
#include <stdio.h>
int main()
{
int a, *p; // declaring the variable and pointer
a = 10;
p = &a; // initializing the pointer
In a 16 bit machine, size of all types of pointer, be it int*, float*, char* or double* is
always 2 bytes. But when we perform any arithmetic function like increment on a
pointer, changes occur as per the size of their primitive data type.
char 1
long 4
float 4
double 8
long double 10
float* i;
i++;
In this case, size of pointer is still 2 bytes initially. But now, when we
increment it, it will increment by 4 bytes because float datatype is of 4
bytes.
double* i;
i++;
Similarly, in this case, size of pointer is still 2 bytes. But now, when we
increment it, it will increment by 8 bytes because its data type is double.
The concept of pointer arithmetic remains exact same, but the size of
pointer and various datatypes is different in a 32 bit machine. Pointer in
32 bit machine is of 4 bytes.
char 2
long 8
float 8
double 16
Note: We cannot add two pointers. This is because pointers contain
addresses, adding two addresses makes no sense.
#include <stdio.h>
int main()
int m = 5, n = 10, o = 0;
int *p1;
int *p2;
int *p3;
o = *p1+*p2;
p3 = p1-p2;
p1++;
p2--;
return 0;
}
Output:
p1 = 2680016
p2 = 2680012
*p1+*p2 = 15
p1-p2 = 1
p1++ = 2680020
p2-- = 2680008