Re: local/pointers ? HELP

From: Artie Gold (
Date: 05/31/04

Date: Mon, 31 May 2004 15:15:38 -0500

flavius wrote:
> why won't be changed the value stored at *s changed, but only local ?
> how would it look like to modify it's value in main too ?

In C all arguments are passed by *value*; whatever you do to an argument
within a function is gone once the function has exited. If you want teh
value of an argument be mutated by a function you need to pass a pointer
to it.

Unfortunately, that is not the only thing you do not yet understand.
Fortunately, given the word `learn' in the name of this newsgroup,
you've come to the right place.

Hopefully the comments below will help.
> void ch_c(char *c)
> {
> c="world";

Here, you have reassigned `c', a parameter local to the function ch_c();
it now points to a chunk of memory containing the characters: 'w', 'o',
'r', 'l', 'd', '\0'. As `c' is local to ch_c() this change will not be
visable once the function exits...

> printf("inside: %s\n",c);

...but is visable in the above call to printf().
> }
> int main(void)
> {
> char *s;
> s=malloc(6);
> if(!s)
> return 1;

The variable `s' now points to a freshly allocated chunk of memory
capable of holding 6 chars.

> s="hello";

Oops. You've now reassigned `s' to point to an anonymous array of const
char containing 'h', 'e', 'l', 'l', 'o', '\0'. Since you no longer have
a pointer to the memory you allocated with malloc() above, you've
introduced a memory leak.

> printf("%s\n",s);

This line prints `hello', as that's what `s' is now pointing to.

> ch_c(s);

The variable `s' points to exactly what it did before the call to ch_s().

> printf("%s",s);
> getch();

This is a non-standard function. It would be a good idea *not* to use
non-standard functions in posts to a.c.l.l.c-c++.

> return 0;
> }
> //demonstrative purpose only

OK. Let's rewrite this so it behaves the way you want.

#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* for strcpy() */
/* when posting here always show what preprocessor directives you've
    actually used */

void ch_s(char *s) {
    strcpy(s, "world");
    printf("inside: %s\n", s);

int main(void) {
    char *s;
                              are the only standard exit codes */

    strcpy(s, "hello");
    return 0;

Make note of the differences between the code you provided and the code
in my response; hopefully it will be enough to get you started. If not,
by all means post any further questions you might have.


Artie Gold -- Austin, Texas

Relevant Pages

  • Re: Best song written by each guy
    ... If abe reads and responds to my posts, ... I don't understand why there is such a tendancy here for posters to ... On December 8th, when other people were posting the expected "I miss John" memory or acknowledgment of the day, somebody posted a link to a message from Yoko Ono to John's fans. ... And you apparently see nothing wrong with it, since you haven't mentioned it since...all you want to talk about is some "vow" I supposedly took to not respond to you, that I somehow "broke". ...
  • Random BSODs
    ... go for days without a crash, and it might crash multiple times during ... In the advice given in responses to posts like this one bad memory ... under Windows, I get 15 to 20 errors. ...
  • RAM specs problem.
    ... I know there are a zillion other posts all around the web concerning ... even BIOS would load up and the screen stayed dead. ... At that point I thought that the new memory stick was broken. ...
  • Re: Mathew Scott
    ... DMA or direct memory access is obviously faster than buffering the data but requires dedicated access so other programs will have to wait. ... Apparently Agent can't convert. ... And I don't have problems with posts of anyone's but his. ...
  • Re: E minor Pentatonic CAGED scale pattern overlap chart --5shapes
    ... Roger wrote: ... so I'll refresh your memory for you. ... if you recall, who took the time and wrote many long posts, drew up many ... far above the beginner, you might as well shouldn't bother. ...