471,093 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,093 software developers and data experts.

a small pointer problem

The following program :

#include <stdio.h>
void func ( char * psz )
{
char * sz = "func";
psz = sz;
}
int main()
{
char * psz = "main";
func( psz );
printf ( "\n %s", psz ); return 0;
}

prints 'main'. Is it because a 'copy of' the pointer psz is sent to the
function, also the pointer sz dies after the function - so is it
not wrong to assign it to psz.
Nov 14 '05 #1
9 1188
In 'comp.lang.c', iu*********@yahoo.co.in (anonymous) wrote:
The following program :

#include <stdio.h>
void func ( char * psz )
{
char * sz = "func";
Correct
psz = sz;
Technically correct, but changing the value of a parameter is often a design
error. Remember, parameters are passed by value in C. You can pass the
address of the variable you want to modify,

T o;

f(&o):

or return a new value

T o = f();
}
int main()
{
char * psz = "main";
func( psz );
printf ( "\n %s", psz ); return 0;
}

prints 'main'. Is it because a 'copy of' the pointer psz is sent to the
function, also the pointer sz dies after the function - so is it
not wrong to assign it to psz.


'not wrong' ? Don't you meant 'wrong' instead ? Your post is unclear...

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #2
"anonymous" <iu*********@yahoo.co.in> wrote in message
news:f7*************************@posting.google.co m...
The following program :

#include <stdio.h>
void func ( char * psz )
{
char * sz = "func";
psz = sz;
}
int main()
{
char * psz = "main";
func( psz );
printf ( "\n %s", psz ); return 0;
}

prints 'main'. Is it because a 'copy of' the pointer psz is sent to the
function,
The value of psz (in main) is received by the function. You might see it
more clearly if you substitute int in place of char *:

#include <stdio.h>

void func(int a) {
int b = 42;
a = b;
}

int main(void) {
int a = 5;
func(a);
printf("%d\n", a);
}
also the pointer sz dies after the function - so is it
not wrong to assign it to psz.


It does nothing in this case, but there is nothing wrong in the way you seem
to think. Again, see the example above: the string literal "func" is
effectively no different to the number 42.

Alex
Nov 14 '05 #3
iu*********@yahoo.co.in (anonymous) wrote:
The following program :

#include <stdio.h>
void func ( char * psz )
{
char * sz = "func";
psz = sz;
}
int main()
{
char * psz = "main";
func( psz );
printf ( "\n %s", psz ); return 0;
}

prints 'main'. Is it because a 'copy of' the pointer psz is sent to the
function, also the pointer sz dies after the function - so is it
not wrong to assign it to psz.


Well... yes. That is, there are two psz's in your program, one in
main(), which is in scope inside main(), and one in func(), which is in
scope inside func(). That these have the same name is immaterial; they
have nothing to do with one another, except that you call func() in
main(), and then the _value_ of main()'s psz is assigned to func()'s
psz. Whatever is done to that value afterwards is done inside func(),
not inside main(); by then, the fact that this value originally came
from main() does not matter any more.
In this, pointers are no different from any other object, btw. See also
<http://www.eskimo.com/~scs/C-faq/q4.8.html>.

Richard
Nov 14 '05 #4
iu*********@yahoo.co.in (anonymous) wrote:
The following program :

#include <stdio.h>
void func ( char * psz )
{
char * sz = "func";
psz = sz;
}
int main()
{
char * psz = "main";
func( psz );
printf ( "\n %s", psz ); return 0;
}

prints 'main'. Is it because a 'copy of' the pointer psz is sent to the
function, also the pointer sz dies after the function - so is it
not wrong to assign it to psz.


The parameter psz in func behaves just like an ordinary automatic
variable (like sz, for example). Whatever you assign to it, it
will be lost when func returns. Thus, the call to func is a no-op
in your program.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #5
Emmanuel Delahaye wrote:
In 'comp.lang.c', iu*********@yahoo.co.in (anonymous) wrote:

.... snip ...
'main'. Is it because a 'copy of' the pointer psz is sent to the
function, also the pointer sz dies after the function - so is it
not wrong to assign it to psz.


'not wrong' ? Don't you meant 'wrong' instead ? Your post is unclear...


