Arrays & String Manipulation Concise
Arrays & String Manipulation Concise
Chapter Four
Arrays, String Manipulation and Pointers
4.1. What is an array
An array is a collection of data structure that stores elements/ items that have the same data
type
An array element is identified and accessed by its own unique index (or subscript).
The index must be an integer and indicates the position of the element in the array
In C++ the index of the first array element is 0 and the last element is N-1, where N is the
size of the array.
0 1 2 3 4 indexes
10 20 30 40 50 Elements or Contents
In the first case the array would be allocated space for seven characters that is sixth spaces
for the string Hello! & one for the null character. In the second case the string is set by the
declaration to be 18 characters long but only sixteen of these characters are set, i.e. the
fourteen characters of the string, one for space and the null character.
There is also another function strncpy, is like strcpy, except that it copies only a specified
number of characters.
Syntax: strncpy(destination, source, int n); //It may not copy the terminating null
character.
Example
#include <iostream.h>
#include <string.h>
int main() {
char str1[] = "String test";
char str2[] = "Hello";
char one[10];
strncpy(one, str1, 9);
one[9] = '\0';
cout << one << endl;
strncpy(one, str2, 2);
cout << one << endl;
strcpy(one, str2);
cout << one << endl;
return 0;
}
Concatenating string
The strcat() or strncat are used for for concatenating strings.
The function strcat concatenates (appends) one string to the end of another string.
strcat(destination, source);
Example:
#include <iostream.h>
#include <string.h>
int main() {
char str1[30];
strcpy(str1, "abc");
cout << str1 << endl;
strcat(str1, "def");
cout << str1 << endl;
char str2[] = "xyz";
strcat(str1, str2);
cout << str1 << endl;
str1[4] = '\0';
cout << str1 << endl;
return 0;
}
The function strncat is like strcat except that it copies only a specified number of characters.
strncat(destination, source, int n); // It may not copy the terminating null character.
Example:
#include <iostream.h>
#include <string.h>
int main() {
char str1[30];
strcpy(str1, "abc");
cout << str1 << endl;
strncat(str1, "def", 2);
str1[5] = '\0';
cout << str1 << endl;
char str2[] = "xyz";
strcat(str1, str2);
cout << str1 << endl;
str1[4] = '\0';
cout << str1 << endl;
return 0;
}
Pointers
______________________________________________________________________________
A pointer is simply the address of a memory location and provides an indirect
way of accessing data in memory. A pointer variable is defined to ‘point to’
data of a specific type. For example:
int *ptr1; // pointer to an int
char *ptr2; // pointer to a char
The value of a pointer variable is the address to which it points. For example,
given the definitions
int num; //can be written as ptr1 = #
The symbol & is the address operator; it takes a variable as argument and
returns the memory address of that variable. The assignment ptr1 = # is
that the address of num is assigned to ptr1. Therefore, ptr1 points to num.
Diagrammatically,
Given that ptr1 points to num, the expression *ptr1 dereferences ptr1 to get
to what it points to, and is equivalent to num.
The symbol * is the dereference operator; it takes a pointer as argument and
returns the contents of the location to which it points.
Example 1: Write C++ Program that finds the square of a number
//Demonstration of Pointers
#include<iostream.h>
#include<math.h>
int main()
{
double num=25;
double *p1, *p2, *sq;
p1 = #
p2= #
sq=#
Write C++ program that accepts array elements and displays them to console
using DMA
//Demonstration of DMA
//Demonstration of DMA
#include<iostream.h>
int main()
{
int *N = new int[20]; // Allocate memory for 20 ints dynamically
int *n = new int; // Allocate memory for single int dynamically
cout<<"\n Enter the number of Array elements: ";
cin>>*n;
cout<<"\n Inserting array contents using DMA\n ";
for(int i=0;i<*n;i++)
{
cout<<"\n Enter Element "<<i+1<<": ";
cin>>N[i];
}
cout<<"\n Displaying array contents using DMA\n ";
for(int i=0;i<*n;i++)
{
cout<<N[i]<<" ";
}
delete [] N;
delete n;
return 0;
}
Pointer Arithmetic
Pointer arithmetic means performing mathematical calculations using
pointers but not the same as integer arithmetic, because the outcome depends
on the size of the object pointed to. For example, an int is represented by 4
bytes. Now, given
char *str = "HELLO";
int nums[] = {10, 20, 30, 40};
int *ptr = &nums[0]; // pointer to first element
str++ advances str by one char (i.e., one byte) so that it points to the second
character of "HELLO", whereas ptr++ advances ptr by one int (i.e., four bytes)
so that it points to the second element of nums.
The elements of "HELLO" can be referred to as *str, *(str + 1), *(str + 2), etc.
Similarly, the elements of nums can be referred to as *ptr, *(ptr + 1), *(ptr + 2),
and *(ptr + 3).
Another form of pointer arithmetic allowed in C++ involves subtracting two
pointers of the same type. For example:
int *ptr1 = &nums[1];
int *ptr2 = &nums[3];
int n = ptr2 - ptr1; // n becomes 2
//Addition of two numbers using pointers
#include<iostream.h>
int main() {
int x,y,sum=0;
int *p1 = &x,*p2 = &y;
int *s = ∑
cout<<”\n Enter Value of x: ”;
cin>>*p1;