linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Newbie Trouble with pointers and structures.
@ 2003-08-09 18:16 Eric
  2003-08-09 20:49 ` Glynn Clements
  2003-08-09 21:02 ` Eric
  0 siblings, 2 replies; 4+ messages in thread
From: Eric @ 2003-08-09 18:16 UTC (permalink / raw)
  To: linux-c-programming

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 
----------------------

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-08-09 22:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2003-08-09 22:55   ` Glynn Clements

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).