0% found this document useful (0 votes)
20 views4 pages

Week 11 - Structured Programming

The document discusses strings in C++. It explains that strings can be represented as character arrays or character pointers. It provides examples of initializing and copying strings using arrays and pointers. It notes that arrays are safer for storing strings than pointers due to potential for overwriting memory.

Uploaded by

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

Week 11 - Structured Programming

The document discusses strings in C++. It explains that strings can be represented as character arrays or character pointers. It provides examples of initializing and copying strings using arrays and pointers. It notes that arrays are safer for storing strings than pointers due to potential for overwriting memory.

Uploaded by

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

Structured Programming (C++) HNDIT

Week 11
Strings
Unlike some other languages, there is no ‘string’ data type in C++.
We can only represent strings of characters in two rather indirect ways:
1. As arrays of type char
2. By pointers of type char

An Array of Type char:


An array of 10 characters would be declared as char astring[10];
In fact the maximum string which we could contain in this array would be 9 characters long, since all
strings must be terminated by the escape sequence character ‘\0’.
The following figure shows the contents of astring when it contains the string ‘hello’.
astring h e l l o \0

If astring was to contain the string ‘characters’ then the terminating ‘\0’ would be lost.
astring c h a r a c t e r s

Any attempt to display or copy this string would cause problems since the string has an undefined
length, and the program would simply display or copy the contents of all memory locations after the
string until a ‘\0’ was encountered.
To initialize a character array with a string literal, we can use the assignment operator (=) and {}.
E.g.: char astring[]={“hello”};
We cannot assign a string literal to a character array after it has been declared - this can only be
done one character at a time.
Example:
#include <iostream.h>
void main()
{
char astring[4]={"abc"};
char bstring[4];
//bstring={"xyz"} will not compile
bstring[0]='x';
bstring[1]='y';
bstring[2]='z';
bstring[3]='\0';
cout<<astring<<endl;
cout<<bstring<<endl;
}

Output:
abc
xyz

Likewise, if we want to make one array of characters equal to another, then unfortunately the
assignment operator cannot be used.
E.g.: bstring = astring is not allowed
So, to copy the contents of astring into bstring, we can use strcpy(destination, source) and
strncpy(destination, source, num_chars) of the header file string.h
E.g.: strcpy(bstring,astring);
strncpy(bstring,astring,9);
Example:
#include <iostream.h>
#include <string.h>
void main()
{
char astring[20];
char bstring[10];
cout<<"Enter a string (upto 19 characters): ";
cin>>astring;

SLIATE 25 J.Pratheepan
Structured Programming (C++) HNDIT

cout<<astring<<endl;
strncpy(bstring,astring,9);
bstring[9]='\0'; //This is important
cout<<bstring<<endl;
}

Output:
Enter a string (upto 19 characters): Superconductivity
Superconductivity
Supercond

Using Pointers to Strings:


Arrays are difficult to manipulate and cannot be used as the return type of a function.
A complementary approach is to use a pointer to reference a string of characters.
To declare a pointer to a string it must be of type char.
E.g.: char* str_ptr;
Here, the pointer str_ptr is able to point to the first character of a string of characters.
We can use a char pointer to reference an array of characters quite easily.
Example:
#include <iostream.h>
void main()
{
char str[11]={"char array"};
char* str_ptr=str;
cout<<str_ptr;
}

Output:
char array

It is easy to assign literals at declaration, requiring only “ ”.


E.g.: char* str_ptr=”A string”;
However, this is not a particularly safe way to create strings, since any attempt to assign a longer
string to the same pointer can cause other data to be overwritten.
The assignment operator may be used at any time after declaration to make one char pointer equal
to another.
Example:
#include <iostream.h>
void main()
{
char a[10]={"spring"}; //Step1
char b[10]={"summer"}; // Step2
char* c="autumn"; // Step3
char* d="winter"; // Step4
//a=b; will not compile
//a={"printemps"} will not compile
c=d; // Step5
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
d="fall"; // Step6
cout<<c<<endl;
cout<<d<<endl;
}

Output:
spring

SLIATE 26 J.Pratheepan
Structured Programming (C++) HNDIT

summer
winter
winter
winter
fall

The following figure shows what happens when we execute the above program.

s
p
Step1
r
a i
n
b g
\0
Step2

s
u
m
m
e
r
\0

a
Step3 u
c t
u
Step5 m
n
d \0
Step4 w
i
n
Step6 t
e
r
\0
f
a
l
l
\0

Using the assignment operator with char pointers should only be used for short term process.
For safe storage of strings arrays are more reliable.
It is possible to use strcpy or strncpy with character pointers to copy data rather than address.
However, this is not a recommended approach since we may overwrite data in subsequent memory
locations.

SLIATE 27 J.Pratheepan
Structured Programming (C++) HNDIT

It will happen if we declare a char pointer to point to a string of one length and then copy a longer
string to it.
Since char pointer only points to the address of one character, what may be in the subsequent
memory locations is unpredictable.
Example:
#include <iostream.h>
#include <string.h>
void main()
{
char* a="fall";
char* b="winter";
char* c="summer";
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl<<endl;
strcpy(b,a);
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl<<endl;
strcpy(a,c);
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl<<endl;
}

Output:
fall
winter
summer

fall
fall
summer

summer
r
summer

A Strategy for Strings:


In practice, we can see that although pointers provide a flexible means for string manipulation, they
can lead to ‘unsafe’ practices in some contexts.
Therefore we are better served by using arrays to store strings, but may use pointers to manipulate
them.

SLIATE 28 J.Pratheepan

You might also like