How is this code working ? ; and why is the output " p" if you don't input anything? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How is this code working ? ; and why is the output " p" if you don't input anything?

I want to have a char array with undetermined parameter and it would contain the elements I input. I was messing around to see what happens and this code was giving weird outputs, I know it's far from what I intended to do But now I want to know how this code is running………… https://code.sololearn.com/c3ThW6tLI3W6/?ref=app

30th Jun 2022, 2:05 PM
Unwanted Blue
Unwanted Blue - avatar
10 Answers
+ 4
Guys, I'm not all too sure about this, but I guess what we see in output pane represents whatever bytes there are in memory at the time the code executes. How the bytes are mapped into instances of a certain type (the array data type) is beyond my understanding, unfortunately. C and C++ allows us to do this still, even though this is technically wrong. An attempt to read/write to a memory address/block that was not already claimed for use. Essentially, we cannot predict what was there in memory, cause we never claimed that block of memory (by specifying array size). So in this case, I guess there probably be no definite answer as to why the 'p' or whatever we see in the output pane. In other languages, what we see here may never happen, cause there might be a rule about memory usage being enforced on. That memory has to be claimed first before it can be used. Hth, cmiiw 👌
30th Jun 2022, 5:17 PM
Ipang
+ 3
You run the loop five times but your array is not properly defined, number of elements hasn't been specified. char TF[] = {}; // array size unspecified
30th Jun 2022, 2:31 PM
Ipang
+ 2
Is it possible to make an array without any fixed size/parameter, or can I somehow change the size of an array gradually?
30th Jun 2022, 2:43 PM
Unwanted Blue
Unwanted Blue - avatar
+ 2
You need to dynamically allocate memory if you do not know how much storage you will need.
30th Jun 2022, 2:49 PM
Mihir Lalwani
Mihir Lalwani - avatar
+ 2
Manav Roy ya dynamic array..
1st Jul 2022, 6:54 AM
Mihir Lalwani
Mihir Lalwani - avatar
+ 1
What do you want do??
30th Jun 2022, 2:24 PM
Mihir Lalwani
Mihir Lalwani - avatar
+ 1
#include <iostream> using namespace std; int main() { char TF[10]; for (int i=0;i<5;i++){ cin>>TF[i]; cout<<TF[i]<<endl; } return 0; } Ipang ya you are right array size...
30th Jun 2022, 2:36 PM
Mihir Lalwani
Mihir Lalwani - avatar
+ 1
Manav Roy i also don't know why its show p
30th Jun 2022, 4:34 PM
Mihir Lalwani
Mihir Lalwani - avatar
+ 1
I just wanted to know why there are irrational outputs whereas it should give no output. back then I didn't know how but now I know It's because in C++, if we call a value out of the array it picks the next available value in the code or the stack. for example: int myarr[3]={0,1,2}; cout << myarr[4]; cout << myarr[5]; cout << myarr[6]; cout << myarr[7]; the output will be irrational or junk value to be precise I think here it takes values scattered over the stack and prints it.... As far as I know other languages do not allow this behaviour, only C++ does.
2nd Jul 2022, 4:27 PM
Unwanted Blue
Unwanted Blue - avatar
2nd Jul 2022, 6:26 AM
DESHMUKH MUSAHIB
DESHMUKH MUSAHIB - avatar