![]() |
|
#1
|
|||
|
|||
What is an array?...you say??? Well I'm placing this for anyone who might be saying something like:
Quote:
Well hopefully you are a bit familiar with basic C data types and what their storage capacities are: char -1 byte short -2 bytes int -4 bytes (on most systems) We'll just stop there with data types. An array is a grouping of one of these data types stored contiguously in memory. The number of these array 'elements' is specified when you declare an array variable like: char str[10]; ...which is about the simplest example of declaring an array. It specifies the size of the array to be 10 elements of size char. We call an array of chars a 'string'. A sting array is the easiest example to observe as it is made up of single bytes rather that the larger data types like int. You can declare all kinds of arrays,,, arrays of pointers (or arrays of arrays), arrays of structures, arrays of ,,, what else..... Anyhow, we can declare a string and initialize it at the same time like this: char str1[] = "Hello World!"; This declaration automatically sizes the array to fit the quoted text +1 for '\0'. A more awkward but illustrative method would be: char str2[] = {'H','e','l','l','o',' ','W','o','r','d','!','\0'}; Note the \0 at the end of str2[]. It is called the 'null' and it's value is '0' Using the " " method of str1[] automatically places the '\0'. The \0 is to signify the end of the string for things like printf(), otherwise it would read on in memory until an '0' was found. This is specific to string arrays. Normal arrays would not have the ending '\0'. Below I deliberately stuck an 0 (zero) into str3[]. Note how it prints and review the above. Play around with it yourself! CPP / C++ / C Code:
With that in hand you should be able to step up to an array of size int. Now think about this... the array name is a pointer.... Note the output of the above. hmmm so we could make an array of pointers to arrays. Like a stack of strings! Some would call this (me included) a 2 dimensional array. It can be declared like this: char str[3][15]; That could be used to store the 3 strings I have above. Or it could be used to just store single bytes of data for other purposes. That's it for now. Hope it helps get you on your way searching the forum for more specific examples of what YOU want to know about arrays (at this time). Howard; Last edited by Howard_L : 26-Sep-2007 at 01:05.
|
|||
|
#2
|
|||
|
|||
Re: What is an array?FYI: In all of the text that you posted, I didn't see where you said that arrays are zero-indexed. This casual information is sometimes useful when trying to define arrays in C.
:davis: |
|
#3
|
|||
|
|||
Re: What is an array?Right davis, sorry to have left that tidbit out.
By 'zero indexed' davis is talking about the fact that when using array notation we refer to the first element as element[0] , and NOT element[1]. For example, referring back to the array declaration above: Code:
str[0] is the ascii character 'H'. str[1] is the ascii character 'e'. str[11] is ascii '\0'. You may or may not have noticed that in the program above I print the arrays beginning at element[i] and each loop begins with the assignment: i = 0; eg: Code:
Code:
And to expand on that a bit a 2 dimensional array's first element would likewise begin at: 2Darray[0][0] This little program gives a good illustration of a two dimensional array AKA and array of pointers to arrays : ) (I think) CPP / C++ / C Code:
Code:
So much for tonights Cprog time allotment... Good Night... Howard; |
|
#4
|
|||
|
|||
Re: What is an array?Quote:
One thing to note is that these addresses are representative of a moment in time on one particular machine and may not every appear again or may even appear every time for a given process, however, from machine to machine, it is unlikely that they will be the same. ...and it looks like you skipped the 'l' in world. FYI: possibilities! An "E" for effort? :davis: |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| return string from a function | Howard_L | C Programming Language | 4 | 17-Aug-2007 23:56 |
| How to sort in C++ alphabetically | wilen | C++ Forum | 5 | 20-Apr-2007 14:43 |
| 1-D array | jack999 | C Programming Language | 16 | 16-May-2006 12:38 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 19:31 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The