Program 1
Program 1
contiguous memory locations. They allow you to store multiple values of the same type under
a single variable name.
Declaration and Initialization:
// Initialization of an array
int numbers[5] = {1, 2, 3, 4, 5}; // Initialize with specific values
int num = numbers[2]; // Accesses the third element (index 2) of the array
Arrays and Pointers:
In C, arrays are closely related to pointers. The name of the array can be thought of as a
pointer to the first element of the array.
Array Size:
The size of the array must be known at compile time and cannot be changed during runtime.
Array Limitations:
Arrays in C have limitations, such as not knowing their size automatically in all contexts.
Thus, when passing arrays to functions, the size often needs to be passed separately.
Structures
Structures in C allow you to group different variables of different types under a single name.
They're used to create more complex data types that can hold multiple pieces of information.
struct Address {
char street[50];
char city[50];
};
struct Employee {
char name[50];
int empID;
struct Address address; // Nested structure
};
Pointers to Structures:
You can use pointers to access structures and their members.
Size of Structures:
The size of a structure in memory is determined by the sum of the sizes of its members,
possibly with additional padding for memory alignment.
Understanding structures in C allows you to organize and work with complex data more
effectively, enabling you to create data structures that match the requirements of your
programs.
Pointers
Pointers in C are variables that store memory addresses. They are powerful and fundamental,
allowing you to work with memory directly and efficiently. Here are some key points:
Declaration and Initialization:
int x = 10;
int *ptr; // Declaration of a pointer to an integer
ptr = &x; // Assigning the address of 'x' to the pointer
printf("Value of x: %d\n", *ptr); // Accessing the value of 'x' using the pointer
To access the address a pointer is holding, you can simply use the pointer variable name.
Pointer Arithmetic:
Pointers are often used in functions to modify variables outside the function scope.
NULL Pointers:
Pointers can also have a special value NULL, which means they are not pointing to any valid
memory address.
int *nullPtr = NULL; // Initializing a pointer as NULL
if (nullPtr == NULL) {
printf("Pointer is NULL\n");
}
// without pointers
#include<stdio.h>
#include<stdlib.h>
struct week_activity{
char day[10];
int date;
char activity[50];
};
void create(struct week_activity WC[])
{
int i;
for(i=0;i<7;i++)
{
printf("\nenter the Day ie., Monday etc: ");
scanf("%s",WC[i].day);
fflush(stdin);
printf("\nenter the date such as 1 to 31: ");
scanf("%d",&WC[i].date);
fflush(stdin);
printf("\nenter the activity: ");
gets(WC[i].activity);
fflush(stdin);
}
}
void display(struct week_activity WC[]){
int i;
printf("\n Day\t Date\t ACtivity\n");
for(i=0;i<7;i++){
printf("%s\t%d\t %s\n",WC[i].day,WC[i].date,WC[i].activity);
}
}
void main(){
struct week_activity WC[7];
create(WC);
display(WC);
}
//with pointers
#include<stdio.h>
#include<stdlib.h>
struct week_activity{
char *day;
int date;
char *activity;
};
void create(struct week_activity WC[])
{
int i;
for(i=0;i<7;i++)
{
printf("\nenter the Day ie., Monday etc: ");
gets(WC[i].day = (char *) malloc(sizeof(char)));
fflush(stdin);
printf("\nenter the date such as 1 to 31: ");
scanf("%d",&WC[i].date);
fflush(stdin);
printf("\nenter the activity: ");
gets(WC[i].activity = (char *) malloc(sizeof(char)));
fflush(stdin);
}
}
void display(struct week_activity WC[]){
int i;
printf("\n Day\t Date\t ACtivity\n");
for(i=0;i<7;i++){
printf("%s\t%d\t %s\n",WC[i].day,WC[i].date,WC[i].activity);
}
}
void main(){
struct week_activity *WC= (struct week_activity *) malloc(7*sizeof(struct week_activity));
create(WC);
display(WC);
}