954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Please help pass struct address to function

I can't seem to pass a struct address to a function. My program "stops working" when it gets to the scanf statement of the getReady function.

/****************************************************
Author: 
----------------------------------------------------------------------
Purpose: This code will explore passing a data structure
address to a function.
****************************************************/
#include <stdio>
#include <stdlib>

//prototypes:
struct things{
	char knife;
	char pen;
	char wallet;
	char keys;
};

int getReady(struct things *t,char x);

void main()
{
char ready;
char r;
struct things *t;

ready =getReady(t,r);
	if (ready == 'y')
		printf("\nLet's go!!!");
}

int getReady(struct things *t,char x)
{
	printf("\nHave you got your wallet? y/n: ");
		scanf("%c",t->wallet);
	printf("\nHave you got your pen? y/n: ");
		scanf("%c",t->pen);
	if((t->knife == 'n') || (t->pen == 'n')){
		printf("\nYou're not ready...");
		x = 'n';
	}
	else
		x = 'y';
	return x;
}
garyengle
Newbie Poster
2 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

You defined your struct, and made a pointer to it, but I don't see an actual struct being declared.

Also, scanf() needs a getchar() after each instance, if you want to keep getting char's from it. Need to pull the newline char, off the keyboard buffer.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

>>scanf("%c",t->wallet);

Scanf() %c requires a pointer to a character, but all you are passing is a char itself. Change t->wallet like this: scanf("%c", &t->wallet);

The the same with other scanf() functions.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thank you!!! ...Ancient One

garyengle
Newbie Poster
2 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: