+ 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 ?
13 Answers
+ 4
Adnane Akkouche Paste your code in the code playground, post a link here and I'll see if I can help đ€đ
+ 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
+ 3
Adnane Akkouche Is there a specific code that you have problems with?
+ 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 )
+ 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.
+ 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
+ 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.
+ 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
+ 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 ?
0
Anna yes actually i was working on a calculator that interferes pointers and functions and arrays would you help me ?
0
Anna Here it is x)
https://code.sololearn.com/cY1oAs874Sn3/?ref=app
0
Anna Um in the line 29 it should return a float is it ? a division..
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?