what is the difference between char*& and char*

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

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

  2. Daniel T. Guest

    In article <>,
    "" <> wrote:

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

  3. 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
    Frederick Gotham, Jul 29, 2006
    #3
  4. Guest

    Thank You

    Geaves
    Frederick Gotham wrote:
    > 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, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,047
  2. wwj
    Replies:
    7
    Views:
    539
  3. Santa
    Replies:
    1
    Views:
    1,061
    Mark A. Odell
    Jul 17, 2003
  4. David
    Replies:
    10
    Views:
    5,924
    Richard Heathfield
    Sep 15, 2003
  5. wwj
    Replies:
    24
    Views:
    2,483
    Mike Wahler
    Nov 7, 2003
Loading...

Share This Page