what is the difference between char*& and char*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gevadas@gmail.com

    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","t hree","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

  • Daniel T.

    #2
    Re: what is the difference between char*&amp; and char*

    In article <1154167013.154 864.185580@i3g2 000cwc.googlegr oups.com>,
    "gevadas@gmail. com" <gevadas@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.

    Comment

    • Frederick Gotham

      #3
      Re: what is the difference between char*&amp; and char*

      gevadas@gmail.c om 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

      Comment

      • gevadas@gmail.com

        #4
        Re: what is the difference between char*&amp; and char*

        Thank You

        Geaves
        Frederick Gotham wrote:
        gevadas@gmail.c om 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

        Comment

        Working...