related to pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashwini1680
    New Member
    • Sep 2007
    • 23

    related to pointer

    Hi,
    I have a very basic problem related to strings using pointers in c++
    //////////////////
    i wrote code as,
    int *i_ptr;
    char *c_ptr;

    int i=5;char c='A';
    i_ptr=&i;
    c_ptr=&c;

    cout<<i_ptr;
    cout<<c_ptr;

    As an o/p i get an address of i_ptr but c_ptr doesn't store any addr WHY?

    How sting are stored in memory(1 dim &2dim both) &how pointers access them?

    As when we write a[1] we get addr if a is an array of pointers but in case of strings it returns string n not the address ,how?
  • mschenkelberg
    New Member
    • Jun 2007
    • 44

    #2
    I think it is because a char * is always interpreted as a "C String" , so it will try to output values until it reaches a null character. I think if you do something like &c_ptr, it will show the address

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by mschenkelberg
      I think if you do something like &c_ptr, it will show the address
      Nope. That will get the address of c_ptr but not the address inside c_ptr.

      You are correct though, in that the operator<< for char* assumes a C-style string.

      What you have to do is create an int out of the pointer.
      [code=cpp]

      unsigned int result(c_ptr);

      [/code]

      then you can display result and see the address.

      This syntrax shown is the constructor form for creating a variable. It is equivalent to doing:
      [code=cpp]
      cout << static_cast<uns igned int> (c_ptr);
      [/code]
      Last edited by Ganon11; Sep 28 '07, 05:13 PM. Reason: fixing typos (unsigned, not unisgned)

      Comment

      • ashwini1680
        New Member
        • Sep 2007
        • 23

        #4
        Originally posted by mschenkelberg
        I think it is because a char * is always interpreted as a "C String" , so it will try to output values until it reaches a null character. I think if you do something like &c_ptr, it will show the address
        Hi,
        Thank you.I changed my code.now it's working.Still i am confused with 2d array of char using pointers.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by ashwini1680
          Still i am confused with 2d array of char using pointers.
          Start a new thread. This is a new question.

          Comment

          • ashwini1680
            New Member
            • Sep 2007
            • 23

            #6
            Originally posted by ashwini1680
            Hi,
            I have a very basic problem related to strings using pointers in c++
            //////////////////
            i wrote code as,
            int *i_ptr;
            char *c_ptr;

            int i=5;char c='A';
            i_ptr=&i;
            c_ptr=&c;

            cout<<i_ptr;
            cout<<c_ptr;

            As an o/p i get an address of i_ptr but c_ptr doesn't store any addr WHY?

            How sting are stored in memory(1 dim &2dim both) &how pointers access them?

            As when we write a[1] we get addr if a is an array of pointers but in case of strings it returns string n not the address ,how?


            As the replies I got for the same problem i changed my code as
            cout<<&c_ptr
            but it gives the addr of pointer & not the addr of te var to which it points.
            Where the addr is stored?
            as we initialises pointer with the addr.
            Same if i have

            char *a[]={"Pointer","Co mputer","Scienc e"};
            then
            printing
            a[0],a[1],a[2] gives me strings themseleves.How ?

            because to swap the strings 2 & 3,we use
            char *t;
            t=a[1];
            a[1]=a[2];
            a[2]=t;
            here it takes addresses & if we print a[0],a[1] we get string .I don't understand this whole concept.I am very Congused.Help me.

            Comment

            • shabinesh
              New Member
              • Jan 2007
              • 61

              #7
              Hi,
              *a[],**a,a[][] all means the same in their concept.
              *a and a[] means the same ,so you needn't use any pointer for printing the values in "a",
              char *t;
              t =a[1];
              a[1]=a[2];
              a[2]=t;
              i suppose its a mistake ,it should
              char *t;
              * t=a[1];
              a[1]=a[2];
              a[2]=*t;
              for better refrence, check out 'The C Programming language' by K&R.
              Last edited by shabinesh; Sep 30 '07, 02:11 AM. Reason: add det

              Comment

              • ashwini1680
                New Member
                • Sep 2007
                • 23

                #8
                Originally posted by shabinesh
                Hi,
                *a[],**a,a[][] all means the same in their concept.
                *a and a[] means the same ,so you needn't use any pointer for printing the values in "a",
                char *t;
                t =a[1];
                a[1]=a[2];
                a[2]=t;
                i suppose its a mistake ,it should
                char *t;
                * t=a[1];
                a[1]=a[2];
                a[2]=*t;
                for better refrence, check out 'The C Programming language' by K&R.



                hi,
                I checked my code.It's correct,I have taken it from a book.
                the point of confusion for me is that,
                pointer var always stores addresses.
                so when we write,
                int *ptr;
                int i=10;ptr=&i;
                then if we write,
                cout<<ptr
                we get address of int i stored in ptr.
                but in case of char
                like

                char *ptr;
                char c='a'
                ptr=&c
                or
                char *ptr
                char c="pointer"
                ptr=&c
                Now if we print value of ptr we dont get address.Why?

                if c is a char variable we get some strange characters & if c is a string then we get the string itself. How???

                Comment

                • Laharl
                  Recognized Expert Contributor
                  • Sep 2007
                  • 849

                  #9
                  In C, arrays of characters (also referred to syntactically with char*), are the only available implementations of strings. These strings end with a special character, '\0', and when a char* is passed to <<, it expects one of these C-style strings, even in C++, so it prints until it sees \0. Since in this case, it doesn't, you get weird results as the "array" goes out of bounds.

                  Comment

                  • shabinesh
                    New Member
                    • Jan 2007
                    • 61

                    #10
                    char *ptr;
                    char c='a'------------------(1)bug here
                    ptr=&c
                    or
                    char *ptr
                    char c="pointer"---------------(2) bug here
                    ptr=&c

                    (1).The char "a" is not stored in the var c the integer value is stored since you use a single quotes('a').
                    (2).The char c cannot hold a string it can hold only a character.

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      Almost nothing of what I have read here about pointers and arrays is correct.

                      First an int** does not point to a 2D array and an int***does not point to a 3D array. There are various reasons but the main one is that ther are no multi-dimensional arrays in C or C++. There are only one-dimensional arrays.

                      Read on :

                      First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
                      [code=c]
                      int array[5];
                      [/code]

                      That is an array of 5 elements each of which is an int.

                      [code=c]
                      int array[];
                      [/code]

                      won't compile. You need to declare the number of elements.

                      Second, this array:
                      [code=c]
                      int array[5][10];
                      [/code]

                      is still an array of 5 elements. Each element is an array of 10 int.

                      [code=c]
                      int array[5][10][15];
                      [/code]

                      is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


                      [code=c]
                      int array[][10];
                      [/code]

                      won't compile. You need to declare the number of elements.

                      Third, the name of an array is the address of element 0
                      [code=c]
                      int array[5];
                      [/code]

                      Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

                      [code=c]
                      int array[5][10];
                      [/code]

                      Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
                      [code=c]
                      int array[5][10];

                      int (*ptr)[10] = array;
                      [/code]

                      Fourth, when the number of elements is not known at compile time, you create the array dynamically:

                      [code=c]
                      int* array = new int[value][5];
                      int (*ptr)[10] = new int[value][10];
                      int (*ptr)[10][15] = new int[value][10][15];
                      [/code]

                      In each case value is the number of elements. Any other brackets only describe the elements.

                      Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

                      [code=c]
                      int** ptr = new int[value][10]; //ERROR
                      [/code]

                      new returns the address of an array of 10 int and that isn't the same as an int**.

                      Likewise:
                      [code=c]
                      int*** ptr = new int[value][10][15]; //ERROR
                      [/code]

                      new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

                      Comment

                      Working...