📚 Learn how to solve problems and build projects with these Free E-Books ⬇️ C++ Lambdas e-book - free download here: bit.ly/freeCppE-Book Entire Object-Pascal step-by-step guide - free download here: bit.ly/FreeObjectPascalEbook 🚀📈💻🔥 My Practical Programming Course: www.codebeautyacademy.com/ Experience the power of practical learning, gain career-ready skills, and start building real applications! This is a step-by-step course designed to take you from beginner to expert in no time! 💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10). Use it quickly, because it will be available for a limited time.
Thank you. This is a very nice and concise explanation and examples of pointers returning multiple values. I'm learning a lot from these tutorials. For those interested in expanding slightly deeper into this tutorial: The getMinAndMax function assumes the min and max has been set to the min and max of the current array that's sent to the function, but is not guaranteed. For example, in a larger program, if the function has already been used with a different array with a smaller min and a larger max then in the current array sent to the function, then the values returned will be incorrect, unless the min and max is reset to the first element of the current array in use every time the function is called. There are two ways that this could be addressed, such as setting the initial min and max inside the function. Another way could be to use the current getMin and getMax functions as shown below: void getMinAndMax(int numbers, int size, int* min, int* max) { *min = getMin(numbers, size); *max = getMax(numbers, size); }
Did you need to use a pointer? Would the & operator have worked in the function declaration, removing the need for pointers in the function definition?
If you remove the & (or the addresses of the variables) operator from that function you would't be able to return those two values at once, you probably would had to return an array, for example that contains those two values :P
Using a reference in the parameters and writing the body accordingly also works. void getMinAndMax(int[] numbers, int size, int& min, int& max){...} No pointers required.
How to return multiple values from a C/C++ function? Usually, a function can only return a single value, using the return mechanism. If you try to return more than one value from a function, only one value will be returned that appears at the rightmost place of the return statement. However, you can return multiple values from the function using the pointer, array, or structure. Using Pointers You can use pointers to return more than one value from a function by passing pointers as function parameters and use them to set multiple values which will then have visibility in the caller function. Using Array If you want to return multiple values of the same data type from a single function, then using an array is best suited because when an array name is passed as an argument then its base address is passed to the function so whatever changes made to the array is changed in the original array. Using Structure Another way to return multiple values from a function is by using structures. Structure is a user-defined datatype in C that can hold several datatypes of the same or different types. The idea is to create a structure variable containing all required data types as its members and return that from the function. We can then retrieve the values from the structure variable inside our caller function.
previous video you said arrayname acts like a first elements pointer. isn't 5:10 passing numbers array is like passing a pointer ? but function waits for a value. it becomes confusing again :/
Misleading title tho. The function is not exactly "returning" multiple values it is just assigning the values of min, max referenced through the pointers and passed as an argument to the function.
Thank you this helped me understand pointers better. Is there more to them that I should know? Except for the fact that they allow you to access and edit the original address of a variable.
When I try to calculate the size of numbers[ ] inside the getMin( ) using sizeof(numbers) / sizeof(int), it gives that the minimum of numbers[ ] is always numbers[0], i.e. the for loop in getMin( ) is skipped. However, when I send the size as a variable getMin( ), although I found size using the same way, it compiles perfectly. Why do I get this bug?
Hey, I just started watching your channel and wanted to say thank you for the amazing help you provided. My teacher uses c instead of c++ but I notice that only things like cout and cin are different for now. In c the library is stdio.h tell me if Im right that it is about the same thx!
They are very similar. They have very similar syntax and code structure and both are close to the hardware - so minor differences here, it should be easy to switch between them. The main difference is that C is a procedural language and it doesn't support OOP, while C++ is both procedural and object-oriented, and because of that, I'd give C++ an advantage over C, but neither one is a bad choice. 🤗
C++ is moving away from raw pointer semantics, if you want an optional value, you should be using the optional container. If you don't want an optional, you should be passing by T&... Also for the specific examples that you given int& would have been better because it avoids the SEG fault when I you pass null to the min and max value. (Also passing by reference rather than passing by pointer makes it so that the data does not need to be dereferenced in the function and that the address does not need to be passed)
Nice tutorial, it reminds me out variables from C# and can someone tell me how does System("pause>0") works? it's bit confusing cuz compare is passed as string literall
system("pause"); is going to pause your console window until you press any key, and if you add >0 to it, it is going to remove junk text You can remove >0 in the program that you're writing, and check out the behavior of your program, and then remove the remaining system("pause") part and check again system("pause") is a platform-specific hack though, and it is not exactly recommended to use. I use it to pause the console window. However, a program that has done its job should quit and return its resources back to the computer, but in this particular situation, we're using system("pause") so that we can pause it and see the output in the console before it closes.
HELP ME PLEASE! 😢 how can i create a program in c++ that is focusing on multiple conversions. (with the use of goto loo, if else, switch) i think these 3 will be use. BIG THANKS IF YOU WILL BE ABLE TO HELP ME ❤️ . THANKS ALSO IF NOT
Great, thanks! Just one question: Can someone explain me how the program assigns the number of elemts of the array to variable size? I don't see it and I don't understand how the for functions because size is never "initialized" or is never given a value. Tkanhs :)
Size is the parameter and 5 is the argument. Meaning, the function declaration is telling us we require an int called size. However, when the function is invoked/called you put the actual int there. CodeBeauty did that. They wrote 5 when calling the function within main. When they write 5 in the argument 5 is then stored within the size variable declared in the function parentheses.
Great channel. You explain very well !! One question: If min and max are also arrays. How do return min[ ] and max[ ] using numbers[ ] as a parameter? Thanks!.
@@robertogomezbarrron4968this by the way is why you should use the STLs array container rather than a pointer, all C style arrays are disappointed to an address that just happens to have the next few words after it allocated as well. (Declaring arrays with [] is a little bit different but still essentially the same thing) There is no distinguishable difference between a pointer to an array and a pointer to a single value in c...
When I have a function like void getMinAndMAx() I pass the parameters by refference (int numbers[], int size, int &min, int & max), and when I call it I write getMinAndMAx(numbers, size, min, max). Is it the same thing as you did? Which method is preferable to use?
Yes, it is. In your version as in the original one the same things happen behind the scenes. Even the generated code is identical in both cases. The version with references is preferable.
In my opinion, using pointers is actually preferable to references in that scenario. The reason why I say that is twofold. First of all, it's very clear that you need to pass in a memory address, which will make the caller think twice about whether or not the data at that address will end up being modified inside the function. Second, it allows you to pass in a null pointer, which could be useful in a function like this. Maybe you don't actually care about the max value and you only want the min value. Anyway, that's my two cents on the matter. Both are valid approaches.
One another great tutorial about pointers. But I should warn you a possible crash in your code. Sometimes we can create an array with only 1 element. if we pass this array to your functions then your code will crash. So you should check array size first. Let me explain by code ; int main() { int myArray[] = {76}; cout
If the array has only one element, won't it have a size of 1? In that case, this code for (int i = 1; i < size; i++) won't run beyond loop initialisation, because the loop variable i is initialised to 1, and i is not less than size (1 is not less than 1), so the "for" loop just won't run. In that case the code will return the only value in the array, as the variable min was initialised to the value held in numbers[0].
That wouldn't work. When you pass an array into a function, it gets coerced into a pointer. Trying to do sizeof(array) would just yield the size of a memory address, not the total size of the array. Even if that did work, since we're no longer passing the length of the array into the function, there would be no way to guarantee that the array wasn't empty, meaning that array[0] could very well seg fault. TL;DR: Good idea in theory, but it wouldn't work in practice.
Hi i am really struggling with a code, Lets say u have a txt file which looks something like, Noun Dog An animal ** Adj Pretty Something beautiful ** Noun Quote A value ** Noun Cinq Something.. ** How can we make the program to display only the words that has a “Q” in it but is not followed by a “U” Result should be something like - “cinq”
Excellent tutorial. However, in the function call "getMinAndMax", the ampersand indicating address is redundant as both "min" and "max" are the addresses of the arrays already.
Really? Why do you say that? min and max both contain the value of numbers[0], which is 5. If you don't pass &min and &max then the getMinAndMax function will receive the address value of 5 in the pointers to min and max.
Actually that is not what is called returning multiple values from a method, just passing multiple references and changing them. Returning multiple values can be done with something like 'tuple' or a custom made class/struct.
You're right. Firstly, the function used in the example is of the void type which indeed means no return, then we cannot accurately talk about multiple return.
5 hours every day is a lot of time, and I'm not sure if it is maintainable long term. I'd aim for 2-3 hours per day, stay consistent, do that for a year and then start looking for junior dev positions, and continue learning. You are not going to be a hell of a developer after one year, but you might get a chance in a company, and if you do, find yourself a good senior dev who is willing to help and guide you and don't let go! Don't chase the money or different benefits that companies offer if you are just starting. Free food, team buildings, remote work, gym memberships, playing games at work... 🚫 The only important thing at this stage is to get experience and knowledge, and once you have that, you'll have all the benefits and money that you want. But, it'll be a marathon, not a sprint. 😉
Using a "struct" with min and max, but I think the (real) return value will be a pointer to the struct variable, I think it is not realy seen as a pointer.
I followed it through but it sounded like a complicated way to find the max and min values in an array. Is there an easier way to do it, or were you just showing us how to do it using pointers?
I think she used this example just to demonstrate how to use pointers. In this case, we can find max value or min value without using pointers. Please correct me if I am wrong. void getMinMax(int input[]) { int min = input [0]; int max = input[0]; for (int i = 1; i < sizeof(input); i++) { if (input[i] < min) min = input[i]; if (input[i] > max) max = input[i]; } cout
My answer is a short program in c++ this: #include class CodeBeautyMinMax { private: int min, max; /* Disallow object creation without an parameter */ private: CodeBeautyMinMax(){} public: CodeBeautyMinMax( int *paramArr ) { /* min and max should be zero */ this->min = this->max = 0; this->findMinAndMax( paramArr ); } private: void findMinAndMax( int *paramArr ) { if ( paramArr == NULL ) return; /* first element of array is now stored into min and max variable*/ this->min = this->max = *paramArr; /* Enter the While-Loop again if you do not find a 0 "Zero" */ while( *(++paramArr) != 0 ) { /* To reach the next element of the array - This does the trick: ++paramArr */ /* This is the same as above into the Video - but shorter =) */ this->min = ( *paramArr < this->min ? *paramArr : this->min ); this->max = ( *paramArr > this->max ? *paramArr : this->max ); } } public: int getMin(){ /* Get only the min-value */ return this->min; } public: int getMax(){ /* Get only the max-value */ return this->max; } public: void getMinAndMax( char* buf ) { /* It would work with sstream as well - but I am a old school guy =) Combine string with variable like this */ sprintf( buf, "min is: %d max is %d", this->min, this->max ); } }; int main(void) { int arrValues[7] = { 1, -5, 14, 6, -20, 99, 0 }; int *ptr = arrValues; CodeBeautyMinMax *cbmm = new CodeBeautyMinMax( ptr ); std::cout
Can someone explain the purpose of pointers I’ve watched most of her videos on them (maybe haven’t gotten to the one with the answer) But pointers, so far, seem like just another option instead of nested if statements. Why use them? I can pass multiple if statements in a single function and return the value. Why go through its address and dereferencing them
Hey Saldina, I'm new to c++ and your videos are really helping. I have a problem with this example as i do not fully understand the need for pointers in it. Here is my own code: #include #include void maxminNum(std::vector nums){ //for max: int max = nums[0]; int min = nums[0]; for(size_t i{1}; i < nums.size(); i++){ if(nums[i] > max){ max = nums[i]; } if(nums[i] < min){ min = nums[i]; } } } int main(){ std::vector vect {3, 5, 1, -3, 8}; maxminNum(vect);
return 0; } Please explain why my code which passes by value may be affected and why we should pass by reference as the code works as it should.
Hey. Your code works good for min and max but if you want use max and min values in function main you can't. If you declare variable in function, this variable on the end of loop will disappear. So if you need use in main or in another function your max or min (and you don't want working with return types in function maxminNum) you need declare variables max and min in main function and send pointer like a parameter to your function.
// using a pointer to pull data out of a function.... // using a pointer to pass a reference to an array #include using namespace std; int* func_GetArray(int numbers[], int size, int min, int max, int* numbers_out) { for (int var_i = 0; var_i < size; var_i++) { if (numbers[var_i] < min) { numbers_out[0] = numbers[var_i]; } if (numbers[var_i] > max) { numbers_out[1] = numbers[var_i]; } } return numbers_out; } int main() { int numbers[5] = { 5, 6, -2, 29, 6 }; int numbers_out[2]; int min = numbers[0]; int max = numbers[0]; // placing a fixed value into a function's argument list is a no-no. // Always use a variable or a pointer to a variable when passing values into a function. int size = 5; func_GetArray(numbers, size, min, max, numbers_out); cout
In my humble opinion this video should be renamed to be "make your Function change in variables which exist in the main function using pointers" OR something like that I just not convinced with it
I try by reference and it does the same, it is different, than using with pointer? (your videos are awesome) void MinandMax(int array[],int size,int& pMin,int& pMax) { for(int i=0;i array[i]){pMin = array[i];} if(pMax < array[i]){pMax = array[i];} } } int main() { int arr[5] = {5,4,3,8,2}; int min = arr[0]; int max = arr[0]; MinandMax(arr,5,min,max); std::cout
// FindMinAndMaxWithoutPointers.cpp : This file contains the 'main' function. Program execution begins and ends there. #include using namespace std; class FindMin_Max { public: int Max; int Min; FindMin_Max() { Max = 0; Min = 0; } void showInfo(int array [], int size) { Max += array[0]; Min += array[0]; for (int i = 1; i < size; i++) { if (array[i] > Max) Max = array[i]; if (array[i] < Min) Min = array[i]; } cout
if we are being honest this was very confusing all I wanted to do was return multiple values from a function, not this whole find smallest and largest biz
Hi, I also felt the challenge, but I believe it's very important to master the basics (which we will only appreciate later on). The videos gradually increase in complexity to allow us to evolve.
📚 Learn how to solve problems and build projects with these Free E-Books ⬇️
C++ Lambdas e-book - free download here: bit.ly/freeCppE-Book
Entire Object-Pascal step-by-step guide - free download here: bit.ly/FreeObjectPascalEbook
🚀📈💻🔥 My Practical Programming Course: www.codebeautyacademy.com/
Experience the power of practical learning, gain career-ready skills, and start building real applications!
This is a step-by-step course designed to take you from beginner to expert in no time!
💰 Here is a coupon to save 10% on your first payment (CODEBEAUTY_YT10).
Use it quickly, because it will be available for a limited time.
Pointers are super powerful, they can access the memory directly by the address of a variable and change its value easily. Fantastic topic.
All of your videos explain the concept so well! I am able to understand a lot without rewatching it. Thanks a lot for making these kind of videos!
Thank you. This is a very nice and concise explanation and examples of pointers returning multiple values. I'm learning a lot from these tutorials.
For those interested in expanding slightly deeper into this tutorial:
The getMinAndMax function assumes the min and max has been set to the min and max of the current array that's sent to the function, but is not guaranteed. For example, in a larger program, if the function has already been used with a different array with a smaller min and a larger max then in the current array sent to the function, then the values returned will be incorrect, unless the min and max is reset to the first element of the current array in use every time the function is called.
There are two ways that this could be addressed, such as setting the initial min and max inside the function. Another way could be to use the current getMin and getMax functions as shown below:
void getMinAndMax(int numbers, int size, int* min, int* max)
{
*min = getMin(numbers, size);
*max = getMax(numbers, size);
}
Love your channel, very clear and well spoken. Subbed!
☺️🥰
so basically passing 2 pointers to a function. so clear and watching it for the second time. I should remember this. I hope
after this video, everything is clear with the pointers. Thank you Saldina
Thanks a lot. I've been struggling with the concepts of pointers for a long time now and your detailed explanations are very helpful. Take love ♥
This exactly what i've been looking for. Thanks a lot!
🤗🤞😁
Did you need to use a pointer? Would the & operator have worked in the function declaration, removing the need for pointers in the function definition?
I was wondering as well. I just used & pointer to send a reference in.
If you remove the & (or the addresses of the variables) operator from that function you would't be able to return those two values at once, you probably would had to return an array, for example that contains those two values :P
This video is amazing. It really helped me understand pointers and the purpose of & to reference memory addresses.
Finally understood the crux of pointers. Thank you very much.
Using a reference in the parameters and writing the body accordingly also works.
void getMinAndMax(int[] numbers, int size, int& min, int& max){...}
No pointers required.
You are so gifted. Keep shining. ❤
Thank you so much, I am happy to share my knowledge and make other people's programming journey easier than mine was 🥰🥰
Great video, you have a great way of presenting the data.
For getmax, how does the program know how to output 26 instead of 6, since theyre both greater than max which is 5
thank you very much for these videos !
its just what i needed, i got confused a lot working with pointers, and you make it look really easy (:
All your videos are amazing!
🤗❤️
How to return multiple values from a C/C++ function?
Usually, a function can only return a single value, using the return mechanism. If you try to return more than one value from a function, only one value will be returned that appears at the rightmost place of the return statement. However, you can return multiple values from the function using the pointer, array, or structure.
Using Pointers
You can use pointers to return more than one value from a function by passing pointers as function parameters and use them to set multiple values which will then have visibility in the caller function.
Using Array
If you want to return multiple values of the same data type from a single function, then using an array is best suited because when an array name is passed as an argument then its base address is passed to the function so whatever changes made to the array is changed in the original array.
Using Structure
Another way to return multiple values from a function is by using structures. Structure is a user-defined datatype in C that can hold several datatypes of the same or different types. The idea is to create a structure variable containing all required data types as its members and return that from the function. We can then retrieve the values from the structure variable inside our caller function.
I'm falling in love with you😁😁😂🔥🔥your teaching skills are excellent💖💖💖
Thnaks! I try to do it by pass reference. it works too.
Thanks, hope you keep up the serie about c++
I will ☺️
previous video you said arrayname acts like a first elements pointer. isn't 5:10 passing numbers array is like passing a pointer ? but function waits for a value. it becomes confusing again :/
Thank you so much!!! It helps me a lot.
Misleading title tho. The function is not exactly "returning" multiple values it is just assigning the values of min, max referenced through the pointers and passed as an argument to the function.
Thank you this helped me understand pointers better. Is there more to them that I should know? Except for the fact that they allow you to access and edit the original address of a variable.
it's really helped me thank you so much :)
thank you sooooooo much. you explain very well.
Thank you. I love your videos, They are well presented and have helped me a lot.
When I try to calculate the size of numbers[ ] inside the getMin( ) using sizeof(numbers) / sizeof(int), it gives that the minimum of numbers[ ] is always numbers[0], i.e. the for loop in getMin( ) is skipped. However, when I send the size as a variable getMin( ), although I found size using the same way, it compiles perfectly. Why do I get this bug?
Thanks! Clear and beautiful!
Thank you! You help me very much! :)
You are amazing! :)
You're welcome 😊
Thank you for this great content
Very clear and interesting. Thank you very much !
Hey, I just started watching your channel and wanted to say thank you for the amazing help you provided. My teacher uses c instead of c++ but I notice that only things like cout and cin are different for now. In c the library is stdio.h tell me if Im right that it is about the same thx!
They are very similar. They have very similar syntax and code structure and both are close to the hardware - so minor differences here, it should be easy to switch between them.
The main difference is that C is a procedural language and it doesn't support OOP, while C++ is both procedural and object-oriented, and because of that, I'd give C++ an advantage over C, but neither one is a bad choice. 🤗
@@CodeBeauty Ok, thank you!
What will happen if we didnt declare int size as parameter. Without even that, the prog is running.. can pls tell
This is amazing!!
C++ is moving away from raw pointer semantics, if you want an optional value, you should be using the optional container. If you don't want an optional, you should be passing by T&...
Also for the specific examples that you given int& would have been better because it avoids the SEG fault when I you pass null to the min and max value. (Also passing by reference rather than passing by pointer makes it so that the data does not need to be dereferenced in the function and that the address does not need to be passed)
What shortcut do you use to comment out a block of code?
CTRL+K, CTRL+C
@@day4834 no don't make chutiya
Nice tutorial, it reminds me out variables from C#
and can someone tell me how does System("pause>0") works? it's bit confusing cuz compare is passed as string literall
system("pause"); is going to pause your console window until you press any key, and if you add >0 to it, it is going to remove junk text
You can remove >0 in the program that you're writing, and check out the behavior of your program, and then remove the remaining system("pause") part and check again
system("pause") is a platform-specific hack though, and it is not exactly recommended to use.
I use it to pause the console window.
However, a program that has done its job should quit and return its resources back to the computer, but in this particular situation, we're using system("pause") so that we can pause it and see the output in the console before it closes.
@@CodeBeauty much thanks
If you want something more platform independent you can use std::cin.get() instead.
can you tell me how to have a thousand seperator in c++?
Hi Can l use Min as Lowest , low in [10] bars shift 0 Max Highest , High in [10] bars shift 1 total = [21] bars
You should really assign default value to min an max in function. You can’t assume user will set it to first element of array.
nice explanation!!!
HELP ME PLEASE! 😢 how can i create a program in c++ that is focusing on multiple conversions. (with the use of goto loo, if else, switch) i think these 3 will be use. BIG THANKS IF YOU WILL BE ABLE TO HELP ME ❤️ . THANKS ALSO IF NOT
Great, thanks!
Just one question: Can someone explain me how the program assigns the number of elemts of the array to variable size? I don't see it and I don't understand how the for functions because size is never "initialized" or is never given a value. Tkanhs :)
Size is the parameter and 5 is the argument. Meaning, the function declaration is telling us we require an int called size. However, when the function is invoked/called you put the actual int there. CodeBeauty did that. They wrote 5 when calling the function within main. When they write 5 in the argument 5 is then stored within the size variable declared in the function parentheses.
Great channel. You explain very well !! One question: If min and max are also arrays. How do return min[ ] and max[ ] using numbers[ ] as a parameter? Thanks!.
The min and max are not arrays. They simply store a singular data from an array at index
@@thanaphonsanongkhun3372 Aren't they pointing to the same memory address ?? numbers[0].
@@robertogomezbarrron4968this by the way is why you should use the STLs array container rather than a pointer, all C style arrays are disappointed to an address that just happens to have the next few words after it allocated as well. (Declaring arrays with [] is a little bit different but still essentially the same thing) There is no distinguishable difference between a pointer to an array and a pointer to a single value in c...
oh por dios eres la mejor!!!
🙏💙
Damnn🔥
Why can't I hit the like button twice🤕
can anyone tell me why copy can't be used in main fun ??
great video !
how to write initwindow function in visual c++ please make the video (output full screen )
Thank you somuch mam😍❤️
When I have a function like void getMinAndMAx() I pass the parameters by refference (int numbers[], int size, int &min, int & max), and when I call it I write getMinAndMAx(numbers, size, min, max). Is it the same thing as you did? Which method is preferable to use?
Yes, it is.
In your version as in the original one the same things happen behind the scenes. Even the generated code is identical in both cases.
The version with references is preferable.
In my opinion, using pointers is actually preferable to references in that scenario. The reason why I say that is twofold.
First of all, it's very clear that you need to pass in a memory address, which will make the caller think twice about whether or not the data at that address will end up being modified inside the function.
Second, it allows you to pass in a null pointer, which could be useful in a function like this. Maybe you don't actually care about the max value and you only want the min value.
Anyway, that's my two cents on the matter. Both are valid approaches.
One another great tutorial about pointers. But I should warn you a possible crash in your code. Sometimes we can create an array with only 1 element. if we pass this array to your functions then your code will crash. So you should check array size first. Let me explain by code ;
int main() {
int myArray[] = {76};
cout
If the array has only one element, won't it have a size of 1? In that case, this code
for (int i = 1; i < size; i++)
won't run beyond loop initialisation, because the loop variable i is initialised to 1, and i is not less than size (1 is not less than 1), so the "for" loop just won't run. In that case the code will return the only value in the array, as the variable min was initialised to the value held in numbers[0].
So helpful thanks
Hi i am chandru,when I run this program with the numbers (2,6,-5,67,-89)............It shows both max and min is -89.how can I fix it.?
Thumb up @TrueCodeBeauty 🙏☝️
@CodeBeauty can u please make a video on how to pass a function to another function ny refernce that we use one function attribute in other function
Your intro sound is super loud compared to your content volume. This info was super helpful though, thank you
Hi Sladina, can I do this method to return an array from a function??
Yes if it's dynamically allocated
Dynamic allocation is an option, but if you don't want to allocate on the heap, you can also add an array parameter and then return the same address
Using int instead of size_t for an array size is a severe error.
u saved me! thanks
Using pointers is C-like, in C++ you would use references in the function argument
Hi! it will be nice not to transfer the size of the array directly, but to calculate it: sizeof array / sizeof array[0]
That wouldn't work. When you pass an array into a function, it gets coerced into a pointer. Trying to do sizeof(array) would just yield the size of a memory address, not the total size of the array. Even if that did work, since we're no longer passing the length of the array into the function, there would be no way to guarantee that the array wasn't empty, meaning that array[0] could very well seg fault.
TL;DR: Good idea in theory, but it wouldn't work in practice.
Hi i am really struggling with a code,
Lets say u have a txt file which looks something like,
Noun
Dog
An animal
**
Adj
Pretty
Something beautiful
**
Noun
Quote
A value
**
Noun
Cinq
Something..
**
How can we make the program to display only the words that has a “Q” in it but is not followed by a “U”
Result should be something like - “cinq”
Any reason you can't you just do this?
void getMinAndMax(int numbers[], int size, int &min, int &max)
Excellent tutorial. However, in the function call "getMinAndMax", the ampersand indicating address is redundant as both "min" and "max" are the addresses of the arrays already.
Really? Why do you say that? min and max both contain the value of numbers[0], which is 5. If you don't pass &min and &max then the getMinAndMax function will receive the address value of 5 in the pointers to min and max.
Actually that is not what is called returning multiple values from a method, just passing multiple references and changing them.
Returning multiple values can be done with something like 'tuple' or a custom made class/struct.
You're right. Firstly, the function used in the example is of the void type which indeed means no return, then we cannot accurately talk about multiple return.
very helpful
i love your videos
your magic !!
can't understand a thing, I keep watching you for a whole video XD
I like that idea, but why not just do it in one step by calling by reference?
give me a heart sis🥰
Thanks
This is so interesting
Greatly
Saldina, if one dedicates 5 hours per day of serious C++ learning, how long will it take to be an advanced C++ programmer? Subscribed and Thanks
5 hours every day is a lot of time, and I'm not sure if it is maintainable long term. I'd aim for 2-3 hours per day, stay consistent, do that for a year and then start looking for junior dev positions, and continue learning.
You are not going to be a hell of a developer after one year, but you might get a chance in a company, and if you do, find yourself a good senior dev who is willing to help and guide you and don't let go!
Don't chase the money or different benefits that companies offer if you are just starting.
Free food, team buildings, remote work, gym memberships, playing games at work... 🚫
The only important thing at this stage is to get experience and knowledge, and once you have that, you'll have all the benefits and money that you want. But, it'll be a marathon, not a sprint. 😉
@@CodeBeautyGreat. will follow your advice. And will comment after a year. Wish you all the very best.
Where is the task?
Using a "struct" with min and max, but I think the (real) return value will be a pointer to the struct variable, I think it is not realy seen as a pointer.
I followed it through but it sounded like a complicated way to find the max and min values in an array. Is there an easier way to do it, or were you just showing us how to do it using pointers?
I think she used this example just to demonstrate how to use pointers. In this case, we can find max value or min value without using pointers. Please correct me if I am wrong.
void getMinMax(int input[]) {
int min = input [0];
int max = input[0];
for (int i = 1; i < sizeof(input); i++) {
if (input[i] < min)
min = input[i];
if (input[i] > max)
max = input[i];
}
cout
This is pretty much what you understand as "call by reference", am I right? Just to make sure I connect the right dots.
Oh yeah, it is, just saw it in the video description! Thanks a lot for writing it in there as well! Great and very helpful video(s)!!
My answer is a short program in c++ this:
#include
class CodeBeautyMinMax
{
private:
int min, max;
/* Disallow object creation without an parameter */
private: CodeBeautyMinMax(){}
public: CodeBeautyMinMax( int *paramArr )
{
/* min and max should be zero */
this->min = this->max = 0;
this->findMinAndMax( paramArr );
}
private: void findMinAndMax( int *paramArr )
{
if ( paramArr == NULL ) return;
/* first element of array is now stored into min and max variable*/
this->min = this->max = *paramArr;
/* Enter the While-Loop again if you do not find a 0 "Zero" */
while( *(++paramArr) != 0 )
{
/* To reach the next element of the array - This does the trick: ++paramArr */
/* This is the same as above into the Video - but shorter =) */
this->min = ( *paramArr < this->min ? *paramArr : this->min );
this->max = ( *paramArr > this->max ? *paramArr : this->max );
}
}
public: int getMin(){ /* Get only the min-value */ return this->min; }
public: int getMax(){ /* Get only the max-value */ return this->max; }
public: void getMinAndMax( char* buf )
{
/*
It would work with sstream as well - but I am a old school guy =)
Combine string with variable like this
*/
sprintf( buf, "min is: %d
max is %d", this->min, this->max );
}
};
int main(void)
{
int arrValues[7] = { 1, -5, 14, 6, -20, 99, 0 };
int *ptr = arrValues;
CodeBeautyMinMax *cbmm = new CodeBeautyMinMax( ptr );
std::cout
Can someone explain the purpose of pointers
I’ve watched most of her videos on them (maybe haven’t gotten to the one with the answer)
But pointers, so far, seem like just another option instead of nested if statements. Why use them?
I can pass multiple if statements in a single function and return the value. Why go through its address and dereferencing them
I think, we can just pass those max and min by reference, I mean without passing addresses.
Hey Saldina, I'm new to c++ and your videos are really helping. I have a problem with this example as i do not fully understand the need for pointers in it. Here is my own code:
#include
#include
void maxminNum(std::vector nums){
//for max:
int max = nums[0];
int min = nums[0];
for(size_t i{1}; i < nums.size(); i++){
if(nums[i] > max){
max = nums[i];
}
if(nums[i] < min){
min = nums[i];
}
}
}
int main(){
std::vector vect {3, 5, 1, -3, 8};
maxminNum(vect);
return 0;
}
Please explain why my code which passes by value may be affected and why we should pass by reference as the code works as it should.
Hey. Your code works good for min and max but if you want use max and min values in function main you can't. If you declare variable in function, this variable on the end of loop will disappear. So if you need use in main or in another function your max or min (and you don't want working with return types in function maxminNum) you need declare variables max and min in main function and send pointer like a parameter to your function.
Chatgpt
I still don't know the point of pointers. Dang. I'll keep trying
Bro same. The problem is i can get those functions working without it.
// using a pointer to pull data out of a function....
// using a pointer to pass a reference to an array
#include
using namespace std;
int* func_GetArray(int numbers[], int size, int min, int max, int* numbers_out)
{
for (int var_i = 0; var_i < size; var_i++)
{
if (numbers[var_i] < min)
{
numbers_out[0] = numbers[var_i];
}
if (numbers[var_i] > max)
{
numbers_out[1] = numbers[var_i];
}
}
return numbers_out;
}
int main()
{
int numbers[5] = { 5, 6, -2, 29, 6 };
int numbers_out[2];
int min = numbers[0];
int max = numbers[0];
// placing a fixed value into a function's argument list is a no-no.
// Always use a variable or a pointer to a variable when passing values into a function.
int size = 5;
func_GetArray(numbers, size, min, max, numbers_out);
cout
In my humble opinion this video should be renamed to be "make your Function change in variables which exist in the main function using pointers" OR something like that I just not convinced with it
kess emmik shou awiye
can i use a function pointer*
OMG! Still No tuples, really?
7:15
I try by reference and it does the same, it is different, than using with pointer? (your videos are awesome)
void MinandMax(int array[],int size,int& pMin,int& pMax)
{
for(int i=0;i array[i]){pMin = array[i];}
if(pMax < array[i]){pMax = array[i];}
}
}
int main()
{
int arr[5] = {5,4,3,8,2};
int min = arr[0];
int max = arr[0];
MinandMax(arr,5,min,max);
std::cout
Sorry, where exactly do you return more than one value?
// FindMinAndMaxWithoutPointers.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include
using namespace std;
class FindMin_Max {
public:
int Max;
int Min;
FindMin_Max() {
Max = 0;
Min = 0;
}
void showInfo(int array [], int size) {
Max += array[0];
Min += array[0];
for (int i = 1; i < size; i++) {
if (array[i] > Max)
Max = array[i];
if (array[i] < Min)
Min = array[i];
}
cout
if we are being honest this was very confusing all I wanted to do was return multiple values from a function, not this whole find smallest and largest biz
Hi, I also felt the challenge, but I believe it's very important to master the basics (which we will only appreciate later on). The videos gradually increase in complexity to allow us to evolve.
I *Miss You
better practice to return a struct