943,503 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7491
  • C++ RSS
Dec 17th, 2004
1

garbage value ?

Expand Post »
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.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
gowswan is offline Offline
2 posts
since Nov 2004
Dec 17th, 2004
0

Re: garbage value ?

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...
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 17th, 2004
1

Re: garbage value ?

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

C++ Syntax (Toggle Plain Text)
  1. int numbers[10]; // array of 10 integers, unitialised
  2. int morenumbers[3] = { 27, 4, 1 }; // array of 3 numbers, initialised
  3. 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:

C++ Syntax (Toggle Plain Text)
  1. int numbers[5]; // array
  2. char *str; // input string
  3.  
  4. for(int i = 0; i < 5; i++) // goes from 0 to 4 (as arrays start at 0)
  5. {
  6. cout << "Enter number " << i << " for the array";
  7. cin >> str;
  8. data = atoi(str);
  9. numbers[i] = data;
  10. }

this code will ask the user to input 5 values, which are inserted into the array.
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 17th, 2004
0

Re: garbage value ?

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 18th, 2004
1

Re: garbage value ?

its a pointer no need to init the number of chars, saves a lot of work.
Reputation Points: 15
Solved Threads: 1
Junior Poster
chound is offline Offline
143 posts
since Aug 2004
Dec 18th, 2004
0

Re: garbage value ?

when using C++ you should never use char* for strings, instead use <string> 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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 18th, 2004
0

Re: garbage value ?

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
Reputation Points: 11
Solved Threads: 1
Junior Poster
anastacia is offline Offline
142 posts
since Nov 2004
Dec 18th, 2004
0

Re: garbage value ?

or even easier...

C++ Syntax (Toggle Plain Text)
  1. int numbers[5]; // array
  2.  
  3. for(int i = 0; i < 5; i++) // goes from 0 to 4 (as arrays start at 0)
  4. {
  5. cout << "Enter number " << i << " for the array";
  6. cin >> numbers[i] = data;
  7. }

needs error checking though
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: CAn any1 tell me watz wrong in da code
Next Thread in C++ Forum Timeline: Help w/ dynamic list funtction definitions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC