You are on page 1of 20

JU,JIT,ECE, ECE2209 CHAPTER POINTERS AND STRUCTURE

Hey X Ive ur memory address as my value and I can POINT at u and access u whenever I want

X s MEMORY ADDRESS

P is a pointer
which points to X

VALUE

P s MEMORY ADDRESS
Jan 2012

X s MEMORY ADDRESS
1

Fundamentals

JU,JIT,ECE, ECE2209

Pointers:A pointer is a variable that contains a memory address. point to a location of another variable in memory.  if P contains the address of X , then P is said to "point to" X .  P is called a pointer Pointer declaration var_type * var_name int *p ; var_type is the pointer s base type, which is a valid C++ type, like int, double, float, char.
Jan 2012
2

Fundamentals
Q1: How to create a pointer p ? A1: int *P;

JU,JIT,ECE, ECE2209

* used to declare P to be a pointer to an integer

Q2: How to assign the memory address of X to


P?

A2: P=&x; // initializing a pointer with address of x & used to assign the memory address(location) of X
to P

N.B: Remember to initialize a pointer to point to a


memory address of the variable to be pointed
Jan 2012
3

Fundamentals

JU,JIT,ECE, ECE2209

Q 3: How to access the value of X indirectly using P ? And give the value of X to Y? A3: normally: Y=X; but, by using p, by pointer: Y=*P; Assigns the value of X to Y which is pointed by P . So, P---contains the address of X *P---points to the value assigned to X,
Jan 2012
4

Demonstration

JU,JIT,ECE, ECE2209

Let, int X=10; and let it is located at the memory location 0x28ff44 Creating a pointer P, int *P; Give the memory location of X to P P=&X; // this means P= 0x28ff44 *P points to the value of X which is 10 int Y= *P; // this means Y=10; *P=20; // this means X=20; i.e changing *P means changing X
Jan 2012
5

Example 1
#include<iostream> using namespace std; int main() { int x; int *p; p=&x; cout<< enter an integer <<endl; cin>>x; cout<<*p<<endl; cout<<p<<endl; system( pause ); return 0; }

Example 2
#include <iostream> using namespace std; int main() {int balance; int *balptr; int value; balance = 3200; balptr = &balance; value = *balptr; cout << "balance is: " << value <<endl; System( pause ); return 0; }

Pointer Arithmetic
These are the only possible operators

JU,JIT,ECE, ECE2209

++,

, +, and

The only possible pointer arithmetic operations pointer + integernumber pointer-integernumber pointer-pointer (the 2 pointers must be of the same base type)

These are impossible in pointer arithmetic pointer+ float(double)number pointer- float(double)number pointer +pointer

For an int type computer reserves 4 bytes and for a char type 1byte To increment or decrement the value at the location pointed to by a pointer, (*p)++; (*p)--; using the brackets is a must for precedence reason.
Jan 2012
8

example
The outcome depends on the size of the object pointed to. For example, if the base type is char, one byte is reserved, if it is integer, 4 bytes are reserved. Assume the pointer int *p if p points to the memory address 2000, the next integer will be at the next location of 2004 because the computer reserves four bytes for each integer. i.e (*p)++; points to the next integer at the memory location of 2004 not at 2001 or (*p)--; points to the previous integer at the memory location of 1996.
9

Example 3
#include<iostream> using namespace std; int main() { char *str="HELLO"; // assigns H to str because HELLO is an array of chars int num[]={10,20,30,40}; int *p1= &num[1]; int *p2= &num[3]; int *p3=p1+1; // p3 will point to the address of the memory location of //num[2] int n=p2-p1; // note that an integer is represented by 4 bytes. n=2 cout<<"*str= "<<*str<<endl; cout<<"*(str+1)="<<*(str+1)<<endl; cout<<"n is: "<<n<<endl; cout<<"p3= "<<p3<<endl; // address of the memory location pointed by p3 cout<<"*p3= "<<*p3<<endl; // the value held by the memory location //pointed //by p3 System( pause ); return 0; } 10

