0% found this document useful (0 votes)
57 views11 pages

"Pointers & Arrays": WEEK # 15

This document discusses pointers and arrays in C++. It contains the following key points: 1) The name of an array is a pointer that holds the address of the first element. An array name and a pointer can be used interchangeably. 2) When a pointer is incremented, it increases by the size of the data type it points to. For example, incrementing an integer pointer increases it by 4 bytes. 3) Examples are provided to demonstrate accessing array elements through pointers using pointer arithmetic and dereferencing. 4) Solutions are given for programs to take user input into an array, display the values through the array and pointers, and calculate the average.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views11 pages

"Pointers & Arrays": WEEK # 15

This document discusses pointers and arrays in C++. It contains the following key points: 1) The name of an array is a pointer that holds the address of the first element. An array name and a pointer can be used interchangeably. 2) When a pointer is incremented, it increases by the size of the data type it points to. For example, incrementing an integer pointer increases it by 4 bytes. 3) Examples are provided to demonstrate accessing array elements through pointers using pointer arithmetic and dereferencing. 4) Solutions are given for programs to take user input into an array, display the values through the array and pointers, and calculate the average.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Pointers & Arrays

WEEK # 15
LECTURE 2

Pointers and Arrays


#include<iostream.h> void main() { int a[3]={4,7,11}; int num=10;

cout<<num<<endl; cout<<&num<<endl;
cout<<endl; cout<<a[0]<<endl; cout<<&a[0]<<endl; cout<<a<<endl<<endl;; cout<<a[1]<<endl; cout<<&a[1]<<endl; }

Pointers and Arrays

The concept of array is very similar to the concept of pointer. The name of an array actually a pointer that holds the address of the first element of the array. Therefore if you have two declarations as follows: int a[10]; int *p; then the assignment p = a; is perfectly valid

Pointer (Revision)

If you increment a pointer, it will be increased by the size of whatever it points to.

int a[5]; int *ptr; ptr = a; or ptr=&a[0]; *(ptr+2) *ptr


a[0] a[1] a[2] a[3]

*(ptr+4)

a[4]

int a[5];

Pointers and Arrays


#include<iostream.h> void main() { int arr[3]={4,7,11}; int *ptr; ptr=arr; cout << *(ptr+1)<<endl; cout << *(ptr+2)<<endl;

Pointers and Arrays


#include<iostream.h> void main() { int arr[3]={4,7,11}; int *ptr; ptr=arr;

ptr++; cout<<*ptr<<endl; ptr--; cout<<*ptr<<endl; }

Examples

Write a program which will take ten values from the user. And the program should display the average of those ten values. Should be done through pointers.

Hint: Arrays and Pointers can be used.

Rewrite program with array of pointer

Solution
#include<iostream.h> void main() { int a[10]; int *p; int sum=0; p=a; for(int i=0;i<=9;i++) { cin>>*p; p++; } cout<<endl<<endl; cout<<"Value Display through Array..; cout<<<<endl; for(i=0;i<=9;i++) { cout<<a[i]<<" "; } p=a; cout<<endl<<endl; cout<<"Value Display through Pointers.."<<endl; for(i=0;i<=9;i++) { cout<<*p<<" "; p++; } p=a; for(i=0;i<=9;i++) { sum=sum+(*p); p++; } cout<<endl<<endl; cout<<"Average is "<<sum/10<<endl<<endl; }

Solution
#include<iostream.h> void main() { int a[10]; int *p[10]; int sum=0; for(int i=0;i<=9;i++) { p[i]=&a[i]; } for(i=0;i<=9;i++) { cin>>*p[i]; } cout<<endl<<endl; cout<<"Value Display through Array.."<<endl; for(i=0;i<=9;i++) { cout<<a[i]<<" "; } cout<<endl<<endl; cout<<"Value Display through Pointers.."<<endl; for(i=0;i<=9;i++) { cout<<*p[i]<<" "; }

for(i=0;i<=9;i++) { sum=sum+(*p[i]); } cout<<endl<<endl; cout<<"Average is "<<sum/10<<endl<<endl; }

String & Pointers


#include<iostream.h> void main() { char arr[20]="this is test"; int num[20]={10,12,34};

cout<<num<<endl; cout<<arr<<endl;

String & Pointers


#include<iostream.h> void main() { char arr[20]="this is test"; int num[20]={10,12,34}; char *ch; int *n; ch=arr; n=num; cout<<num<<endl; cout<<arr<<endl;

cout<<endl; cout<<n<<endl; cout<<ch<<endl;


cout<<endl; cout<<*n<<endl; cout<<*ch<<endl; cout<<endl; cout<<*(n+2)<<endl; cout<<*(ch+2)<<endl;}

You might also like