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
Functions take values from variables and pass them to their inner code as parameters. Please someone correct me if I'm wrong
Raghad
N is not declared as a variable
I declare it in function interface
Anonymous
with 5 to be exact
Anonymous
I declare it in function interface
Yes, but it needs to exist for main
Raghad
n does not exist in the scope of main, just replace n with a number there
I want take a number from user Add int n; in main?
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
Anonymous
Why would you even want to scan into a param
Yeah true, probably a better way is to just use argv. There are multiple ways that come to mind, just thought this might be an easy one
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?
Raghad
Thank you all
Anonymous
i know with qt you can use html and css to do the graphics
Okay but is there an alternative to Qt?
Andrew
Okay but is there an alternative to Qt?
if you want to use c++ there is gtk
Andrew
or , yes you said mobile, in android studio, you can use also c++ for developing application, but is more complicated
Tamah
V
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
Andrew
When should I use a function pointer?
when you have to call a function that changes over time, or maybe inside a struct to act like it is a class
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
When should I use a function pointer?
When you cannot call a function directly
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
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
сумбула
My code is throwing error
Can u describe it?
Sherlock
Here's my code Codeshare.io/5Nwblr
Sherlock
It would have been much easier if I could send a picture
Sherlock
Here's my code Codeshare.io/5Nwblr
Compile Error on line 25,30,33
MAC
Anonymous
happy Easter
сумбула
Compile Error on line 25,30,33
I guess it is because in 30 line you did not insert anything into the map, so your map is empty
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
Please can some help in arrays in c++ ????
What is your problem with them?
Rahul
what does last line (ptr + i) means
Anonymous
What is your problem with them?
I don't understand them very Well
Pavel
what does last line (ptr + i) means
Taking pointer to an student element (as if it's in an"array" of size n, then you take an element on position i)
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...
сумбула
Compile Error on line 25,30,33
try to change x into q
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
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 ?
Pavel
ok I got it but how the marks is store or mark is store to the address of ptr ?
mark is stored in the first bytes of the memory of the student structure. (ptr+i) points to the memory of the struct, (ptr+i)->marks gives you the value of marks of that struct, &(ptr+i)->marks gives pointer to that memory of the mark of that struct (that can be used to read to it by scsnf
Anonymous
thank you so much
👍🏻🌺
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