What is pf apply in c? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a function
void join(table first, void(*pfapply)(const char *inp1,const void *inp2,void *addn),const void *addn);
here first is a instance of structure table as below
struct table
{
char inp1;
int inp2;
}struct table *first;
Now how to add this extra variable "addn" in this "join function"
In my symbol table i have two values inp1 and inp2..how to add addn to my structure using the above function?
I need to bind addn to the two variables inp1 and inp2...

The addn is probably just the API being friendly and providing support for an additional parameter to the pfapply() callback.
This is a sign of good design, since it allows the callback to access some of its data without having to make it global.
If you don't need any additional data, just ignore the argument inside your apply-function, and call join() with NULL as the third argument.
You don't need to change anything in the declaration of table, all is fine.

Related

How to unit test this c function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a simple c function,
uint_8 tempM_main()
{
if(g_variable1 > g_variable2)
tempM = g_variable1;
else
tempM = g_variable2;
}
where g_variable1 and g_variable2 are the global variable.
How can i able to unit test this scenario?
equivalent class and boundary class need to apply for this?
I am using Tessy tool for unit testing.
It has undefined behavior if you use the return value, because it says it should return an uint_8 but does not return anything. Before it can be tested in a sensible way, this should be corrected. Either via changing the signature or returning a value.
Let's take the case where you have added a return statement. You're obviously using globals, so you can test it like this:
g_variable1 = <value1>
g_variable2 = <value2>
assert(tempM_main() == <value3>);
assert(tempM == <value4>);
If you change the signature to return void, just remove the first assert and replace it with only the function call.

How to add new element to const char ** array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
What's up?
I am very interested in the question of how to add a new element to my array
uint32 glfw_extension_count = 0;
const char** glfw_extensions;
glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_extension_count);
return glfw_extensions;
I just need add to 'glfw_extensions' VK_EXT_DEBUG_UTILS_EXTENSION_NAME
The (great!) documentation says:
Pointer lifetime
The returned array is allocated and freed by GLFW. You should not free it yourself. It is guaranteed to be valid only until the library is terminated.
So clearly you can't do the necessary re-allocation. So you must allocate a new array, copy the data in, and then append your additional entry. I would assume that you can safely copy the string pointers themselves, didn't see a lot of discussion about that in the documentation page so it might need further verification.

C - Passing a single variable instead of an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm facing an issue on a PIC from Microchip but my question is more general. This will help me check if it is an issue from my code or from the debugger of MPLAB X which is sometimes buggy.
So here is my question, let's say I have this function:
int test( int *array )
{
int a = array[0];
return a;
}
Now, if I want to pass an array it's working, there is no issue. But let's say I want to use a single variable instead of the array like this:
int main()
{
while(1)
{
int test_variable_1 = 8;
int test_variable_2 = test( &test_variable_1 );
}
return 0;
}
I did not use an array but for me this should work as expected since the test_variable_1 is like an array of size of one int.
EDIT QUESTION: So, is it possible to pass a single variable like this (using the pointer with &) instead of a real array into an function? Also, is it a good/bad practice?
Thanks in advance and don't hesitate to tell me if I'm unclear.
Have a good day!
Adrien
So, is it possible to pass a single variable like this (using the pointer with &) instead of a real array into an function?
Yes. In fact C doesn't even know that you passed an array. For details, see Do pointers support "array style indexing"?
Also, is it a good/bad practice?
It's fine. Though ideally the function should take the number of items as one of the parameters passed.

How can I write an array of arraylist in the uml? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is my array of arraylist: Arraylist[] a = new Arraylist[SIZE];
I’m struggling with writing it in my UML diagram, how can I write it?
With or without the <>?
The simplest way is to define it this way:
a is of type Arraylist (after the colon) with multiplicity 0..* and its default (after the equal sign) is Arraylist[SIZE].
As commented by #bruno the default value is a bit of interpretation. UML basically should be held language agnostic, but sometimes you just want to point out implementation details (for whatever reason). So you can add the new keyword right in front of the Arraylist[SIZE]. What that actually means is language dependent (and so out of a general scope I like to stick to).

Why the const value can be changed using pointer inside the stack.? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The const value can be changed inside the stack using pointer.But we can not modify the value of a const variable when it is defined globally.Because it goes to the RO data section.So this value is protected.But inside a stack the const variable doesn't provide protection.Why..?..If it is not providing any protection means then what is the use of const value inside the stack.?.
When you use the const keyword you just tell the compiler to throw error when you try to assign this variable, but it doesn't protect memory.

Resources