473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 1242
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marek Malowidzki | last post by:
Hi all, I am writing a component that exposes a C++ library as a .NET component. The approach is somewhat automatic: every library C++ class has its managed C++ counterpart that keeps a pointer...
11
by: Bo Peng | last post by:
Dear List, It is not clear what the title means. :-) Here is the details: I need to manage a big bunch of small objects, like struct{ int a; int b; }obj;
1
by: JuanPedro | last post by:
OS: Windows XP Home SP2 CPU/Ram: Mobile AMD Duron 4, 1GHz / 256MB System Manufacturer: Compaq Presario 730us Using windiag.exe to test my RAM I get a consistent error for the following address:...
16
by: mamo74 | last post by:
Hello. I am administering a SQL Server (Enterprise Edition on Windows 2003) from some month and can't understand what is going on in the latest week (when the db grow a lot). The DB is around...
1
by: Ramprasad A Padmanabhan | last post by:
I have written a simple script to search a word in an array But bsearch does not seem to work here. I know I am missing out something very simple , But I am not able to find out what Thanks...
28
by: Robert Gamble | last post by:
I was taking a look at some of the C puzzles at: http://purana.csa.iisc.ernet.in/~gkumar/cquestions.html and have not had any trouble with any of them except for the first one which is reproduced...
6
by: kalyan.listsubs | last post by:
Hi, I have the below program which will simply write struct employee to a file (binary mode). The problem here is empid is writen to the file but the name (char name) is not written. I am using...
14
by: vikaskumaragrawal | last post by:
hi friends, i have a question plz help me. my question is ... how can i find middle node of linked list in single traverse???????
1
by: pilafi | last post by:
void CSVImageIO::Read( void * buffer) { char onedataline; char *tmp=0; double d=0; unsigned short data=0; unsigned short * inptr = static_cast< unsigned short * >( buffer ); unsigned...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.