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. 9 1229
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/
"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 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 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
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 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. 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 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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;
|
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:...
|
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...
|
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...
|
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...
|
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...
|
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???????
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |