0% found this document useful (0 votes)
5 views16 pages

Lecture 8

The document covers various programming concepts in C, including arrays, pointers, structures, and file processing. It explains how to declare and use arrays, pointers, and structures, along with their syntax and examples. Additionally, it introduces advanced topics such as arrays of structures and file processing methods.

Uploaded by

mugumeblessed123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Lecture 8

The document covers various programming concepts in C, including arrays, pointers, structures, and file processing. It explains how to declare and use arrays, pointers, and structures, along with their syntax and examples. Additionally, it introduces advanced topics such as arrays of structures and file processing methods.

Uploaded by

mugumeblessed123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 19—

Working with Arrays


An array is a list of more than one variable having the same name.

Not all lists of variables are arrays.


Array variables, or array elements, are differentiated by a subscript, which is a number inside
brackets.
Suppose you want to store a person's name in a character array called name. You know
you can do this with
char name[] = "Ray Krebbs";
or
char name[11] = "Ray Krebbs";
• An array can also be defined as a collection of elements of the same
data type stored in contiguous memory locations
• Each element in the array is accessed using an index, starting from 0.
syntax
dataType arrayName[array_size];
SIMPLE EXAMPLE OF ARRAYS
.

#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> .

// Define a constant for the number of elements in the array


#define NUM 8
int main() {
// Declare an array of integers with size NUM
int nums[NUM];
// Variable to hold the total of user's 8 numbers
int total = 0;
// Loop to get 8 numbers from the user
for (int i = 0; ctr < NUM; i++) {
// Prompt the user to enter the next number
printf("Please enter the next number: ");
// Read the number from the user and store it in the array
scanf("%d", &nums[i]);
// Add the entered number to the total
total += nums[i];
}
// Print the total of the entered numbers
printf("The total of the numbers is %d", total);
return 0;
SEARCHING ARRAYS

• READ SEARCHING OF ELEMENTS IN AN ARRAY


Chapter 20—
Pointer Storage
• Pointers are variables. They follow all the normal naming rules of regular,
nonpointer variables.
• As with regular variables, you must declare pointer variables before using
them. There is a type of pointer for every data type
• in C; there are integer pointers, character pointers, floating-point pointers,
and so on. You can declare global pointers or local pointers, depending on
where you declare them.
• About the only difference between pointer variables and regular variables is
the data they hold.
• Pointers do not contain data in the usual sense of the word. Pointers
contain addresses of data
Declaring Pointers
• declaring pointers is almost as easy as declaring regular variables.
After all, pointers are variables.
SYNTAX
data_type *pointer_variable;

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 = &num;
ptr_pi = &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

•Returning multiple values from a function:


Structures can be used to return multiple values from a function, as the members of a structure can hold
different values .
EXAMPLES OF STRUCTURES
// Structure declaration

struct Point {

int x;

int y;

};

int main() {

// Structure variable declaration

struct Point p;

// Accessing structure members

p.x = 10;

p.y = 20;

// Printing structure members

printf("x = %d, y = %d\n", p.x, p.y);

return 0;
#include <stdio.h>

// Structure declaration
struct Student {.

char name[50];
int age;
float marks;
};

int main() {
// Structure variable declaration
struct Student s1;

// Assigning values to structure members


strcpy(s1.name, "John");
s1.age = 20;
s1.marks = 85.5;

// Displaying structure members


printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Marks: %.2f\n", s1.marks);
Chapter 23—
Arrays of Structures
• This chapter teaches you the following topics:
• • Creating arrays of structures
• • Initializing arrays of structures
• • Referencing elements from a structure array
• • Dynamic memory allocation with malloc() and free()
Chapter 24—
Simple C File Processing
• This chapter teaches you the following topics:
• • An overview of disk files
• • Types of files
• • Sequential file access
• • Random file access

You might also like