0% found this document useful (0 votes)
38 views

C++ Programming: Data Structure

1. The document discusses various C++ data structures including arrays, pointers, structures, and arrays of structures. 2. It provides examples of declaring, initializing, and using one-dimensional and multi-dimensional arrays. 3. Pointers are described as variables that contain the memory address of another variable, and examples of pointer syntax and operations like dereferencing are given. 4. Structures allow organizing simple variables into more complex entities, and structure elements can be accessed using the dot operator. Arrays of structures and structures within structures are also covered.

Uploaded by

Angelica Medina
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

C++ Programming: Data Structure

1. The document discusses various C++ data structures including arrays, pointers, structures, and arrays of structures. 2. It provides examples of declaring, initializing, and using one-dimensional and multi-dimensional arrays. 3. Pointers are described as variables that contain the memory address of another variable, and examples of pointer syntax and operations like dereferencing are given. 4. Structures allow organizing simple variables into more complex entities, and structure elements can be accessed using the dot operator. Arrays of structures and structures within structures are also covered.

Uploaded by

Angelica Medina
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

C++ Programming

Data Structure
Objectives

By the end of this courseware, the student is expected to:


1. Discover what arrays are and how to declare them
2. Analyze strings and how to use character arrays to make
them
3. Apply arrays in programs
4. Use Pointers
5. Organize Simple variables into more complex entities
using Structure.
Data Structure

Data Structure is a specialized format to store and


organize data in a computer’s memory.
Array
Array is a collection of data elements of the same type
that are referenced by a common name.

• One-dimensional Array
Syntax: type var_name[size];

Example:
int x[20];
char letter[26];
double price[10];
Array

• Two-dimensional Array
Syntax: type var_name[rowsize]
[colsize];

Example:
int x[3][4];
double amount [10][50];

• Multi-dimensional Array

Syntax: type var_name[size1][size2]…[sizen];

Example: int m[3][5][4];


Array Initialization
An array may be initialized at the time of its declaration

Syntax: type var_name[size1]…[sizeN]={ value list } ;

Examples:
int id[7]={1,2,3,4,5,6,7}
float f[] = {1.2. 14.3, 52.54 };
char vowel[5]={‘a’, ‘e’,’I’,’o’,’u’]};
char name=“rac”;
char vowel[6] = “aeiou”;
Pointer

• Pointer is a variable that contains the memory


address (or location) of another variable.
• It is an efficient way of accessing data since it
contains the actual location of your data where in the
compiler can find that data in memory easily.
Pointer
Pointer

• Pointer Variable are variables that can hold pointers to other


variables

Syntax : Type name *var_name1, * var_name2, …

Example:
char *p1, *p2;
int *p3;
double *p4, *p5;
Pointer

& Operator
• It is simply called the address operator.
• & operator is a unary operator that returns the
memory address of its operand.
• It produces a pointer that points to the variable
Example: p = &v
Pointer

* Operator
• It is called “dereferencing operator”
• * operator is a unary operator that returns the
value of the variable located at the address that
follows.
• It produces the variable it points to
Example: q = *m
Pointer

The asterisk (*) appears before a pointer variable in two


places:
• When declaring a pointer variable
• When dereferencing a pointer variable (to find data it
points to
Pointer Assignments

A pointer may be used on the right hand


side assignment statement to another pointer.
Pointer Assignments
int main(){
int num=5; *p1, *p2;
//declares an integer variable and 2
pointers
cout<<“Num : “<<num<<endl;
cout<<“Address of num : “<<&num<<endl;
cout<<“P1 : “<<p1<<endl;
p1=&num;
//assigns the address of num to p1;
p2=p1;
//assigns the value of p1 to p2;
cout<<p1<<endl;
cout<<p2<<endl;
return 0;
}
Pointer Arithmetic

Pointers can be incremented or decremented


by an integer

Example:
long *q;
q = q +1;
// value of q increased by 4
Pointer Comparison

• When two or more pointers are pointing to a


common object, it is valid to compare their address
values just like comparing two expressions.

• It is illegal to compare pointers pointing to objects


of different data types
Passing Arguments by Reference

int main(){
void max_min(int a, int b, int &max, int &min );
//function protrype
Int x,y,min, max;
cout<<“Enter 2 number : “;
cin>>x>>y;
max_min(x,y,max,min);
cout<<“MAX = “<<max<<endl;
cout<<“MIN = “<<min<<endl;
return 0;
}
Passing Arguments by Reference

void max_min(int a, int b, int &max, int &min){


if( a>b){
max=a;
min=b;
}else{
max=b;
min=b;
}
}
Pointers to Character Arrays

Many string operations are usually performed by using


pointers because strings tend to be accessed in a strictly
fashion.
Pointers to Character Arrays

strcmp(char *s1, char*s2){


while(*s1){
if (*s1-*s2)
return (*s1 - *s2)
else {
s1++;
s2++;
}
}
return 0;
}
Array of Pointers
Pointers may be arrayed like any other data type.

Examples:
• The declaration for an int pointer array of size 20
 int *arrayPtr[20]
• To assign the address of an integer variable var to the
3rd element of the array
 *arrayPtr[2] = &var;
Arrays of Pointer

• To find the value stored in var


cout<<*arrayPtr[2];

• To pass an array of pointers to a function, simply


call the function with the array name without index.
Ex: Pass the array arrayPtr to function viewArray
 viewArray(arrayPtr);
Arrays of Pointer

• To receive the address of array x in function fn


fn(float *x) //Pointer
fn(float x[5]) //Sized Array
fn(float x[]) //Unsized Array
Arrays of Pointer

The address of x is passed to the parameter ptr:

main(){
floatx[5];
void fn (float *);
fn(x);
return 0;
}
void fn() {

}
Structure

struct student{
struct student{
char id[5];
char id[5];
char name[25];
char name[25];
int age;
int age;
};
}stud1, stud2;
struct student stud1, stud21;

struct {
char id[5];
char name[25];
int age;
}stud1, stud2;
Referencing Structure Elements

A Structure element can be accessed and assigned a value


by using the:
• Structure variable name
• Dot operator
• Element’s name

Example : stud1.name=“Racquel”
Array of Structures

Structure of the same type can be grouped together


into an array.

struct student{
struct student{
char id[5];
char id[5];
char
char
name[25];
name[25];
int age;
int age;
};
} stud[40];
Sturct stud[50]
Passing Structure to Functions

As in the case of arrays, individual structure elements or


an even entire structure can be passed to functions as
arguments

Examples:
Modify(stud[6].name);
// pass structure element to function modify
Modify(stud[6] name);
// pass entire structure to function modify
Structure within Structure
struct student{
char id[5];
char name[25];
int age;
}stud1, stud2;

struct detail {
struct student stud;
char course[10];
Int level;
} stud_detail1;
Student id = stud_detail1.stud.id;
Structure within Structure

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace td;
struct book{
char title[40];
char author[25];
double cost;
};
Structure within Structure
int main(){
struct book bk;
cout<<“ENTER TITLE : “
gets(details.title);
cout<<“ENTER AUTHOR : “
gets(details.author);
cout<<“ENTER COST : “
cin>>details.cost;
printf("%s by %s : P %.2f " ,
details.title, details.author, details.cost);
cout<<endl;
return 0;
}

You might also like