what is the difference between char*& and char*

G

gevadas

sample program

#include <iostream>
#include <vector>
using namespace std;


int find(char*& value,char** arr,int size)
{
for(int i = 0;i < size;i++)
{
if(strcmp(arr,value) == 0)
return i;
}
}




int main()
{
char* word = "two";
char* arr[4] = {"one","two","three","four"};
int res = find(word,arr,4);
cout << res;
}


the above program works fine even if the find was defined as follows


int find(char* value,char** arr,int size)
{
for(int i = 0;i < size;i++)
{
if(strcmp(arr,value) == 0)
return i;
}
}

It will be of great help if someone can tell me the difference b/w
char* and char&*

Thanks

Geaves
 
D

Daniel T.

It will be of great help if someone can tell me the difference b/w
char* and char&*

void exampleA( const char* value )
{
value = "exampleA";
}

void exampleB( const char*& value )
{
value = "exampleB";
}

int main()
{
char* value = "main";
exampleA( value );
assert( strcmp( value, "main" ) == 0 );

exampleB( value );
assert( strcmp( value, "exampleB" ) == 0 );
}

Hope that helps.
 
F

Frederick Gotham

(e-mail address removed) posted:
It will be of great help if someone can tell me the difference b/w
char* and char&*


char*: A pointer to a char.

char*&: A reference to a pointer to a character.

int main()
{
char c = 'a'; /* This is a char */

char *p = &c; /* This is a pointer to a char */

char *&rp = p; /* This is a reference to a pointer to a char */


/* Now "rp" and "p" are the exact same object.
You can confirm this with simple tests: */

if(p == rp) DoSomething();

if(&p == &rp) DoSomething();
}
 
G

gevadas

Thank You

Geaves
Frederick said:
(e-mail address removed) posted:



char*: A pointer to a char.

char*&: A reference to a pointer to a character.

int main()
{
char c = 'a'; /* This is a char */

char *p = &c; /* This is a pointer to a char */

char *&rp = p; /* This is a reference to a pointer to a char */


/* Now "rp" and "p" are the exact same object.
You can confirm this with simple tests: */

if(p == rp) DoSomething();

if(&p == &rp) DoSomething();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top