Coder's Guild Mailing List

Re: passing arrays

Posted by Eian on 2001-04-08

Mark,

No problem helping you out. Don't worry, just ask! :)

I think that the main problem that you are running into at this point is
that when you create an array of 200 pointers, they are just that
pointers. There is no actual "memory" allocated for them to point at. Make
sense? 

With this in mind, there is a very large difference between setting a
pointer equal to null and setting the pointer to point to a string with
the first character being the null character.

Dictionary[0] = NULL;

This takes the pointer pointed to by Dictionary[0] (the first pointer in
the array) and says that it points to nothing (NULL). The following code
actually makes the pointer point to a string that has the null character
as the first character ("\0").

#define NULL_STRING_LENGTH 1

Dictionary[0] = malloc(NULL_STRING_LENGTH); /* allocate the memory to
					       place the string in */
strcpy(Dictionary[0],"\0"); /* actually place the string there */
Dictionary[0][0] = '\0'; /* an alternate to the above line of code. This
			    code simply makes the first character of the 
			    block of memory that the first pointer points
			    at the null character */

Does this make sense? Hopefully you now know why you can't simply do
something like:

Dictionary[0] = "A"; 


Write me back and let me know what you think. I don't have a whole lot of
time at this point to write more. Write me backa nd let me know if you
understand what I am saying or if I need to expound on it more...

Stay cool,

Eian

> Hi again, is it possible to initialise an array of pointers to NULL  i.e. 
> "\0" .
> 
> For example an array of size 10 maybe, with the first 4 pointers pointing 
> to maybe Happy, Sleepy, Grumpy , Dock, and the rest being from 4 to 9 set 
> as null bytes.
> 
> Basically, can I shove an array full of null bytes??
> 
> my current code is as follows:-
> 
> char *Dictionary[200];  /*Dictionary*/
> int j = 4;
> 
> Dictionary[0] = "A";
> Dictionary[1] = "B";
> Dictionary[2] = "C";
> Dictionary[3] = "D";
> 
> /*initialise dictionary to NULL*/
> for (j = 4; j < 200; j++)
>          Dictionary[j] = "\0";
> 
> Many thanks
> 
> 
> 
> Mark R Burford
> Undergraduate in The Dept. Of
> Electronics And Computer Science
> University Of Southampton
> Mobile: 07730 437375
> Email:  mrb199@xxx.xxxxx.xx.xx
> 
> > 
> 

                      -=-=-=-=-=-=-=-=-=-
         And if the dam breaks open many years too soon
             And if there is no room upon the hill
          And if the cloud bursts, thunder in your ear
       And if your head explodes with dark forbodings too
           I'll see you on the dark side of the moon
                          -Pink Floyd
                      -=-=-=-=-=-=-=-=-=