Example 4
//demonstrates accessing arrays through pointer arithmetic #include<iostream> using namespace std; int main() { const int size = 3; int a[size]={22,33,44}; int *end = a+size; // end points the fourth element of the array which is none cout<<"a "<<a<<endl; int sum=0; for(int *p=a;p<=end; p++ ) { sum += *p; // adds the value pointed by p to sum cout<<"\tp = "<<p; cout<<"\t*p = "<<*p; //accesses array elements indirectly using pointer cout<<"\tsum= "<<sum; cout<<endl; } cout<<"end "<<end<<"\n"; System(pause); return 0;}

11

Structure
Sometimes it is useful to have a collection of values of different types and to treat the collection as a single item. In C++, a structure is a collection of variables that are referenced under one name, providing a convenient means of keeping related information together. Structures are called aggregate data types because they consist of several different, yet logically connected, variables. The important property of structures is that the data in a structure can be a collection of data items of diverse types. Generally, all members of the structure will be logically related to each other. For example, structures are typically used to hold information such as mailing addresses, compiler, library card catalog entries, and the like.

Relationship between members variables are purely determined by the programmer. The keyword struct tells the compiler that a structure definition is beginning. Notice that the declaration is terminated by a semicolon. This is because a structure declaration is a statement.
12

syntax
struct tag { member(s); } variable; struct shopitem { char itemname[3]; // item name int itemnum; //item tag number double cost; //cost double retail; //retail price int onHand; //amount on hand }; //structure definition the variables are called members or fields of the structure
13

Accessing structure members


structure elements are accessed as; structure-varname.member-name For Eg: to access cost of the structure variable myShop and give it value 15.5 myShop.cost = 15.5; To print cost on the screen, cout<<myShop.cost;

14

Continue .
The general form of a structure declaration is shown here: For Eg. struct struct-type-name { type element_name1; type element_name2; type element_name3; . . . type element_nameN; } structure-variables;
15

Example 1
//a program that store information of students #include<iostream> using namespace std; struct students { int age; int id; char sex; char name[10]; }; int main() { const int size=10; int n; struct students student1;
16

Continue .
cout<<"enter name of student"<<endl; cin>>student1.name; cout<<"enter id"<<endl; cin>>student1.id; cout<<"enter sex"<<endl; cin>>student1.sex; cout<<"enter age"<<endl; cin>>student1.age; cout<<endl<<endl; cout<<"Students' Information"<<endl<<endl; cout<<"\t______________________________________________________"<<endl; cout<<"\tno\t"<<"name\t"<<"id"<<"\t"<<"sex"<<"\t"<<"age"<<endl; cout<<"\t______________________________________________________"<<endl; cout<<"\t"<<1<<"\t"<<student1.name<<"\t"<<student1.id<<"\t"<<student1.sex<<"\t"<<stude nt1.age<<endl; system("pause"); return 0; }

17

Array of structure
Structures may be arrayed. In fact, structure arrays are quite common. To declare an array of structures, you must first define a structure, then declare an array of its type. For eg: shopitem arrayitem[100]; To access a specific structure within an array of structures, you must index the structure name. For example, to display the cost member of the third structure, you would write, cout << arrayitem[2].cost;

18

Example
// a program that will store students information for more than one student. #include<iostream>

using namespace std; struct students { int age; int id; char sex; char name[10]; }; int main() { const int size=10; int n; students mystudents[size]; cout<<"enter number of students to this virtual data base"<<endl; cin>>n;

19

Continue
for(int i=0;i<n;i++) { cout<<"enter name of student"<<endl; cin>>mystudents[i].name; cout<<"enter id"<<endl; cin>>mystudents[i].id; cout<<"enter sex"<<endl; cin>>mystudents[i].sex; cout<<"enter age"<<endl; cin>>mystudents[i].age; } cout<<endl<<endl; cout<<"Students' Information"<<endl<<endl; cout<<"\t______________________________________________________"<<endl; cout<<"\tno\t"<<"name\t"<<"id"<<"\t"<<"sex"<<"\t"<<"age"<<endl; cout<<"\t______________________________________________________"<<endl; for(int i=0;i<n;i++) {cout<<"\t"<<i<<"\t"<<mystudents[i].name<<"\t"<<mystudents[i].id<<"\t"<<mystudents[i]. sex<<"\t"<<mystudents[i].age<<endl; } cout<<endl; system("pause"); return 0; 20 }

You might also like