How is this code working ? ; and why is the output " p" if you don't input anything? | Sololearn: Learn to code for FREE!

+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

6/30/2022 2:05:45 PM

Unwanted Blue

14 Answers

New Answer

+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 👌

+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

+2

Is it possible to make an array without any fixed size/parameter, or can I somehow change the size of an array gradually?

+2

You need to dynamically allocate memory if you do not know how much storage you will need.

+2

Ipang Mihir Lalwani I'm also wondering that why it's outing " p" if we do not enter any input 🤔

+2

Unwanted Blue Is it possible to make an array without any fixed size/parameter, or can I somehow change the size of an array gradually? Yes ,By vectors.

+2

Manav Roy ya dynamic array..

+1

What do you want do??

+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...

+1

Manav Roy i also don't know why its show p

0

Unwanted Blue Yup,I saw from a comment in your code, We need to wait for a good answer then.

0

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.

0

Unwanted Blue You've created array of 3 size,And trying to access the value at 4th ,5th,6th,7th index of the array, Compiler will give you garbage value.

-2

https://youtube.com/shorts/75oJMVADKHY?feature=share