468,497 Members | 1,897 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,497 developers. It's quick & easy.

what is the difference between char*& and char*

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[i],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[i],value) == 0)
return i;
}
}

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

Thanks

Geaves

Jul 29 '06 #1
3 17148
In article <11**********************@i3g2000cwc.googlegroups. com>,
"ge*****@gmail.com" <ge*****@gmail.comwrote:
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.
Jul 29 '06 #2
ge*****@gmail.com 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();
}

--

Frederick Gotham
Jul 29 '06 #3
Thank You

Geaves
Frederick Gotham wrote:
ge*****@gmail.com 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();
}

--

Frederick Gotham
Jul 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Alex Vinokur | last post: by
8 posts views Thread by pembed2003 | last post: by
3 posts views Thread by Marcin Kalicinski | last post: by
11 posts views Thread by pembed2003 | last post: by
2 posts views Thread by Zorro | last post: by
5 posts views Thread by arnuld | last post: by
5 posts views Thread by Samant.Trupti | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.