what is the difference between char*& and char*

Discussion in 'C++' started by gevadas, Jul 29, 2006.

  1. gevadas

    gevadas Guest

    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
     
    gevadas, Jul 29, 2006
    #1
    1. Advertisements

  2. gevadas

    Daniel T. Guest

    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.
     
    Daniel T., Jul 29, 2006
    #2
    1. Advertisements

  3. 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();
    }
     
    Frederick Gotham, Jul 29, 2006
    #3
  4. gevadas

    gevadas Guest

    Thank You

    Geaves
     
    gevadas, Jul 31, 2006
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.