string with pointer

 

String initialization and two string can be exchanged or swap without string library function by using string with pointer in C++.
Pointer can be a good option for any string initialization. such as,

without use of pointer string initialization:-

char name[6]= “Rahul”;
strcpy (string1, string2);

with pointer

char *name = “Rahul” ;
string1=string2;
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()
{
  clrscr();
  char *fname="Rahul";
  char *sname;

  sname=fname; // assign fname into sname

  cout<<sname;
  getch();
}
OUTPUT:-
Rahul

But to compare the two string (string == string), it becomes a little complex. We can initialize the string using a char pointer but bring the string library function to the task to be used.

we can understand this from the program given below, where the string has been initialized from the char pointer while those strings have been compared with strcmp

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main()
{
  clrscr();
  char *frst="Rahul";
  char *scnd="Rahul";

  if(strcmp(frst,scnd)==0)
      cout<<" equal";

  else

     cout<<"Not equal";
  getch();
}
OUTPUT:-
Equal

access string value using pointer increment operator

As we know that in C++, array of character is called string and as each element of an array is accessed through the index value, but in the char pointer we use pointer arithmatic (increment/decrement) . i.e.-

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main()
{
   clrscr();
   char *fname="Rahul";
   cout<<"Name: ";
   while(*fname!='\0')
   {
        cout<<*fname;   // display using one by one character
        fname++;    //increament one by one character
   }
   cout<<"\naddress: "<<&fname;  
   getch();
}
OUTPUT:-
 Name: Rahul
 Address: 0x8f7ffff4

The library functions of the above string are derived from c language, as C++ is the updated version of C language so all these library functions can also be used in C++. (As in the programs above). Hence they are also called C string.

In C++ two features have been added by the ANSI committee, one of which is new classes of string. Which are used only in C++ So in c++ you can use both C string and C++ string. Because the programs in this tutorial have been presented in accordance with turbo C++. So here c string has been provided.

The functions of string strcat, strcpy and‌ strcmp are important. but swapping two strings and string initialization, point maybe good choice sometimes.

 get string size

#include<iostream.h>
#include<conio.h>

void main()
{
  clrscr();

  char *name1="Rahul"; // using char pointer
  char name2[5]="Rahul"; // using normal char variable

  cout<<"sizeof char data type    : "<<sizeof(char); // size of data type
  cout<<"\nsizeof variable name  : "<<sizeof(name1); // size of char pointer
  cout<<"\nsizeof variable name2: "<<sizeof(name2);  

 getch();
}
OUTPUT:-
sizeof char data type  : 1
sizeof variable name1 : 2
sizeof variable name2 : 5

As you can see in the program, we have assigned the same string “Rahul” to, two variable name1 and name2. The first statement assigns string to the pointer variable while the second statement assigns string to a normal variable, and as the ouput of the program shows how much space is reserving in string memory.


previous- Function and their type | Parameter type

next-Storage class and their types