You are on page 1of 7

Arrays in C are a fundamental data structure used to store elements of the same data type in

contiguous memory locations. They allow you to store multiple values of the same type under
a single variable name.
Declaration and Initialization:

// Declaration of an array to hold 5 integers


int numbers[5];

// Initialization of an array
int numbers[5] = {1, 2, 3, 4, 5}; // Initialize with specific values

// You can also initialize without specifying the size explicitly


int numbers[] = {1, 2, 3, 4, 5}; // Compiler infers size based on number of elements
Accessing Array Elements:
Array indexing in C starts from 0, meaning the first element of the array is accessed using
index 0.

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.

int numbers[5] = {1, 2, 3, 4, 5};


int *ptr = numbers; // 'ptr' points to the first element of the 'numbers' array

// Accessing elements using pointer arithmetic


int thirdElement = *(ptr + 2); // Accesses the third element of the array using pointer
arithmetic
Arrays and Memory:
Arrays store elements in contiguous memory locations, which means elements are placed
next to each other in memory. This property allows for efficient memory access and
manipulation.

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.

void processArray(int arr[], int size) {


// Code to process the array using 'size'
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int arraySize = sizeof(numbers) / sizeof(numbers[0]); // Calculate array size
processArray(numbers, arraySize); // Pass array and its size to a function
return 0;
}

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.

Declaration and Initialization:


// Defining a structure
struct Person {
char name[50];
int age;
float height;
};

// Declaring structure variables


struct Person person1; // Declaring a variable of type struct Person
Accessing Structure Members:
You access the members of a structure using the dot operator ..

// Assigning values to structure members


strcpy(person1.name, "John");
person1.age = 25;
person1.height = 5.9;

// Accessing structure members


printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.2f\n", person1.height);
Initialization of Structure Variables:

// Initializing structure variables during declaration


struct Person person2 = {"Alice", 30, 5.5};

// Accessing structure members of initialized variables


printf("Name: %s\n", person2.name);
printf("Age: %d\n", person2.age);
printf("Height: %.2f\n", person2.height);
Nested Structures:
Structures can be nested within other structures, allowing for more complex data
organization.

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.

struct Person *ptrPerson = &person1; // Pointer to struct Person


printf("Name: %s\n", ptrPerson->name); // Accessing members using pointer and -> operator

Passing Structures to Functions:


You can pass structures to functions by value or by reference (using pointers).

void displayPerson(struct Person p) {


printf("Name: %s\n", p.name);
printf("Age: %d\n", p.age);
printf("Height: %.2f\n", p.height);
}

// Passing structure by value


displayPerson(person1);

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

Accessing Value and Address:


To access the value a pointer is pointing to, you dereference it using the * operator.

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.

printf("Address of x: %p\n", ptr); // Printing the address of 'x'

Pointer Arithmetic:

Pointer arithmetic involves adding or subtracting integers to/from a pointer. Incrementing or


decrementing a pointer moves it to the next or previous memory location of its type.

int arr[5] = {1, 2, 3, 4, 5};


int *arrPtr = arr; // Pointer to the first element of the array

printf("Value at arrPtr: %d\n", *arrPtr); // Accessing the first element

arrPtr++; // Move the pointer to the next element


printf("Value at arrPtr after increment: %d\n", *arrPtr); // Accessing the second element

Pointers and Arrays:


Arrays in C can be accessed via pointers. The name of the array is a pointer to the first
element.

int arr[3] = {10, 20, 30};


int *ptrArr = arr; // 'ptrArr' points to the first element of 'arr'

printf("%d\n", *ptrArr); // Prints the value of the first element (10)


printf("%d\n", *(ptrArr + 1)); // Accesses the second element (20)
Pointers and Functions:

Pointers are often used in functions to modify variables outside the function scope.

void changeValue(int *ptr) {


*ptr = 50; // Changing the value pointed by the pointer
}
// Usage
int num = 10;
changeValue(&num); // Passing the address of 'num' to the function
printf("New value of num: %d\n", num); // Output: 50

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");
}

Understanding pointers is crucial in C programming as they are extensively used in memory


allocation, data structures, and accessing hardware efficiently. However, incorrect usage of
pointers can lead to bugs like segmentation faults or memory leaks, so they should be handled
with care.

1. Develop a Program in C for the following:


a) Declare a calendar as an array of 7 elements (A dynamically Created array) to
represent 7 days of a week. Each Element of the array is a structure having three fields.
The first field is the name of the Day (A dynamically allocated String), The second field
is the date of the Day (A integer), the third field is the description of the activity for a
particular day (A dynamically allocated String).
b) Write functions create(), read() and display(); to create the calendar, to read the data
from the keyboard and to print weeks activity details report on screen.

// 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);
}

You might also like