Unit 3
Unit 3
struct Organisation
{
char organisation_name[20];
char org_number[20];
struct Employee
{
int employee_id;
char name[20];
int salary;
} emp;
};
int main()
{
struct Organisation org;
printf("The size of structure organisation : %ld\n", sizeof(org));
org.emp.employee_id = 101;
strcpy(org.emp.name, "Robert");
org.emp.salary = 400000;
strcpy(org.organisation_name, "VelsUniv");
strcpy(org.org_number, "VISTAS123768");
printf("Organisation Name : %s\n", org.organisation_name);
printf("Organisation Number : %s\n", org.org_number);
printf("Employee id : %d\n", org.emp.employee_id);
printf("Employee name : %s\n", org.emp.name);
printf("Employee Salary : %d\n", org.emp.salary);
}
Pointers
Pointers are one of the most important and
powerful features of the C programming
language. They allow us to manipulate memory
directly, which can be very useful in many
programming scenarios.
printf("%d\n", *p);
#include <stdio.h>
int main() {
int a= 44;
int *b = &a;
printf("%d" , *b);
}
Pointer to pointer
A pointer can also point to another pointer variable. This is
known as a "pointer to a pointer". We declare a pointer to a
pointer by using two asterisks **. For example:
int x = 42;
int *p = &x;
int **q = &p;
union car
{
char name[50];
int price;
} car1;
Union vs Structure
#include <stdio.h>
union unionJob
{
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;
}
Output
size of union = 32
size of structure = 40
{
float salary;
int workerNo;
} j;
int main()
{
j.salary = 12.3;
// when j.workerNo is assigned a value,
// j.salary will no longer hold 12.3
j.workerNo = 100;
printf("Salary = %.1f\n", j.salary);
printf("Number of workers = %d", j.workerNo);
return 0;
}
Typedef
• The typedef is a keyword used in C
programming to provide some meaningful
names to the already existing variable in the C
program.
• we can replace the name of the existing data
type with the name which you have provided.
• It works similarly when we define aliases for
the commands.
Typedef
Syntax:
typedef <existing_name> <alias_name>
Example:
typedef long int lint;
Typedef example
#include <stdio.h>
struct student
int main()
{
{ char name[20];
typedef long int lint; int age;
lint i,j; };
i=100000; typedef struct student stud;
j=200000;
printf("Value of i is :%ld",i); stud s1, s2;
printf("\nValue of j is :%ld",j);
return 0;
}
Dynamic memory management functions
• Memory allocation is the act of allocating blocks
of physical memory to a particular program for it
to store data during execution.
• Allocation can take two forms: static and dynamic.
• Static memory allocation is when a certain amount
of memory has been preallocated for a program
and cannot be changed during runtime.
• Conversely, dynamic memory allocation is when
the amount of allocated memory can be adjusted
during runtime as needed by the program.
Dynamic memory allocation in C
C provides several functions in stdlib library for
dynamic memory allocation. <stdlib.h>
• malloc() allocates single block of requested
memory.
• calloc() allocates multiple block of requested
memory.
• realloc() reallocates the memory occupied by
malloc() or calloc() functions.
• free() frees the dynamically allocated
memory.
malloc()
General Syntax:
(cast-data-type *)malloc(size-in-bytes);
Example Syntax:
int *ptr = (int *)malloc(sizeof(int));
if( argc == 2 )
{
printf("\n Value given by user is: %s \t", argv[1]);
}
else if( argc > 2 )
{
printf("\n Many values given by users.\n");
}
else
{
printf(" \n Single value expected.\n");
}
}