Lecture 8
Lecture 8
#include <stdio.h>
int main() {
// Declare an array of integers with size 5
int numbers[5];
// Assign values to the elements of the array
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Print the elements of the array
printf("Elements of the array:\n");
for (int i = 0; i < 5; ++i) {
printf("Element %d: %d\n", i, numbers[i]);
} return 0;
}
#include <stdio.h> .
E.G
• To declare the pAge pointer variable, you must do the following:
int * pAge; // Declare an integer pointer
Assigning Values to Pointers
int age=30; /* Declares a regular integer variable, putting 30 in it */
int *pAge=&age; /* Declares an integer pointer, initializing it with the
address*/
printf("%d", age); // Prints the value of age
#include <stdio.h>
int main()
{ int num = 42;
.
float pi = 3.14;
char ch = 'A';
// Declare pointers for different data types
int *ptr_num; // Pointer to an integer
float *ptr_pi; // Pointer to a float
char *ptr_ch; // Pointer to a char
// Assign the address of variables to pointers
ptr_num = #
ptr_pi = π
ptr_ch = &ch;
// Access values through pointers
printf("Value of num: %d\n", *ptr_num);
printf("Value of pi: %.2f\n", *ptr_pi);
printf("Value of ch: %c\n", *ptr_ch);
return 0;}
Chapter 21—
Using Pointers
• READ AND UNDERSTAND
Chapter 22—
Introduction to Structures
• A structure is a user-defined data type that allows you to group
together variables of different data types under a single name.
SYNTAX
struct structure_name {
data_type member1;
data_type member2;
// ...
};
USES OF STRUCTURES
Defining custom data types: Structures can be used to define custom data types that are not present in
the language,
such as dates, time, complex numbers, etc.
•Data organization: Structures can be used to store a large amount of data in different fields,
allowing for better organization and management
•Creating data structures: Structures can be used to create more complex data structures,
such as trees and linked lists
struct Point {
int x;
int y;
};
int main() {
struct Point p;
p.x = 10;
p.y = 20;
return 0;
#include <stdio.h>
// Structure declaration
struct Student {.
char name[50];
int age;
float marks;
};
int main() {
// Structure variable declaration
struct Student s1;