Thread: Problem with passing variables between functions

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    51

    Problem with passing variables between functions

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    const char *seperationChar = "@";
    
    
    void getUsername(char *usernamestring)
    {
    	char *c = usernamestring;
    	int i = 0;
    	int sep = 0;
    	while(*c)
    	{
    		if(strchr(seperationChar, *c))
    		{
    			printf("%d", i);
    			sep = i;
    		}
    		i++;
    		c++;
    	}
    
    	char username[11];
    
    	int j;
    
    	for(j = 0; j< sep; j++)
    	{
    		username[j] = usernamestring[j];
    	}
    	
    	usernamestring = username;
    }
    
    void registerUser(char *usernamestring)
    {
    
    
    	FILE *logon_file;
    	logon_file = fopen("users.txt", "a");
    	fprintf(logon_file, "%s", usernamestring);
    	fclose(logon_file);
    
    }
    
    int main()
    {
    	char name[11] = "martin@1234";
    	getUsername(name);
    	printf("\n%s\n", name);
    	return 0;
    }
    The code is supposed to take a string formatted as "name@password" and return the name part.

    I've run this through the debugger and usernamestring at the end of the getUsername() function is set to the correct value. However, when it returns to main the variable reverts to it's original value.

    I thought the changes would be applied to the original as I passed it to the function as a pointer.

    Any help appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    I think you should declare username variable first in main() and then pass it to getUsername()
    as a second arg, and then deal with it in that method, rather than making a new one and then
    setting it to username...

    Code:
    void getUsername(char *usernamestring, char * username)
    {
    	char *c = usernamestring;
    	int i = 0;
    	int sep = 0;
    	while(*c)
    	{
    		if(strchr(seperationChar, *c))
    		{
    			printf("%d", i);
    			sep = i;
    		}
    		i++;
    		c++;
    	}
    
    
    	int j;
    
    	for(j = 0; j< sep; j++)
    	{
    		username[j] = usernamestring[j];
    	}
    	
    	
    }
    Code:
    int main()
    {
    	char name[11] = "martin@1234";
    	char username[11]="";
    	getUsername(name, username);
    	printf("\n%s\n", username);
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Code:
    int main(void)
    {
    	char user[50] = "testuser@123456";
    	char userTwo[50] = "jabman@999";
    	printf("\n\nUser string created");
    	registerUser(user);
    	printf("\n\nUser registered");
    	
    	printf("\n\nget username");
    	char uname[50];
    	getUsername(user, uname);
    	printf("\n\nThe returned user name was : %s ", &uname);
    	strcat(uname, "@");
    	printf("\n\nThe returned user name is now : %s ", &uname);
    	if(searchForUser(uname)==1)
    	{
    		printf("\n\ntrue User %s found", user);
    	}
    	else
    	{
    		printf("\n\nfail real user find");
    	}
    	
    	
    	printf("\n\nTry and get password");
    	char pass[50];
    	getPassword(uname, pass);
    	printf("\n\nThe returned password was : %s \n\n", pass);
    	
    }
    Code:
    ...
    void getPassword(char *username, char *password)
    {
    	FILE *logon_file;
    	logon_file = fopen("users.txt", "r+a");
    	char tmp[100];
    	int userLength = strlen(username);
    	char tempPassword[50];
    	
    	
    	while(fgets(tmp, 100, logon_file))
    	{	
    		
    		if(strstr(tmp, username))
    		{
    			tempPassword = &tmp;
    			int i; 
    			for(i = 0; i<userLength; i++)
    			{
    				tempPassword++;
    				password = tempPassword;
    			}
    		}
    		
    	}
    	fclose(logon_file);
    }
    ....
    I get:

    The returned password was : ��


    The problem is in the getPassword function shown above.

    Basically, I have a txt file (users.txt) containing passwords and user names
    One username/password combo per line
    Each username seperated from its password with a '@' symbol

    The function should take a username, find the line containing that name
    e.g.
    "thisIsTheNameIWanted@password"

    It should then advance the pointer on the above string by the length of the username string and result in the password parameter being set to the password portion only.

    Sorry if this doesnt make sense. I wrote a detailed description then my connection was lost and I've had to redo quickly. I will be happy to clarify anything.

    Again, I appreciate all the help this forum gives me.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    but then why do you need getUserName() method?
    as i understand your input is username and you need to look for his password in .txt file, is that right?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    password = tempPassword;
    You reassign password inside the function. "password" is a variable name. Initially, it contained a value equal to the value submitted in the parameter -- that is, a memory address of a stack variable (uname) in main.

    Now, password contains a new value -- the memory address of a stack variable in getPassword(). This does not change the address of uname in main(). You cannot do that outside of main.

    You could do it inside main:
    Code:
    uname = getPassword(name, uname);
    in which case getPassword must return an address for a char pointer (ie, be of type char*, not void).

    Or you could use strcpy in getPassword. This will not change the address of "password" (which is the address of uname). It will copy a new value into that address. As long as the new string is not longer than the memory allocated to uname (50), that will be fine.

    So, always remember: if you reassign a parameter inside a function, that parameter no longer refers to the variable that was submitted. (Re)assignment is anything like this:
    Code:
    myvar =  ANYTHING
    Last edited by MK27; 11-22-2009 at 05:05 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Quote Originally Posted by r00t View Post
    but then why do you need getUserName() method?
    as i understand your input is username and you need to look for his password in .txt file, is that right?
    It is a client/server environment. The way I have it setup is that the server is passed a whole "logon" string including username and pasword. From that I need to getUsername to check if the user exits, then if it does i want to get the password

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I forgot to mention that using the return value of getPassword() is not the better choice -- you will have to malloc a heap variable, and this reassignment in main() will leak memory. So go the strcpy() route.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    MK27, once again you come to my rescue. It is really appreciated.

    Not sure if it's just me but the site availability seems bad tonight. Keep getting a database error returned.

    Thanks for the reply.

    Code:
    void getPassword(char *username, char *password)
    {
    	FILE *logon_file;
    	logon_file = fopen("users.txt", "r+a");
    	char tmp[50];
    	int userLength = strlen(username);
    
    	while(fgets(tmp, 50, logon_file))
    	{	
    		if(strstr(tmp, username))
    		{
    			int i; 
    			for(i = 0; i<userLength; i++)
    			{
    				tmp++;   <-----  lvalue required as increment operand
    
    				strcopy(password, tmp);
    			}
    		}
    		
    	}
    	fclose(logon_file);
    }
    I made a few changes but it won't compile. I get the above error. It really is these pointers etc that are my stumbling block. I'm still having to code java at the same time and have coded java for quite a bit so its making it a little more difficult.

    Thanks again.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Martin_T View Post
    Not sure if it's just me but the site availability seems bad tonight. Keep getting a database error returned.
    It's not just you.

    You can't advance tmp because tmp is an array pointer (well, I don't know what the term is, but you get the idea). Its address is permanently fixed, you can't reassign it either -- you will get the same error.

    Which is just as well, because after the first iteration of the while loop, tmp would be pointing userLength characters into it's space, then you'd move it forward more!

    However, pretty sure what you were aiming for doesn't require that for loop at all:
    Code:
    strcpy(password,&tmp[userLength]);
    Make sense?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    thankyou. i think your 100% correct. I dont need the for loop do i. Il try this as soon as am back at comp. Am on phone at the minute.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing by reference to functions
    By avi2886 in forum C Programming
    Replies: 12
    Last Post: 03-03-2009, 01:56 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  4. Problem passing data between functions
    By manutdfan in forum C Programming
    Replies: 9
    Last Post: 12-10-2006, 04:32 PM
  5. Passing variables by reference
    By Tonto in forum C++ Programming
    Replies: 0
    Last Post: 07-16-2005, 12:56 AM