Click here to Skip to main content
15,887,273 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: passing pointers to function Pin
Stuart Dootson8-Jun-09 2:43
professionalStuart Dootson8-Jun-09 2:43 
GeneralRe: passing pointers to function Pin
«_Superman_»8-Jun-09 2:53
professional«_Superman_»8-Jun-09 2:53 
GeneralRe: passing pointers to function Pin
Stuart Dootson8-Jun-09 3:22
professionalStuart Dootson8-Jun-09 3:22 
GeneralRe: passing pointers to function Pin
«_Superman_»8-Jun-09 3:40
professional«_Superman_»8-Jun-09 3:40 
GeneralRe: passing pointers to function Pin
Stuart Dootson8-Jun-09 3:43
professionalStuart Dootson8-Jun-09 3:43 
GeneralRe: passing pointers to function Pin
«_Superman_»8-Jun-09 3:48
professional«_Superman_»8-Jun-09 3:48 
GeneralRe: passing pointers to function Pin
hrishiS9-Jun-09 0:34
hrishiS9-Jun-09 0:34 
AnswerRe: passing pointers to function Pin
Stuart Dootson8-Jun-09 2:50
professionalStuart Dootson8-Jun-09 2:50 
himangshuS wrote:
char *str="abc";
fun(str);
cout<<str;

.
.
fun(char *str)
{
str="gggg" ;
}



but="" its="" still="" showing="" abc...why?
now="" if="" i="" change="" the="" function="" definition="" to="" as="" follow
fun(char="" ;
}<="" blockquote="">

You are altering the pointer value that is passed into fun. Like all other value parameters, that updated value is not passed back out to update the thing whose value you passed in.

If fun was change to either of these, then you could alter the pointer. But

fun(char*& str)
{
   str="ggg";
}

fun(char** str)
{
   (*str)="ggg";
}


Hopefully the string literal "ggg" will stick around outside the scope of fun.

You could alter the string that str points at using this:

fun(char* str)
{
   strcpy(str, "ggg");
}


But I really don't think that's safe either, as (in this case), str points at a string literal, which is likely to reside in read-only space.

I see you're using STL streams...why not go the next step and use STL strings as well. Then you can forget the division between pointer values and what they point at. See the code below - str is obviously a value, would you expect it to be updated by this function?

fun(std::string str)
{
   str = "ggg";
}


Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

GeneralRe: passing pointers to function Pin
hrishiS9-Jun-09 0:35
hrishiS9-Jun-09 0:35 
GeneralRe: passing pointers to function Pin
David Crow8-Jun-09 3:16
David Crow8-Jun-09 3:16 
GeneralRe: passing pointers to function Pin
Rajesh R Subramanian8-Jun-09 3:28
professionalRajesh R Subramanian8-Jun-09 3:28 
AnswerRe: passing pointers to function Pin
CPallini8-Jun-09 3:26
mveCPallini8-Jun-09 3:26 
Questionhindi character pattern recognition [modified] Pin
Member 62745948-Jun-09 0:41
Member 62745948-Jun-09 0:41 
AnswerRe: hindi character pattern recognition Pin
Chandrasekharan P8-Jun-09 1:07
Chandrasekharan P8-Jun-09 1:07 
AnswerRe: hindi character pattern recognition Pin
Chandrasekharan P8-Jun-09 1:49
Chandrasekharan P8-Jun-09 1:49 
AnswerRe: hindi character pattern recognition Pin
Alan Balkany8-Jun-09 4:18
Alan Balkany8-Jun-09 4:18 
Questioncode for recognising character pattern in vc++ Pin
prerananit8-Jun-09 0:17
prerananit8-Jun-09 0:17 
AnswerRe: code for recognising character pattern in vc++ Pin
CPallini8-Jun-09 0:20
mveCPallini8-Jun-09 0:20 
AnswerRe: code for recognising character pattern in vc++ Pin
Chandrasekharan P8-Jun-09 0:22
Chandrasekharan P8-Jun-09 0:22 
AnswerRe: code for recognising character pattern in vc++ Pin
Rajesh R Subramanian8-Jun-09 0:30
professionalRajesh R Subramanian8-Jun-09 0:30 
JokeRe: code for recognising character pattern in vc++ Pin
CPallini8-Jun-09 0:40
mveCPallini8-Jun-09 0:40 
GeneralRe: code for recognising character pattern in vc++ Pin
Rajesh R Subramanian8-Jun-09 0:50
professionalRajesh R Subramanian8-Jun-09 0:50 
JokeRe: code for recognising character pattern in vc++ Pin
CPallini8-Jun-09 0:54
mveCPallini8-Jun-09 0:54 
GeneralRe: code for recognising character pattern in vc++ Pin
Rajesh R Subramanian8-Jun-09 0:57
professionalRajesh R Subramanian8-Jun-09 0:57 
QuestionRe: code for recognising character pattern in vc++ Pin
David Crow8-Jun-09 3:18
David Crow8-Jun-09 3:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.