Anonymous
so sets say i call find_size(find_answer);
Javi
The parameter that find_size takes is a pointer to a function. Then, inside, it calls the function that you passed (anspointer). The line answer=anspointer(); is where you are using find_answer
Javi
so the variable answer, inside find_size is where you are storing the return value of find_answer
Anonymous
Javi
You aren't setting equal to the pointer. You are assigning it to the return value of anspointer. When you do anspointer(), note the parentesis, you are actually calling the function
Javi
Setting equal to the pointer would be answer=anspointer; which would give you a compilation error because answer is an int, not a pointer to function
Anonymous
Javi
No problem
Anonymous
Hello guys
Anonymous
Who can help me write a c++ binary search recursive program??
Anonymous
No problem
create_array(find_size(find_answer)); so i can easily do something like this if i make use of function pointers
Anonymous
call a function within a function within a function
Javi
If create_array accepts a function pointer, yes
Javi
But it would be a quite cumbersome pointer declaration
Ilya
Javi
Yes, like that
Anonymous
nice
Anonymous
Yes, like that
does the create_array function has access to it nested variables within the other functions?
Javi
What do you mean "has access"?
Javi
It can call find_size
Javi
But it needs to provide the pointer to find_answer to be able to call it
Javi
Javi
Here create array is getting the return value of find_size
Javi
So, when the code is executing, you are calling find_size with the parameter find answer, and the return value is passed to create_array
Anonymous
like one function returns size , other function returns answer, the create array function takes a parameter that is a pointer to the find_size function and the find size function takes a parameter of a pointer that points to the find answer function
Anonymous
create_array(find_size(find_answer)); so if i call it like this for example, will the create array function have access to what both functions return
Javi
No, here create_array is just receiving the return value of find_size
Javi
find_answer is never in its scope
Javi
Nothing created inside a function is known outside it, but the return value
Javi
As soon as the function call ends, all internal variables are destroyed
Anonymous
Javi
What do you want to do exactly?
Nomid Íkorni-Sciurus
does it make sense to apply functional programming in C ?
Anonymous
What do you want to do exactly?
just practicing, writing a program with all the stuff i have learnt so far, but trying to write a program that uses no global variables
Anonymous
i think i can use a struct to return two values Javi
Anonymous
and that should do the trick
klimi
Rose?
klimi
#ides
Anonymous
no
klimi
Okay
Anonymous
my name is can, new member :)
klimi
Okay okay
klimi
Welcome
Anonymous
thx
klimi
Please read the rules in pinned message
Anonymous
okey
Javi
You're welcome. For quite some years, though I'm not a programmer by education
Anonymous
Javi
I'm a programmer, but I arrived to it the long way. I started as physicist
Javi
Good luck. It's tough, but quite exciting. Any particular area?
Anonymous
Javi
😂😂
Javi
Good attitude
Anonymous
typedef struct{
int answer;
int size;
}CreateArrayValues;
int find_answer(){
int answer;
puts("Do you want to create a String or an Integer Array?");
puts("Type 1 for an Integer Array or 2 for string Array");
scanf("%i", &answer);
clearScreen();
return answer;
}
CreateArrayValues find_size(int(*anspointer)()){
int answer;
answer = anspointer();
if(answer == 1){
puts("How many member do you need in your Integer Array?")
}
else if(answer == 2){
puts("How many member do you need in your String Array?");
}
scanf("%i", &size);
CreateArrayValues Values = {answer, size};
return Values;
}
Anonymous
int create_array(CreateArrayValues(*AnsNSizePtr)(int(*anspointer)())){
CreateArrayValues s = AnsNSizePtr();
int answer, size;
answer = s.answer;
size = s.size;
switch(answer){
case 1:
make_Int_Array(size);
break;
case 2:
make_String_Array(size);
break;
default:
puts("thats is not an options");
return 1;
}
}
Javi
Looks good
Javi
I would remove the function pointer altogether, so that create_array gets a CreateArrayValues array
Javi
And just call it with create_array(find_size(find_answer())
Anonymous
Javi
Much straightforward and easier to read
Javi
Remember that your code will be read by other people, or you in the future
Javi
And you won't have any idea of what you were thinking a couple of month ago when you wrote this
Anonymous
Anonymous
i just know it compiles lol
Javi
Great
Anonymous
Great
one last question lets say i have a function that is ued to print out an array , but i dont know what type of array it is until after runtime, could i use a void pointer as a parameter and cast the it later
Javi
You can, but it would be dangerous. How will you know the type?
Javi
In C++ it's complicated, in C, I think it isn't possible
Anonymous
Anonymous
maybe i need to go back to the drawing board with that one
Javi
You would need reflection, like in java
Anonymous
Javi
Usually, you should pass a parameter that would tell the type