"Neil Fallon" <nf****@comcast.net> wrote in message news:<OC8Qb.105267$sv6.503992@attbi_s52>...
I'm new to C and having some problems passing a string to an array. I don't
know if I can do a direct assignment. I have a string (make) that I would
like to add to the array (itemList). itemList should be an array of 150
items. When I try to do a direct assignment the debugger tells me that I am
trying to pass a null string. If I display what is in make it has the data
there.
Char itemList[150];
itemList[0] = make;
Your description is not entirely clear. Others have interpreted your
question as wanting to copy the contents of 'make' into 'itemList'
so that 'itemList' is the same as 'make'. I'm guessing that you
want your 'itemList' to be a list of up to 150 strings like 'make'.
If I'm correct, your error is in the definition of 'itemList'.
You want something along the following lines:
char *itemList[150];
itemlist[0] = make;
itemlist[1] = anotherString;
You should perhaps also consider how you will know that a particular
entry in 'itemList' currently points to a string. The normal way to
avoid confusion in this area is to make sure that every entry in
'itemList' is initialized to a null pointer before you start filling
it. This can usually be done where the list is defined:
#include <stddef.h>
char *itemList[150] = { NULL };
You can subsequently check if an entry has been filled in yet by
comparing its value to NULL.