C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-07-2003, 04:17 AM   #1
Registered User
 
Join Date: Nov 2001
Posts: 3
Question what's wrong with the following code?

Hello ppl, was wondering if you can help with me with some pointer code.

Code:
void Menu()
{
	char* phone;
	char* name;

	GetPhone(&phone);
	GetName(&name);
// use the following printf function to test my code. 
// check bottom of msg for output and what my problem.
	printf("%d - %s - %d - %s", &name, name, &phone, phone);

}

void GetName(char** nameRef)
{
	int ch;
	char buffer[100];  /* buffer used to get input from the user.
			      if the user enters in 105 characters for name
			      the buffer will reset to 5
			    */
	printf("(Name must be between 3-20 characters in length)\nEnter a name to be added to the list: ");
	fgets(buffer, sizeof(buffer), stdin);	// gets input from stdin and puts in buffer
	while ((ch = getchar()) != '\n' && ch != EOF);
	*nameRef = buffer;
	//printf("%d - %s", &nameRef, *nameRef);
	if((strlen(*nameRef)-1 < 3) || (strlen(*nameRef)-1 > 20))
	{
		printf("Name has to be between 3 and 20 characters long.\nPress [Enter] key to be prompted again.");
		
		GetName(nameRef);
	}	
}

void GetPhone(char** phoneRef)
{
	int ch;
	char buffer[50];
	
	printf("Enter the phone number to be added to the list: ");
	fgets(buffer, sizeof(buffer), stdin);
	//while((ch = getchar()) != '\n' && ch != EOF);
	*phoneRef = buffer;
	printf("%d - %s", &phoneRef, *phoneRef);
	if(strlen(*phoneRef)-1 != 9)
	{
		printf("The phone number you entered has too many digits.\nMust be in either xxxx xxxx or xxxx-xxxx formats.\nReplace x with numbers.");
		
		GetPhone(phoneRef);
	}
	if(!((buffer[4] == ' ') || (buffer[4] == '-')))
	{
		printf("Must be in xxxx xxxx or xxxx-xxxx formats. Replace x with numbers.");
		GetPhone(phoneRef);
	}
	if(validate_phone(*phoneRef) != 0)
	{
		printf("The phone number you have entered contains non-numeric data. Try again.");
		GetPhone(phoneRef);
	}
}

int validate_phone(char* phoneRef)
{
	unsigned x;
	for (x = 0; x < strlen(phoneRef); x++)
	{
		if(phoneRef[x] == phoneRef[4])
			break;
		if(!isdigit(phoneRef[x])) return 1;
	}
	return 0;
}
output of test printf statement: & it does go to the new line like that
601364 - paul
- 601368 - 5074-0658

problem is when i put a printf statement in the GetPhone and GetName functions and notice that the reference address of the name pointer is the same as the one for phone reference. problem is whenever i try and get the string entered in GetName i get weird ASCII characters.

What i'm try to do is create a simple phone book using a link list structure to hold the data, as a exercise to try and understand pointers.

Thx in advance

catalyst
catalyst is offline   Reply With Quote
Old 11-07-2003, 04:30 AM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,628
Code:
void GetName(char** nameRef)
{
	int ch;
	char buffer[100];
Buffer is a local variable, which loses scope as soon as this function returns. Anything in this buffer is gone. Your pointer pointing to this buffer ends up pointing at something that no longer exists.

Either make your buffer static, or use dynamic memory allocation (malloc or calloc). If you use *alloc functions, you will need to use 'free' on them before your program exits.

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
what is wrong in this simple code vikingcarioca C Programming 4 04-23-2009 07:10 AM
what is wrong with this code please korbitz Windows Programming 3 03-05-2004 10:11 AM
I cant find what is wrong with this code senegene C Programming 1 11-12-2002 06:32 PM
Anyone see what is wrong with this code? Wise1 C Programming 2 02-13-2002 02:01 PM
very simple code, please check to see whats wrong Unregistered C Programming 3 10-10-2001 12:51 AM


All times are GMT -6. The time now is 12:39 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22