Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Forenman

Pointers and fuctions C

Recommended Posts

Posted · Original PosterOP
#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 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 post
Share on other sites
9 minutes ago, Forenman said:

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?

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?

Link to post
Share on other sites
Posted · Original PosterOP
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 post
Share on other sites
41 minutes ago, Forenman said:

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

Here is how I would do it:

 

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

// you can keep your generic error function
void SError(int errorNum)
{
    static char*err[] = { "Cannot Open File\n", "Read Error\n", "Write Error\n"};
    puts(err[errorNum]);
}

//but passing parameters with function pointers is weird and I can't really recommend
// so I would use macros to declare a function for each individual error calling 
the previous function with the correct parameter
#define SErrorI(x) void SError_##x(){SError(x);}
#define SErrorPtr(x) SError_##x

//declaring the new functions with different fixed parameters
SErrorI(0)
SErrorI(1)
SErrorI(2)

//Create an array with pointers to each function with each different parameter
static void(* const SErrors[])(void) = {SErrorPtr(0), SErrorPtr(1), SErrorPtr(2)};

int BinaryWriteToFile(void)
{
    int errorCode = 2;// Fictitiousdisk occurred, report for logging to file via SError...
    return errorCode;
}

//you can't write the contents of an argument, so you need a point of a pointer to be able to change the callback address
void WriteFile(void(**errorFunction)())
{
    int error = BinaryWriteToFile();
    if(error)
        errorFunction[0] = SErrors[error];
}

int main(void)
{
	//here I create and initialize an array with a single function pointer
	void(*functionPtr[0])() = {};
	WriteFile(functionPtr);//then we pass the address to the array, not the function pointer itself
	functionPtr[0]();//then call the function pointer changed by the previously called function
}

 

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. 


Don’t take any of my posts seriously. 

 

LTT Fan Fiction:

 

PC game list: 

 

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


×