Jump to content

Pointers and fuctions C

#include<stdio.h>
#include<malloc.h>

void SError(int);
void WriteFile(void(*functionPtr)(int));/* Parameter is function pointer */
int BinaryWriteToFile(void);
void main(void)
{
	void(*functionPtr)(int);
	functionPtr = SError;
	WriteFile(functionPtr);
}
void SError(int errorNum)
{
	static char*err[] = { "Cannot Open File\n", "Read Error\n", "Write Error\n"};
	puts(err[errorNum]);
}
void WriteFile(void(*errorFunction) (int))
{
	int error = BinaryWriteToFile();
	if(error)errorFunction(error);
}
int BinaryWriteToFile(void)
{
	int errorCode = 2;// Fictitiousdisk occurred, report for logging to file via SError...
	return errorCode;
}

I want to be able to modify the call-back example,by inserting pointer parameter into the arguments lists of the function calls as required,to return the error-message test to main(). Display the error-message text on-screen in main()before terminating the program. But i am really struggling with where to start etc. Can someone point anything out to me?

Link to comment
Share on other sites

Link to post
Share on other sites

The code runs as is. What exactly are you having an issue with? Based on your post history, it seems that you are lacking a fundamental understanding of how the code works, so copy and pasting other people's code is not the best way to learn.

 

Read through the functions to understand what each one does and how the program flows.

 

BinaryWriteToFile is supposed to write to a file and return a status code, indicating success or failure. Currently, it is hardcoded to return an error code of 2.

 

WriteFile calls BinaryWriteToFile. If BinaryWriteToFile returns a code other than 0, it calls the pointer function that it received as its parameter. main() sets the pointer function as SError.

 

SError defines a string array, and prints the index of the return value of BinaryWriteToFile. Since BinaryWriteToFile is hardcoded to return 2, SError prints index 2 of the err[] array, which is "Write Error\n", and this is the output of the program.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, gabrielcarvfer said:

Not sure if I got what you meant. You want the main function to call the error callback with the correct error code? Or do what the code already does and print the error message and then return to the main function?

I want the main function to call the error callback with the error code then display it

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Forenman said:

#include<stdio.h>
#include<malloc.h>

void SError(int);
void WriteFile(void(*functionPtr)(int));/* Parameter is function pointer */
int BinaryWriteToFile(void);
void main(void)
{
	void(*functionPtr)(int);
	functionPtr = SError;
	WriteFile(functionPtr);
}
void SError(int errorNum)
{
	static char*err[] = { "Cannot Open File\n", "Read Error\n", "Write Error\n"};
	puts(err[errorNum]);
}
void WriteFile(void(*errorFunction) (int))
{
	int error = BinaryWriteToFile();
	if(error)errorFunction(error);
}
int BinaryWriteToFile(void)
{
	int errorCode = 2;// Fictitiousdisk occurred, report for logging to file via SError...
	return errorCode;
}

I

 

want to be able to modify the call-back example,by inserting pointer parameter into the arguments lists of the function calls

You have to modify it in the declaration & definition to state what arguments. 

4 hours ago, Forenman said:

as required,to return the error-message test to main(). Display the error-message text on-screen in main()

Most output of errors I do in the method so I don’t have to pass data. 


method():

if (doMethod()):

work

else:

print (error! Didn’t do work!!)			4 hours ago, Forenman said:				
			before terminating the program.			
	[code]Scanf()

4 hours ago, Forenman said:

But i am really struggling with where to start etc. Can someone point anything out to me?

If you don’t have a textbook, get one. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×