You are right.  I forgot to change the a:

 

char* a="yet another"; 

 

so the following works with me:

 

void fun(char **p)

{

char* a="yet another";

*p=a;

}

 

int main(array<System::String ^> ^args)

{

    

      char *p="hellow world";

      fun(&p);

      cout<<p;

}



 

                          - Eng. Ahmed Youssef        -

      Enterprise Solutions Developer And Team Leader

  _____  

From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of
kou ksk
Sent: Thursday, August 23, 2007 11:18 PM
To: [email protected]
Subject: Re: [c-prog] concept of pointer...

 

Hi,
1. First Code won't work because scope of "a" ends as fun ends. As fun ends,
address 
of a becomes unallocated. So it would output garbage. I don't know how you
got
"Hello World". I tried it on Linux (fedora 5, gcc version 4.1.0 20060304
(Red Hat 4.1.0-3) )
If you try same code with integer arrays instead of string, same will be the
result.

2. This is perfectly valid program. Memory allocated for b is still valid in
main (which is 12 bytes in gcc).
Here you are changing value at 2nd location (5,6,7,8 bytes) to 56.

FYI.
char *p="hello";
*(p+1)='j';
//above will give segmentation fault.

char p[]="hello";
*(p+1)='j'
//this doesn't give segmentation fault. 

interesting !!! try and find out why ...:)

kou.

----- Original Message ----
From: Eng. Ahmed Youssef <[EMAIL PROTECTED] <mailto:ahhatem%40gmail.com> com>
To: [EMAIL PROTECTED] <mailto:c-prog%40yahoogroups.com> com
Sent: Thursday, 23 August, 2007 2:59:20 PM
Subject: RE: [c-prog] concept of pointer...

Hi, I didn't write c from a long time but as far as I remember the following
should work:

What you have done is sending a pointer by value, you send a copy of its
value to the function, that is good only if you want to change the value of
the thing the pointer points to, but if you want to change the what the
pointer points to, you have to make a pointer to the pointer and send the
pointer's address by reference to the function .

Hi friends,

Plz help me . I am not good in pointer in c.

#include<iostream>
using namespace std;
void fun(char **p)// a pointer to a pointer , so that you receive the
address of the pointer so you can change it.
{
char a[]="yet another";
*p=a; //Make the value of the pointer to the pointer (meaning the value of
the pointer sent, the address itself) equal to a.

}
main()
{
char *p="hellow world";
fun(&p); //send the address of the pointer.
cout<<p;
system("pause" );
}

I was expecting output is "yet another". but I got "hellow world".

#include<iostream>
using namespace std;
void fun(int *c)
{
c[1]=56;
}
main()
{
int b[]={2,3,5};
int *c=b;
fun(c);
cout<<c[1];
system("pause" );
}
but in case of interger. i was expecting value of c[1] equal to 56. I got
56.

can any one explain it?

------------ --------- --------- ---
Yahoo! Answers - Get better answers from someone who knows. Tryit now.

[Non-text portions of this message have been removed]

[Non-text portions of this message have been removed]

Try the revolutionary next-gen Yahoo! Mail. Go to http://mrd.mail.
<http://mrd.mail.yahoo.com/dc/landing> yahoo.com/dc/landing

[Non-text portions of this message have been removed]

 



[Non-text portions of this message have been removed]

Reply via email to