Strings



How to access string through pointers. I am using turbo C++ and i am having the doubt. please tell me simple example to understand the relationship between pointer and strings


#include<iostream.h>
#include<string.h>
#include<conio.h>
class string
{
char name[100];

public:
string()
{
strcpy(name,"Aab ");}

string(char a[]) //here i am not mentioning the array size how it works??? Please explain the concept behind this
{
strcpy(name,a);
}
void display()
{
cout<<name<<endl;
}
string operator + (string);
int operator < (string);
};

string string::operator +(string c)
{
string temp;
temp=strcat(name,c.name);
return temp;
}
int string::operator <(string c)
{
return strcmp(name,c.name);
}


void main()

{
clrscr();
string a,b("Abc");
a.display();
b.display();
string c=a+b;
c.display();
if(a<b)
cout<<"S1 is greater";
else
cout<<"String is less";
}
here i am not mentioning the array size how it works??? Please explain the concept behind this
strcpy(...) and the other str...(...) functions are looking for the terminating 0 in the data that the char pointer addresses.

If there is no terminating 0 you will have an out of bounds operation -> undefined behavior.

A string like "Abc" implicitely contains the terminating 0.


As a paramter char a[] and char *a are equivalent.
Topic archived. No new replies allowed.