Anonymous
But that doesn't mean this is not possible
Renan
You cannot return an array in C
You return a pointer to the array.
Anonymous
//Declare a NULL int pointer int *a = NULL; //pass it to your function int length = func(a); // check if a is not null if(a) { // whatever you wanna do here } else print out of memory error The function itself int func(int* a) { // do something with a return the length of a }
Emir
I think I made a mistake. Change line 9 to: id* input_ids = malloc(sizeof(id));
why? sizeof(int) = 4 4 * 5 = 20; sizeof(id) —> 20 too
Anonymous
Your function that receives the pointer must call malloc on it
Anonymous
That is the only way to use an array of unknown length
Emir
Renan
why? sizeof(int) = 4 4 * 5 = 20; sizeof(id) —> 20 too
sizeof(id) is 20 because it has 5 int, and size of each int is 4. Then 4 * 5 = 20👍
Anonymous
Your function takes a pointer as an argument
Anonymous
It uses malloc to allocate memory for the pointer
Anonymous
It can then treat the pointer as an array and add data to it
Anonymous
The function must return the length of the array
Anonymous
This is very similar to how printf works
Renan
The function must return the length of the array
It is fixed length array. So the length is already known.
Anonymous
It is fixed length array. So the length is already known.
But the question is asked for an array with unknown length
Anonymous
So your solution does not solve the problem
Renan
So your solution does not solve the problem
He asked for fixed length array.
Emir
He asked for fixed length array.
yeah, after your solution i wondered what if it is unknown array
Anonymous
Always test your changes with -fsanitize=address whenever you're dealing with pointers
NXiss7
Here: https://pastebin.com/6x5vj1jD I hope it is correct. 😅 👍
Keep in mind that you need to free that memory you've just malloc'd. Or you'll be leaking, wasting memory.
Tiakuh_San
Anyhelp here? My program is crashing 😫 it's most probably memory related
Tiakuh_San
Tiakuh_San
Anonymous
/warn screen photo
Tiakuh_San
Its a code image
Anonymous
Anonymous
Read the rules lol
Tiakuh_San
I read and saw nothing about images. How can I post code help if i can't past code?
Tiakuh_San
I tried change by sizeof(int) * x
Tiakuh_San
But still it was crashing
Tiakuh_San
Oh!
Tiakuh_San
Thank you!
Anonymous
I read and saw nothing about images. How can I post code help if i can't past code?
Tell me — what is this about?! * Long code snippets must be posted via a snippet website(links below), posting pictures of code and posting long snippets in the group is not allowed.
Anonymous
Can anyone explain below mentioned statement? while(n-->0)
Dima
It’s not an operator, there are two operators
Pavel
n-- and > 0
Anonymous
It's original value will be changed or not if we have 2 input value for n?
Pavel
n will change
Pavel
it will be decreased by one each loop iteration, after the loop n will be equal to 0
Pavel
(if n not changed inside the loop body, if it changed it can have some other value after the loop)
Anonymous
Thanks guys. Got it now. Was little bit confused.
Anonymous
Guys I am Student I know Basics about C and C++ I want to develop something for increasing my interest and grip on skills . Pls guide me where can i start
Anonymous
😂
MᏫᎻᎯᎷᎷᎬᎠ
Where is the rules breaking here🤔?
Anonymous
Where is the rules breaking here🤔?
"Pls guide me where can i start" -> the pinned post
Pavel
Guys I am Student I know Basics about C and C++ I want to develop something for increasing my interest and grip on skills . Pls guide me where can i start
Do you know any GUI library? If yes, you can try to make a Paint analog for example If not, you can try to make a console game: tic-tac-toe, or some maze game or a text quest
MᏫᎻᎯᎷᎷᎬᎠ
"Pls guide me where can i start" -> the pinned post
Well simple roadmap questions are common around here
Ariana
ikr no one ever read rules
Ariana
vim >ω<
MᏫᎻᎯᎷᎷᎬᎠ
ikr no one ever read rules
I read it Hahah good boy
Ariana
im also super scared when posting something so i reread rules too many times lol
MᏫᎻᎯᎷᎷᎬᎠ
From the very first foot step in this group
Francisco
im also super scared when posting something so i reread rules too many times lol
Common sense is normally enough to not break the rules. Once you've read the rules, you should be able to post coherently
Ariana
just me trying to make sure it's good haha also i usually end up resolving my problem myself when trying to post it
Francisco
just me trying to make sure it's good haha also i usually end up resolving my problem myself when trying to post it
The thing is, to make a good post so people can help easily, you have to craft a minimal, complete an verifiable example. By crafting that example, most of the times you find the problem
Emir
Hey guys, i have a question in C++ language What this the difference 2 definition? int a = 10; int& p = a; ————— int a = 10; int *p = &a; Is there any difference between the two?
Anonymous
But if that works it means p would be a reference to a
Anonymous
Anyway the other one is pretty simple
Anonymous
p is pointing to the same memory address as a
Emir
I wondered and try this code std::cout << &a << std::endl; std::cout << &p << std::endl; And then i saw two object in the same adress
Anonymous
That's what reference should mean
Anonymous
What's the use?
Anonymous
Of making such a reference
Mar!o
References are implemented as pointers under the hood in most compilers...
Mar!o
They are more secure since they require initialization and they can not be null without any tricks... Perfect to pass parameters which should not be null...
Emir
Thanks to answering, so i must use reference mostly isnt that?