+ 1

C Language. (( POINTERS ))

Hello guys .... So.. i learned Pointers in C and i get the syntax but when i try applying what i have learned i block .. any help ?

16th Feb 2019, 10:35 PM
Dynno
Dynno - avatar
13 Antworten
+ 4
Adnane Akkouche Paste your code in the code playground, post a link here and I'll see if I can help 🤓👍
17th Feb 2019, 9:02 AM
Anna
Anna - avatar
+ 4
That's just the way Sololearn is implemented I guess. Sololearn checks if a program needs input, requests all input from the user, sends the input to the server, computes the input, sends the output back to your phone (or browser). Whereas if you run the code in a console, it will pause every time it expects input
17th Feb 2019, 10:20 AM
Anna
Anna - avatar
+ 3
Adnane Akkouche Is there a specific code that you have problems with?
17th Feb 2019, 6:13 AM
Anna
Anna - avatar
+ 2
blackwinter yes that is what i thought when i was learning it but the problèm is that i need them in my university (algorithm )
16th Feb 2019, 11:07 PM
Dynno
Dynno - avatar
+ 2
I've answered on a couple of other posts on this topic. I've found that pointers are really only necessary if you're making API for other people to use, or you are making a program that needs to dynamically allocate things because the user requests them during run time. Even then, you can get by without needed pointers by using smart pointers. The best practice with them is to make your own data structure, like a linked list or custom container class.
17th Feb 2019, 1:23 AM
Zeke Williams
Zeke Williams - avatar
+ 2
The way I understood it, the block from line 25 to 30 is for addition? If you want to use it for division, you need to make a couple of changes since you're storing the result in an int variable (result) which cannot hold decimal fractions. You can change it like this: float result_f; // variable for return values of functions with return type float if (choice ==0){ // division printf("enter two numbers :\n"); scanf("%d %d",&x, &y); result_f=op1[choice](x,y); printf("the result is = \n %f",result_f); } Within the function divide(), change c=(a/b); to c=((float)a/b); since a and b are integer parameters
17th Feb 2019, 9:48 AM
Anna
Anna - avatar
+ 2
Not sure if I understand you correctly. Note that you have to enter all numbers immediately after you run the program. That's kind of a restriction in Sololearn. So if you want to enter 0 for division and the numbers 3 and 14, you have to enter 0 3 14 (each input in a separate line) as soon as you run the program.
17th Feb 2019, 9:56 AM
Anna
Anna - avatar
+ 1
I just made a couple of changes and it seems to work fine for addition: - Make sure to use the comparison operator (==) instead of the assignment operator (=) in all if clauses. - In line 28, use the function pointer op (return value: int) instead of op1 (return value: float). - The format specifier in line 29 (%f) should be replaced with %d. - Not sure about the standard parameter int b=0 in line 9, but when I removed the =9, it worked
17th Feb 2019, 9:37 AM
Anna
Anna - avatar
+ 1
Anna Aaahh i get it now thank you 😊😊 but when i enter 0. It tekes me directly to choice 1 so what's the error ?
17th Feb 2019, 9:51 AM
Dynno
Dynno - avatar
0
Anna yes actually i was working on a calculator that interferes pointers and functions and arrays would you help me ?
17th Feb 2019, 8:37 AM
Dynno
Dynno - avatar
17th Feb 2019, 9:23 AM
Dynno
Dynno - avatar
0
Anna Um in the line 29 it should return a float is it ? a division..
17th Feb 2019, 9:39 AM
Dynno
Dynno - avatar
0
@Anna sorry if i'm wasting your time 😅 But i am working with my coputer. (Dev c++) and not is solo playground why there is a difference?
17th Feb 2019, 10:10 AM
Dynno
Dynno - avatar