#include<iostream>;
using namespace std;
int main(){
    
    char arr[8]={};
    
    for(int count(0); count<=7; ++count){
            
            
            arr[count]='*';
            
            }
    cout<<arr;
    
    
    cin.get();
   return 0; }
I really do not understand how some garbage values get stored into the array; even after I have initialized it properly; and how it can display MORE values than what I have stored into it; like if my array size for character array is 10; it will show more than that !! 


I use Dev C++ to write, compile my code.

I have attached the code; that I run. 


In the below code; 3-4 EXTRA values get displayed as well; they are useless characters; I am not sure where they come from !!

For displaying arrays you do not just call the array its self you have to make a for() loop to output.

for(int i = 0; i < 8; i++)
{
	cout << arr[i];
}

also you have not terminated the array with a newline value so cout does not know where the end oh the char array is.

Thank you; ^ and ^^ for your prompt reply :)

I have another problem similar problem; i.e.

now ONCE i have declared a char array of a particular size; if I add more values into other indexes of it; it stores them; its like the char array is EXPANDING automatically; while in int array; it gives an error !! Why does this happen ?

PS - after i can understand the reason for this problem; how can i go back to this thread and mark it as solved; and access it; ( i am new; and i really do not understand how to access the threads that i have begun )

my code as follows:

#include <iostream>

using namespace std;

int main()

{
char arraY[1]={};
//only one char allowed in it; with index name 0
arraY[0]='b';

for(int count(0); count<=2; ++count)
{
        arraY[count]='t';
        }

for(int count(0); count<=2; ++count)
{
        cout<<arraY[count];  }
    
    
    
    cin.get();
return 0;}

You log in and click your name on the top left hand corner then click statistics >> Find all threads started by name. Then just select the thread that you want to go to. To mark as solved scroll to the bottom where it says reply to thread in a yellow box and just to the right of that is blue text that says mark as solved.

sure, thanks. I understand, could also please help me understand why the size of the array expands ?

-uzi

I'm not sure because when I was doing some testing I made a char array size 2 and the size of it said it was 2 bytes in size but there were 5 characters in the array.

Thank you; ^ and ^^ for your prompt reply :)

I have another problem similar problem; i.e.

now ONCE i have declared a char array of a particular size; if I add more values into other indexes of it; it stores them; its like the char array is EXPANDING automatically; while in int array; it gives an error !! Why does this happen ?

Bottom line, array does not resize itself. Your problem has to
do with memory. Your code has a bug; You declared char arraY[1],
but in your for loop you are looping it from 0 to 2, which has 3 index.
Again, arrays does not resize its self, nor does it check for bound
errors. Don't worry about whats happening in the memory right
now. Just remember that when you have a code similar to this :

data_type  dataVariableArray[ someConstantNumber ] ;

That array has elements from 0 to someConstantNumber - 1,
anything else then its invalid.

the reason that you can go beyond the array size is that in c++ it is leagal to increment the pointer past the size of the array. its a through back to c. you cant start before the array like index = -1 but if the size of the array is 2 it is legal to go the index = 2.

char temp[1];
temp[-1]  // cannot be done
temp[1]  // okay even though the data is only in temp[0]

thank you for your reply, nathan.

i just wanted to ask something similar as well.

i am attaching the file of the code(along with my txt file from whch data is read), now what i donot; understand is; that at i=8190; the value once is output and it is exactly different, once again after i output; why does it automatically change. i know that changing the size of the columns in the longitude array make it to 11 will make it fine; but i donot understand why does the array at 8190 row automatically CHANGE; itselfs data, and show something else.

airports_clean.txt

Untitled1.cpp

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.