From: Eric <eric@cisu.net>
To: linux-c-programming@vger.kernel.org
Subject: Newbie Trouble with pointers and structures.
Date: Sat, 9 Aug 2003 13:16:34 -0500 [thread overview]
Message-ID: <200308091316.34217.eric@cisu.net> (raw)
Hello:
I am a newbie who is trying to teach himself C programming and I am having
trouble modifying structure contents. I want the function setoption() to take
a pointer to a pointer in a structure and modify it without needing to return
a value (although currently it does). The function works correctly and GDB
shows that *Option has been set to *String. However, when the function
returns the pointer in the structure has not been modified. Here are the
relevant code pieces.
in file vpn.c (where main() is)
structure definition (declared global in vpn.c):
struct {
/* Structure to hold configuration parameters. The options are set to
their defaults below. The options that need to be built will be set in
detdefaultconfig(). The defaults here are mainly to initilize the
pointers and any changes will be overridden by setdefaultconfig(); */
// General Options
char *PPPDevice;
char *VPNHost;
char *LocalIP;
char *RemoteIP;
char *PPPDOptions;
int Delay;
//SSH Specific
char *SSHCipher;
char *SSHLogin;
char *SSHOptions;
int SSHPort;
char *SSHIdentityFilename;
//Files
char *VPNPIDFilename;
FILE *VPNPIDFile;
char *LogFilename;
FILE *LogFile;
} ConfigOptions = { NULL, NULL, NULL, NULL, NULL, 0,\
NULL,NULL,NULL,0, NULL,NULL, NULL,NULL,NULL};
extern char *setoption(char *Option,const char *String);
setoption is called like this from vpn.c:
setoption(ConfigOptions.LogFilename,"/var/log/vpn.log");
in file helpers.c:
char *setoption(char *Option,const char *String){
int achar = sizeof(char);
/* This function takes a pointer to a structure element and sets it to
the given string. Takes care of memory allocation with free and malloc. We
can just give it the option and the string and let it play. Saves time. */
if (Option != NULL){
free(Option);
}
Option = (char *)malloc(achar * strlen(String));
if (Option == NULL){
fprintf(stderr,"Error Allocating Memory in setoption");
}
//Copy the new string.
strcpy(Option,String);
}
After the function returns. GDB print ConfigOptions.LogFilename shows 0x0
address (NULL).Obviously this is what it has been initialized to in the
beginning. I know that
ConfigOptions.LogFilename = \
setoption(ConfigOptions.LogFilename,"/var/log/vpn.log");
will work....but thats not quite how I want to do it. And besides, its longer
and kinda clunky compared to giving it the pointer only once.
According to my knowledge I should be modifying the Pointer in the structure,
but according to gdb It seems that I am missing something.
Any help would be greatly appreciated.
----------------------
Eric Bambach
Eric (at) CISU (dot) net
----------------------
next reply other threads:[~2003-08-09 18:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-08-09 18:16 Eric [this message]
2003-08-09 20:49 ` Newbie Trouble with pointers and structures Glynn Clements
2003-08-09 21:02 ` Eric
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=200308091316.34217.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).