+ 3

C : Best way to alter pointers with functions

I am not sure what the best or recommended way of using functions to calculate and retrieve information is. Especially when you want to return more than one value at the same time(pointers). In the three given examples. I used, global variables | void functiontype with paramater pointer | int *functiontype with parameter pointer Using Global Variables: https://code.sololearn.com/c3UpKSIBAxPJ/?ref=app Using void functiontype with paramater pointer: https://code.sololearn.com/ctw1dnrsNkxi/?ref=app Using int *functiontype with parameter pointer: https://code.sololearn.com/cumXb2oCFF5p/?ref=app Explanation of the code: The programs work all the same. They input an fraction, shorten it and output it. The continue part with the while loop only works with the other compiler I am using outside sololearn. You could also do it with most of it in the main but the Prof. wants it to be split up as much as possible. I want to know which one of this is the best, worst and if there are better methods to do it.

21st Oct 2020, 7:27 PM
Daniel Krasemann
7 Respuestas
+ 2
Coder Kitten thanks for your long and detailed answer. The sscanf part. I already had my problems with scanf and thought about rather using fgets with sscanf.But I guess I am gonna do the jump now. Also you gave really great advice. Thanks 🙏
22nd Oct 2020, 5:45 AM
Daniel Krasemann
+ 1
As you may know variables in C are passed to function by value. This means that a copy is sent to the function, rather than the variable itself. So even if the function modify the copy, the original variable is still untouched. Now you can use pointers to simulate a pass by reference. To do that you need pass the address of the variable, like that: calledFunc( &variable ); and the called function's definition need to have a pointer as argument (of the same type of "variable"). In this way when the function modify the value in the pointer, it modify the value stored in the original variable location. If the variable you want to pass is an array you can pass the address of its first subscript ( &array[0] ) or just the name of the array ( array ) that's because the name of the array is a pointer to its first element. Passing the address of the first element works for passing the entire array because all the array elements are stored in contiguous pieces of memory.
21st Oct 2020, 8:48 PM
Davide
Davide - avatar
+ 1
.... finishing here, there was no space. Once you give to a called function the power to modify variables of the caller function, you don't need to return anything, nor you need to use global variables.
21st Oct 2020, 8:51 PM
Davide
Davide - avatar
+ 1
Coder Kitten 🐈 excuse my ignorance but what do you mean by "buffer"?🤔🙄 I am trying to understand your answer😭 edit: "long static buffer" here it seems like I can put 'variable' instead of 'buffer', but this doesn't mean that I should. "output buffer" no idea then you speak about two buffers that need to exist, but I of course can't follow you. Please show me the truth 🙏🐈🥺
21st Oct 2020, 9:09 PM
Davide
Davide - avatar
+ 1
Coder Kitten so in the code does this buffer looks like an uninitialised variable? I am sorry if I didn't get it😭 There's a tricky part when you say that the buffer comes into existence with the called function, but previously you said that buffers are chunk of memory and that the calling function prepares the memory ( prepares == creates ? contraddiction : IamSorry ) Or does it maybe looks like a pointer to a chunck of memory with no mame? And to prepare means to create the pointer without initializing it, while the buffer comes into existence only when the called function put something into the memory chunk. This seems more likely to be the case. Anyway thank you for trying to make me understand! You are a talented ASCII artist!
22nd Oct 2020, 11:36 AM
Davide
Davide - avatar
+ 1
Coder Kitten OMG you couldn't be more clear! Thank you so much!!! I wish you a life full of kittens!🐈🐈 What about making the input buffer and the output buffer the same thing? Like storing the shorter fraction in the same variable received as longer fraction. Or creating an array of two elements, the first storing the input fraction and the second the output fraction. Maybe you haven't spoke about these possibilities because it is a good practice to keep the two buffer separate for code readability purposes. However they seem to be good solutions in regards of performance.
22nd Oct 2020, 2:03 PM
Davide
Davide - avatar
0
Davide my second code bit uses the void functions to modify based on adress. I accidentally attached my 3rd code bit twice. But I fixed it know. I am just unsure what the recommended way of implementation is. Using void with adress, return methods or global var.
22nd Oct 2020, 5:35 AM
Daniel Krasemann