Results 1 to 13 of 13

Thread: Pointers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    203

    Pointers

    Another Query.


    #include<iostream.h>
    void main()
    {
    void ox(char*);
    char* str="Hello";
    ox(str);
    cout<<str;

    }

    void ox(char* abc)
    {
    cout<<abc;
    abc="meory";

    }
    This displays HelloHello...that means it is actually a call by value that happens(the function is working on a duplicate copy of argument)..while I am passing a pointer to the function..and it should be modifying the contents of the original argument passed.why is this so ?

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    203

    another one

    If I replace the 2nd line of the main()....and make it
    char* str2 insted of char str2[80]....shudnt it work???....it doesnt..Unhandled Exception..at *bcd=*abc


    #include<iostream.h>
    void ox(char*,char*);
    void main()
    {
    char* str="Hello";
    char str2[80];// char*str2;
    ox(str,str2);
    cout<<str2;

    }

    void ox(char* abc,char* bcd)
    {
    *bcd=*abc;
    while(*abc)
    {
    *bcd++=*abc++;
    }

    }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The second one is easy. If you do
    char *str2;
    str2 points to nowhere and dereferencing it will cause an error.
    If you do
    char str2[80];
    str2 is an array and if it is used like a pointer the pointer points to the start of the array - valid memory.

    The first one is more complicated.
    Every string literal you have in your code is hard coded into the exe and has a specific address. This is why char * str = "Hello" is valid. The pointer now points to the memory location of "Hello".
    Then you call the function. Indeed, it is a call by value. The value of the pointer variable (the memory address) is copied to a new pointer variable. The string behind it is still the same, this is why the cout works. Both str and abc point to the string, but they are different variables.
    When you do
    abc = "meory";
    you assign the memory address of "meory" to abc. str still points to "Hello". Therefore, when you then call cout, it outputs "Hello" again.

    To make it work you would have to do
    strcpy(abc, "meory");
    but abc points to read-only memory (a location in the exe file), so this would cause an access violation.
    Here is the code that really works:
    PHP Code:
    #include<iostream>  // the .h ending is no longer used in C++ headers
    #include <cstring> // for strcpy
    using namespace std;

    // although some compilers seem to accept it, it is not a good idea
    // to put function prototypes inside other functions
    void ox(charabc);

    void main() 

    char str[50]; // reserve some memory
    strcpy(str"Hello"); // [b]copy[/b] the string into the memory
    ox(str); 
    cout<<str// should now output "meory"


    void ox(charabc

    cout<<abc
    strcpy(abc"meory"); // [b]copy[/b] the string into the memory

    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Lively Member FantastichenEin's Avatar
    Join Date
    Mar 2000
    Location
    dairy
    Posts
    106
    udit99,

    This is one thing that many books fail to mention for some reason.

    It's to do with the way that C++ works with chars.

    Take the following declaration:
    Code:
    char* str
    C++ treats the above as a null terminated string (c-string) rather than a pointer to char.

    Your code:
    Code:
    #include<iostream.h> 
    void main() 
    { 
       void ox(char*); 
       char* str="Hello"; 
       ox(str); 
       cout<<str; 
    } 
    
    void ox(char* abc) 
    { 
       cout<<abc; 
       abc="meory"; 
    }
    In your code above, C++ assumes that you are passing a null terminated string 'by value'.
    You will need to pass a 'pointer to pointer to char' as below:
    Code:
    #include <iostream>
    using namespace std;
    
    void ox(char **num);
    
    void main() 
    { 
       char *num="100";
       char** pnum=0;
       pnum = &num;
    	
       cout<<num<<endl;
       ox(pnum);
       cout<<num<<endl;
    
    }
    
       void ox(char **num)
    {
       *num="200";
    }
    You are now passing a pointer to a null terminated string. It looks more complicated than it is.
    Hope that helped.
    ****

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yep, that's the second option.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    This is one thing that many books fail to mention for some reason.

    It's to do with the way that C++ works with chars.

    C++ treats the above as a null terminated string (c-string) rather than a pointer to char.
    It's an implementation thing, not a language thing

    char* is a pointer to a char, but
    C style strings are char*'s pointing to an array of char's ending with a null. It's the C string functions that treats the char*
    s as null terminated strings, you could use that char pointer for any other concept, like for instance negative indices:
    char* x=new char[3]+1
    x[-1]=2;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Lively Member FantastichenEin's Avatar
    Join Date
    Mar 2000
    Location
    dairy
    Posts
    106
    kedaman,

    Why does ** work and * doesn't in the examples above?

    thanks
    ****

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Read my post then you know it. Changes to the value of the pointer in ox do not affect the value of the pointer in main.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    in other words, nothing is actually "passed" to a function only a copy pushed on the stack, and then free'd up when the function returns. Passing by reference, is passing a pointer and the dereferenced value can be changed, since it's not on the stack (At least not in the part that will be free'd)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Lively Member FantastichenEin's Avatar
    Join Date
    Mar 2000
    Location
    dairy
    Posts
    106
    I know that....


    ...But why does passing a 'pointer to a pointer to a char' work in the above but passing a normal char pointer doesn't.

    I thought I understood it but it seems not!!


    Cheers!
    ****

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    But why does passing a 'pointer to a pointer to a char' work in the above but passing a normal char pointer doesn't.
    The value you want to change is the char pointer, not a char, and so you need to pass a pointer to it
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    203
    thnx fr the help guys...am still in the process of clearing my pointer 2 pointer concepts...Wanted to ask another thing.. I have access to a book which deals exclusively in Pointers..but its called Pointers in 'C'...so..will it make any difference If I make a slight digression to C to clear my Pointer concepts?...or do the Concepts differ between the 2.?

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    the concept is the same but there's implicit conversion from void*, it needs to be explicit in C++
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width