GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Sep-2007, 00:26
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 846
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

What is an array?


...you say??? Well I'm placing this for anyone who might be saying something like:
Quote:
i'm having problems with how to understand how array works exactly...
...and not be able to decypher some of the many examples in other threads already posted.
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:
#include <stdio.h>

int main(void)
{
  unsigned int i;
  char str1[] = "Hello World!";
  char str2[] = {'H','e','l','l','o',' ','W','o','r','l','d','!','\0'};
  char str3[] = {'H','e','l','l','o',' ','W','o','r',0,'l','d','!','\0'};

  printf("\nsizeof(str1)=%d , %p , %s \n", sizeof(str1), (void*)str1, str1);
  printf("sizeof(str2)=%d , %p , %s \n", sizeof(str2), (void*)str2, str2);
  printf("sizeof(str3)=%d , %p , %s \n", sizeof(str3), (void*)str3, str3);

/* We can prove that these bytes are stored 'contiguously' in memory by examining
each byte starting with the one at the beginning of storage and looping x bytes.
In my Windows98 OS storage allocations move downward i memory, hence str3[] is lowest address.
In Linux storage is the opposite (I think, I forget right now). (or is that compiler related???)
To do this I will use what is called 'array notation'
*/
  printf("\n  count    address    ascii  character \n");

  for(i = 0; i < sizeof(str3); i++)
  {
    printf("str3[%2d]:  %p , 0x%02x , %c \n", i, (void*)&str3[i], str3[i], str3[i] );
  }
  getchar();
  for(i = 0; i < sizeof(str2); i++)
  {
    printf("str2[%2d]:  %p , 0x%02x , %c \n", i, (void*)&str2[i], str2[i], str2[i] );
  }
  getchar();
  for(i = 0; i < sizeof(str1); i++)
  {
    printf("str1[%2d]:  %p , 0x%02x , %c \n", i, (void*)&str1[i], str1[i], str1[i] );
  }

  return 0;
}
Run that and see what you see....
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  
Old 04-Oct-2007, 02:02
davis
 
Posts: n/a

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  
Old 04-Oct-2007, 23:45
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 846
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

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:
char str2[] = {'H','e','l','l','o',' ','W','o','r','d','!','\0'};
We see that it contains 12 elements , str2[0] through str2[11].
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:
/* This loop begins with i = 0 which will access the first element. */ for(i = 0; i < sizeof(str2); i++) { printf("str2[%2d]: %p , 0x%02x , %c \n", i, (void*)&str2[i], str2[i], str2[i] ); }
Run it again and look carefully at the output. Example of the loop above:
Code:
Element Address Hex ascii str2[ 0]: 0073FDD0 , 0x48 , H str2[ 1]: 0073FDD1 , 0x65 , e str2[ 2]: 0073FDD2 , 0x6c , l str2[ 3]: 0073FDD3 , 0x6c , l str2[ 4]: 0073FDD4 , 0x6f , o str2[ 5]: 0073FDD5 , 0x20 , str2[ 6]: 0073FDD6 , 0x57 , W str2[ 7]: 0073FDD7 , 0x6f , o str2[ 8]: 0073FDD8 , 0x72 , r str2[ 9]: 0073FDD9 , 0x64 , d str2[10]: 0073FDDA , 0x21 , ! str2[11]: 0073FDDB , 0x00 ,
Get the idea??

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:
#include <stdio.h>
int main()
{
  int i, x, y, b[5][3];
  i=0;

  for(y=0; y < 5; y++) {
	for(x=0; x < 3; x++) {
      b[y][x] = i++;
    }
  }
  printf("\nA simple 2D Array: b[5][3]  ( I think of as b[y][x] ) \n\n");
  printf(" y   b[y]    b[y][0]  b[y][1]  b[y][2]\n");

  for(y=0; y < 5; y++) {
	printf("\n%2x  %x ", y, b[y]);
    for(x=0; x < 3; x++) {
      printf("%8x ", b[y][x]);
    }
  }

  printf("\n");
  return 0;
}
Output:
Code:
A simple 2D Array: b[5][3] ( I think of as b[y][x] ) x____________0________1________2_________ y | b[y] b[y][0] b[y][1] b[y][2] | 0 | 63fdbc 0 1 2 1 | 63fdc8 3 4 5 2 | 63fdd4 6 7 8 3 | 63fde0 9 a b 4 | 63fdec c d e
Hope that's not going too far , but it gives a glimpse of array possabilities.
So much for tonights Cprog time allotment... Good Night...
Howard;
  #4  
Old 05-Oct-2007, 05:11
davis
 
Posts: n/a

Re: What is an array?


Quote:
Originally Posted by Howard_L
Code:
Element Address Hex ascii str2[ 0]: 0073FDD0 , 0x48 , H str2[ 1]: 0073FDD1 , 0x65 , e str2[ 2]: 0073FDD2 , 0x6c , l str2[ 3]: 0073FDD3 , 0x6c , l str2[ 4]: 0073FDD4 , 0x6f , o str2[ 5]: 0073FDD5 , 0x20 , str2[ 6]: 0073FDD6 , 0x57 , W str2[ 7]: 0073FDD7 , 0x6f , o str2[ 8]: 0073FDD8 , 0x72 , r str2[ 9]: 0073FDD9 , 0x64 , d str2[10]: 0073FDDA , 0x21 , ! str2[11]: 0073FDDB , 0x00 ,
Get the idea??

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 GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 09:37.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.