Raghad
#include <stdio.h>
int factorial(int n) {
 
printf("write a number to calculate its factorial");
scanf("%d",&n);
 
if (n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main()
{
printf("factorial(5) = %d\n", factorial(n));
    return 0;
}
Raghad
Error:
main.c: In function ‘main’: main.c:26:41: error: ‘n’ undeclared (first use in this function) printf("factorial(5) = %d\n", factorial(n));  
Raghad
Why🙆🏿♀️
Anonymous
the regular tor version is headless. But my application is very small. And most people to use it will likely have tor installed already so it seems redundant to include it again. Im not sure. Im just not happy with either solution 😅 maybe have a small startup check to see if tor is installed and prompt the user for consent to download and install it otherwise
Anonymous
Why🙆🏿♀️
You'r taking adress of a temporary value
Anonymous
Anonymous
Functions take values from variables and pass them to their inner code as parameters. Please someone correct me if I'm wrong
Anonymous
Anonymous
with 5 to be exact
Raghad
Anonymous
You can't use a value that you will still declare
Anonymous
yes then you need a variable and scanf to read the user input into that variable or pass it as command line argument, but you need to adjust your main function signature for that.
Anonymous
Why would you even want to scan into a param
Raghad
Its work! But its repetition the function with out a reslut
Anonymous
Declare a variable
Ask user for a value then make this variable receive it
Then, on printf this value will be copied into the n of the function. That's how it works
Raghad
Anonymous
Hi everyone, I started learning Qt and QML cause I'm developing a mobile application and I wanna use C++ for the logic of the app. Is there another way to do the GUI and still be using C++ for the logic? Is there another way to make my app cross-platform?
Andrew
Raghad
Thank you all
Anonymous
Palinuro
Andrew
Andrew
or , yes you said mobile, in android studio, you can use also c++ for developing application, but is more complicated
Tamah
V
Anonymous
Anonymous
When should I use a function pointer?
Diego
When you want to make a callback function
Diego
Basically a service that will call FuncX at a given scenario, but the service doesn't get to choose what FuncX actually does
You can, then, define a function that is compatible with FuncX's signature (arguments and return type) and then pass a pointer to it to the service, so that in that same scenario, it will call that instead
Diego
Basically any time you want to make a "function socket"
you go like 'Okay so place here whatever you want and I'll make sure to do something with it'
Anonymous
I got :D
Vlad
i.e it's a callback or you want to call a function depending on a condition that you cannot check right here and now
Vlad
Vlad
Or just to reduce amount of ifs in the code. Say you conditionaly call one function or the other for some collection of data. But the condition itself does not change
Sherlock
My code is throwing error
сумбула
Sherlock
Here's my code
Codeshare.io/5Nwblr
Sherlock
It would have been much easier if I could send a picture
Sherlock
MAC
Anonymous
happy Easter
Anonymous
Please can some help in arrays in c++ ????
Rahul
#include<stdio.h>
#include<stdlib.h>
struct student
{
int marks;
char name[30];
};
void main()
{
struct student *ptr;
int n, i;
printf("enter the no. of records");
scanf("%d%d", &n);
ptr= (struct student *)malloc(n * sizeof(struct student ));
for (i=0; i<n; i++){
printf("enter name and marks: \n");
scanf("%s%d",(ptr + i)->name,&(ptr + i)->marks);
}
Pavel
Rahul
what does last line (ptr + i) means
Rahul
Pavel
also &(ptr + i)
This is not exactly correct cut from the code above
&(ptr + i)->marks
taking pointer of marks of this element
mayway
I want to learn c++.. I have no programming background..where should I start...
Rahul
сумбула
Rukiye
Hi guys. Can anyone ask my question please? I watched a video about Arduino Uno project and I have Arduino uno R3 on Proteus. Are they same or not?
Pavel
means it will store the data at array's ith position
Yes
Basically malloc allocated enough memory to store n elements of student.
ptr points to the start of the "array"
the pointer type is student, so if you add a number to this pointer, it will shift in memory on the size of student structure multiplied by the number you've added
Effectively the same as addressing an array
Rukiye
Rahul
Pavel
but since there is no array declare in it then how the date will store in the array or ptr will work as array
Well, let's say your student structure takes 34 bytes of memory to be stored, you entered 3 as n.
malloc will allocate 102 bytes and give you the pointer to the start of this memory.
Then you iterate your loop, on the first iteration you add zero to this pointer. And interpret 34 bytes at the start as if it was a student struct. You read mark into 4 first bytes and name into next 30 bytes.
Then you do the second iteration with i=1. You add one to the address and it shifts it by 34*1=34 bytes, right after memory of the first "student" ends. Then you do the same reading into the memory as if it was int and char[30].
And then do that the third time to fill the third student, memory of which will end exactly on 102nd byte.
When you use arrays, they do the same math under the hood basically.
Rahul
ok I got it but how the marks is store or mark is store to the address of ptr ?
Rahul
Anonymous
Anonymous
Rahul
Rahul
Anonymous
Rahul
Rahul
if any you remember or leave it😅
Rahul
and ones again thank you
Anonymous
Anonymous
RefLink is allowed here?!
Janko Ⓥ
I'm doing some exercises and I want to provide the user with a choice between addition and multiplication of a range.
If I have only two choices, [add/mul], I guess a strcmp() would suffice in this little case, but what if I gave a lot of options? My searches only hurt my head. Is there a straightforward solution?
b
@privracki
I think, u can do the if-else ladder like this
if (strcmp(string, "add") == 0) {
}else if (strcmp(string, "mul") == 0){
}else if (strcmp(string, "etc") == 0){
}else .....
Janko Ⓥ
I could've thought of that myself, I was thinking of something in a switch statement since the else-if hell isn't quite efficient
حسام
Good evening everyone, I’ve been stuck on this problem for a while. I’m supposed to put 3 void functions one for inputting and one for getting the max numbers between 2 arrays and the last one is for printing the array I just got with my function, the input and output parts are manageable but the max numbers one is out of my level atm all I get is garbage data so I could use some help
S s
Hii