From: Eric <eric@cisu.net>
To: linux-c-programming@vger.kernel.org
Subject: Re: Newbie Trouble with pointers and structures.
Date: Sat, 9 Aug 2003 16:02:45 -0500 [thread overview]
Message-ID: <200308091602.45482.eric@cisu.net> (raw)
In-Reply-To: <200308091316.34217.eric@cisu.net>
Ok, here is a less complicated post.My other one is lengthy but this SIMPLE
test program illustrates my point and works off the same concept.I know the
test code is messy but it illustrates my concept. I would like to know why
this very simple test program outputs:
test
test
instead of what i would expect:
test
modifed
here is the code.
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
int main();
int modify(char *p);
int main(){
char *p = "test";
puts(p);
modify(p);
puts(p);
}
int modify(char *p){
p = malloc(sizeof(char) * 20);
strcpy(p,"modified");
}
I am sure I am missing some simple concept...but can anyone explain what that
concept is? I have a few thick books on C and cannot deduce what the problem
would be. The code below SHOULD be of the same concept and it produces the
expected result. It's output is
1
2
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
int main();
int modify(int *p);
int main(){
int *p;
p = malloc(sizeof(int));
*p = 1;
printf("%d\n",*p);
modify(p);
printf("%d\n",*p);
}
int modify(int *p){
free(p);
p = malloc(sizeof(int));
*p = 2;
}
----------------------
Eric Bambach
Eric (at) CISU (dot) net
----------------------
next prev parent reply other threads:[~2003-08-09 21:02 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-08-09 18:16 Newbie Trouble with pointers and structures Eric
2003-08-09 20:49 ` Glynn Clements
2003-08-09 21:02 ` Eric [this message]
2003-08-09 22:55 ` Glynn Clements
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200308091602.45482.eric@cisu.net \
--to=eric@cisu.net \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).