954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

garbage value ?

i am learning "c".i am in the beginning level. i want more explanations about arrays. in arrays what does the garbage means? how it affect the program.

gowswan
Newbie Poster
2 posts since Nov 2004
Reputation Points: 11
Solved Threads: 0
 

garbage?
If you haven't initialised array elements or incorrectly declared an array the data in it will be the data that was in the memory spots before the array even existed.

If you change it you can cause the computer to crash...

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

an array is essentially a variable which can store a number of values, all stored one after another in memory and all having a certain index;
eg

int numbers[10]; // array of 10 integers, unitialised
int morenumbers[3] = { 27, 4, 1 }; // array of 3 numbers, initialised
int numbers2 [] = { 0, 1, 14, 25 }; // array of 4 numbers, initialised (the [] means the compiler determines the size, in this case 4 as we have asked for 4 numbers


numbers2 is the variable name.
*numbers2 is a pointer to the first in the list (therefore the variable name refers to the head of the array)

Each item can be retreived using the [] operator;

numbers2[0] = 0 // note, the first element is index 0!!
numbers2[1] = 1
numbers2[2] = 14
ect...

arrays can be initialised after they are declared and the best method is a loop eg:

int numbers[5]; // array
char *str; // input string

for(int i = 0; i < 5; i++) // goes from 0 to 4 (as arrays start at 0)
{
    cout << "Enter number " << i << " for the array";
    cin >> str;
    data = atoi(str);
    numbers[i] = data;
}


this code will ask the user to input 5 values, which are inserted into the array.

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 
int numbers[5]; // array
char *str; // input string

for(int i = 0; i < 5; i++) // goes from 0 to 4 (as arrays start at 0)
{
    cout << "Enter number " << i << " for the array";
    cin >> str;
    data = atoi(str);
    numbers[i] = data;
}


That's not a string -- you just did a Bad Thing.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

its a pointer no need to init the number of chars, saves a lot of work.

chound
Junior Poster
145 posts since Aug 2004
Reputation Points: 15
Solved Threads: 1
 

when using C++ you should never use char* for strings, instead use which yields the string class.
You can then convert those to char* where needed.

I agree that char* is usually a better idea than char[] to handle strings in C.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

u get garbage values when u have not initialised your variables or arrays inthis case. because when your compiler aloocates a memory space to your array that space may already contain values- hence garbage values

anastacia
Junior Poster
142 posts since Nov 2004
Reputation Points: 11
Solved Threads: 1
 

or even easier...

int numbers[5]; // array

for(int i = 0; i < 5; i++) // goes from 0 to 4 (as arrays start at 0)
{
    cout << "Enter number " << i << " for the array";
    cin >> numbers[i] = data;
}


needs error checking though :)

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You