English usage strikes again. Don't attach the 'not' to the
'wrong', but to the 'is it' phrase. The (ambivalent) meaning is
"is'nt it true that it is wrong to ...".

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison
Nov 14 '05 #6
In 'comp.lang.c', CBFalconer <cb********@yahoo.com> wrote:
'main'. Is it because a 'copy of' the pointer psz is sent to the
function, also the pointer sz dies after the function - so is it
not wrong to assign it to psz.


'not wrong' ? Don't you meant 'wrong' instead ? Your post is unclear...


English usage strikes again. Don't attach the 'not' to the
'wrong', but to the 'is it' phrase. The (ambivalent) meaning is
"is'nt it true that it is wrong to ...".


Ah, thanks! It's clearer like this!

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #7
iu*********@yahoo.co.in (anonymous) wrote in message news:<f7*************************@posting.google.c om>...
The following program :

#include <stdio.h>
void func ( char * psz )
{
char * sz = "func";
psz = sz;
}
int main()
{
char * psz = "main";
func( psz );
printf ( "\n %s", psz ); return 0;
}

prints 'main'. Is it because a 'copy of' the pointer psz is sent to the
function, also the pointer sz dies after the function - so is it
not wrong to assign it to psz.


If we make a little change in the function "func", we will be able to
print out 'func' instead of 'main' in the main.

void func( char *psz)
{
char *sz = "func";
*psz = *sz; /* update what the pointer 'psz' points to */
}

Yes, we passed the value of *psz from main to func, it's the address.
With the address, we are able to change the value it points to.
Nov 14 '05 #8
ty*******@yahoo.com (Xingbo G) wrote:
iu*********@yahoo.co.in (anonymous) wrote in message news:<f7*************************@posting.google.c om>...
The following program :

#include <stdio.h>
void func ( char * psz )
{
char * sz = "func";
psz = sz;
}
int main()
{
char * psz = "main";
func( psz );
printf ( "\n %s", psz ); return 0;
}

prints 'main'. Is it because a 'copy of' the pointer psz is sent to the
function, also the pointer sz dies after the function - so is it
not wrong to assign it to psz.


If we make a little change in the function "func", we will be able to
print out 'func' instead of 'main' in the main.

void func( char *psz)
{
char *sz = "func";
*psz = *sz; /* update what the pointer 'psz' points to */
}

Yes, we passed the value of *psz from main to func, it's the address.
With the address, we are able to change the value it points to.


No. The psz in main points to (the anonymous array resulting from) a
string literal, which is read only by definition. And even /if/ a
certain implementation lets you get away with it, the program would
print 'fain', not 'func'. To do what you intended you'd have to
change the declaration of psz in main to:

char psz[] = "main";

and then in func copy the *complete* string.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #9
ty*******@yahoo.com (Xingbo G) wrote in message news:<68**************************@posting.google. com>...
iu*********@yahoo.co.in (anonymous) wrote in message news:<f7*************************@posting.google.c om>...
The following program :

#include <stdio.h>
void func ( char * psz )
{
char * sz = "func";
psz = sz;
}
int main()
{
char * psz = "main";
func( psz );
printf ( "\n %s", psz ); return 0;
}

prints 'main'. Is it because a 'copy of' the pointer psz is sent to the
function, also the pointer sz dies after the function - so is it
not wrong to assign it to psz.


If we make a little change in the function "func", we will be able to
print out 'func' instead of 'main' in the main.

void func( char *psz)
{
char *sz = "func";
*psz = *sz; /* update what the pointer 'psz' points to */
}

Yes, we passed the value of *psz from main to func, it's the address.
With the address, we are able to change the value it points to.


That example segfaulted for me... :( But this definitely works:

#include <stdio.h>

void func (char **psz);

int main(void) {
char *psz = "main";
func(&psz);
fprintf(stdout, "%s\n", psz);
}

void func (char **psz) {
static char *sz = "func";
*psz = sz;
}

I use a pointer to pointer to char, so I can properly modify psz.
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Bo Peng | last post: by
1 post views Thread by JuanPedro | last post: by
1 post views Thread by Ramprasad A Padmanabhan | last post: by
28 posts views Thread by Robert Gamble | last post: by
6 posts views Thread by kalyan.listsubs | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.