Member Avatar for garyengle

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

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.

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

Member Avatar for garyengle

Thank you!!! ...Ancient One

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.