PDA

Click to See Complete Forum and Search --> : Pointers


udit99
Jan 25th, 2002, 12:12 AM
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 ?

udit99
Jan 25th, 2002, 02:22 AM
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++;
}

}

CornedBee
Jan 25th, 2002, 05:27 AM
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:

#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(char* abc);

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

void ox(char* abc)
{
cout<<abc;
strcpy(abc, "meory"); // copy the string into the memory
}

FantastichenEin
Jan 25th, 2002, 05:29 AM
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:

char* str

C++ treats the above as a null terminated string (c-string) rather than a pointer to char.

Your 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:

#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.

CornedBee
Jan 25th, 2002, 05:31 AM
Yep, that's the second option.

kedaman
Jan 25th, 2002, 06:28 AM
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;

FantastichenEin
Jan 25th, 2002, 06:33 AM
kedaman,

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

thanks

CornedBee
Jan 25th, 2002, 07:59 AM
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.

kedaman
Jan 25th, 2002, 08:48 AM
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)

FantastichenEin
Jan 25th, 2002, 08:54 AM
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!

kedaman
Jan 25th, 2002, 09:29 AM
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

udit99
Jan 25th, 2002, 09:39 AM
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.?

kedaman
Jan 25th, 2002, 10:15 AM
the concept is the same but there's implicit conversion from void*, it needs to be explicit in C++