a small pointer problem

A

anonymous

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

Emmanuel Delahaye

In said:
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...
 
A

Alex Fraser

anonymous said:
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
 
R

Richard Bos

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
 
I

Irrwahn Grausewitz

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
 
C

CBFalconer

Emmanuel said:
In 'comp.lang.c', (e-mail address removed) (anonymous) wrote:
.... snip ...

'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 ...".
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
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!
 
X

Xingbo G

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

Irrwahn Grausewitz

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
 
F

Foobarius Frobinium

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top