![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programmer
Join Date: Oct 2005
Posts: 56
Rep Power: 3
![]() |
How do i do it ?
|
|
|
|
|
|
#12 |
|
Programmer
Join Date: Oct 2005
Posts: 56
Rep Power: 3
![]() |
Nvm..I got it ..I will change it ..
|
|
|
|
|
|
#13 |
|
Programmer
Join Date: Oct 2005
Posts: 56
Rep Power: 3
![]() |
I can't seem to edit my post so next time when i post i would use code tags ..
|
|
|
|
|
|
#14 |
|
Programmer
Join Date: Sep 2005
Posts: 50
Rep Power: 0
![]() |
use code tag like this [ C O D E ] your code [/ C O D E ]
just do't use spaces in code tage. its like html code. or click # sign in ur advance windows of reply post. |
|
|
|
|
|
#15 | |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Quote:
It's C but I hope it should still help.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
|
#16 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,038
Rep Power: 5
![]() |
The main use of arrays is to manipulate a set of things (objects or variables) that are all of the same type. This allows you to write the code to do the manipulation once, rather than once for each value. You can then loop through, starting with the first element you wish to process, and ending at the last.
Remember that arrays are always zero-based; that is, the first element is element zero, not element one, and the last is numbered as one less than the size. This means if you're looping through an array, make sure you don't go beyond the bounds; the compiler does not check if you exceed the bounds of the array, so you have to do this yourself. Nor does your program throw an exception at runtime, as a Java program will. Note that, in some special circumstances, you actually will want to go beyond the boundaries you've declared for your arrays (the only case I can think of offhand is when you use an array as the last element of a structure, and then dynamically allocate memory for your structure plus some 'extra'), but cases like this are rare, and when you do it, be sure you know why you're doing it. In C and C++, arrays and pointers are closely related. A pointer is a variable that stores an address of something else (an object, variable, or function), and an address is just the location at which it resides in memory. Pointers are useful because you can access a chunk of memory through the pointer, which is often more efficient. For example, let's assume my system has ints that are 32 bits, and pointers that are 32 bits (this is a very common scenario on modern machines). Now let's say I have an array of 1024 ints. If I pass the whole array (ie, each value separately), that is a lot of parameters I have to pass, and consequently, a lot of work that needs to be done every time the function gets called. On the other hand, I could pass a single pointer to the start of the array, and the called function could then access the entire array through this pointer. C and C++ support this sort of coding by having arrays and pointers be somewhat interchangeable. The idea is that any array name without a subscript (the number in the brackets), or with fewer subscripts than the array actually has (ie, one subscript for a two-dimensional array) is treated as an address. As such, you can assign it to a pointer, and you can use array notation on pointers. However, there are some things to know about. First is that an array's memory is at a fixed location- you can assign its address to a pointer, but cannot assign to its address (this is for much the same reason that x = 5 is legal, whereas 5 = x is not). Second is that true arrays (often called 'static arrays') allocate memory for the requested number of elements; hence, sizeof(array) is equal to the number of elements times sizeof(arrayElement); pointers, on the other hand, have a fixed size (often, but not always, the same as an int on the system in question). Last is that pointers need to be initialized before using them to access what they point at. Remember that variables in C and C++ are not initialized by default (except for those declared as static, or outside all functions), and pointers are no exception. Using an uninitialized pointer will at best yield garbage output, and at worst may do some serious damage (you're mucking with memory at essentially random locations), though many modern systems will terminate programs that try to access memory they don't own. There are also other uses of pointers, such as 'returning' more than one value from a function (this was common in C, but isn't used as much in C++, thanks to 'reference variables'), allocating dynamic arrays (simply allocating a chunk of memory at runtime, and accessing it through the pointer, just as Polyphemus_ did in his example), and other things. For more on pointers, you should see DaWei's article on them; the link is in his sig. It's worth the read, but if you don't understand them right away, that's normal. Pointers generally take a bit of work to understand, but once you do, you'll be amazed how easy they really are to use.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#17 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Nicely explained and nice to see you back pharoaoh (after 2 months).
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#18 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,038
Rep Power: 5
![]() |
Quote:
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|