I am solving an exercise and I don't understand exactly how this function that returns a pointer of type char would be working.
And how could I call it or transfer it to another variable in my main function,
To be more clear,
- I want to know how the return of a pointer of type char works
- I also want to know how I can invoke that string that I introduce in a function and be able to invoke it in my main function.
PSDT: The exercise asks us to use the function as it is there "char *load(void);" Thanks in advance.
I want to know how the return of a pointer of type char works
We can start at the beginning. Let's look at a simple example:
This function returns an integer, which has been entered by the user.
How does the
return
here work? Simply copy the value ofto_return
.Well, exactly the same thing happens with pointers. A pointer is nothing more than a variable that stores memory addresses. Thus, when returning a pointer, the only thing the program does is return a memory address.
I also want to know how I can invoke that string that I introduce in a function and be able to invoke it in my main function
We have already said that pointers are nothing more than variables that store memory addresses. So, given a function that returns a pointer, you can do 2 things with that pointer:
Said with code:
The difference between both uses is that the first one, when using shared memory, is sensitive to the changes made in said memory region. This is independent of the point at which such changes are made. For this reason last
puts
is able to display the last text entered by the userAn example in real life could be the class blackboard, all attendees see the same blackboard and the changes that the teacher makes to it can be observed by all of them.
Dereferenced pointers
If you compare the example that I have given you with the one that appears in the question, you can see a fundamental difference.
In your case, the variable
cadena
is not static . This is a problem, since itcadena
is a local variable, which means that when execution exitscarga
, the memory associated withcadena
becomes available to other variables, so its content could change at any time.You have to be very careful with this. You cannot return a memory address from a local variable. In the same way you should also not return a pointer on which you have executed
free
:I/O C++
It is not convenient to mix I/O uses typical of C with those of C++:
First of all,
fflush
**should not be used withstdin
. This function is designed to be used with output streams only . The behavior of this function when passed an input stream is indeterminate, which means that, depending on the compiler, it may work, it may not work, or it may fail.On the other hand, it must be taken into account that although
stdin
andcin
refer, by default, to the standard input, they are different mechanisms. Everyone processes information in their own way.It happens that by default both mechanisms are synchronized, so, in principle, we could mix uses of
stdin
andcin
. This synchronization, however, comes at a price and makes the I/O in C++ much slower than in C.The delicate thing about this matter is that said synchronization can be deactivated at any time by executing the following line:
From that moment on
stdio
,cin
each one will go their own way and the calls tocin
will be much faster than their ownscanf
(something logical on the other hand).For this reason and also for clarity, I would avoid using
stdio
e in the same programiostream
.If you need to clean
cin
you can do the following:well you could start by looking for a c and c++ tutorial where they explain the data types
int char
there are many since that is basic, second as we see the functioncarga
of type char where it asks the user to enter a series of char's or word, then use the Strcpy function to copy the data from the right to the left and thereforecadena
obtains the values ofcarga
being the 2 of the same type.it is somewhat summarized but there it goes
what is happening is that your function returns a data pointer type char, the function in question fills the internal variable which is stored in memory and your function returns the address in memory or a pointer that points to the address in memory of said variable already filled, then the other string is copied with the strcpy() function and then printed
something like this but better organized.