garbage value ?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 2
Reputation: gowswan is an unknown quantity at this point 
Solved Threads: 0
gowswan gowswan is offline Offline
Newbie Poster

garbage value ?

 
1
  #1
Dec 17th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: garbage value ?

 
0
  #2
Dec 17th, 2004
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...
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: garbage value ?

 
1
  #3
Dec 17th, 2004
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

  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:

  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.
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 255
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: garbage value ?

 
0
  #4
Dec 17th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 140
Reputation: chound is an unknown quantity at this point 
Solved Threads: 1
chound chound is offline Offline
Junior Poster

Re: garbage value ?

 
1
  #5
Dec 18th, 2004
its a pointer no need to init the number of chars, saves a lot of work.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: garbage value ?

 
0
  #6
Dec 18th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 140
Reputation: anastacia is an unknown quantity at this point 
Solved Threads: 1
anastacia's Avatar
anastacia anastacia is offline Offline
Junior Poster

Re: garbage value ?

 
0
  #7
Dec 18th, 2004
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
:lol: I am not one of those who wait for things to happen, :p but one of those who make things happen ;)
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: garbage value ?

 
0
  #8
Dec 18th, 2004
or even easier...

  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
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4709 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC