On Tue, Oct 28, 2008 at 6:07 PM, Andre Fecteau <[EMAIL PROTECTED]> wrote:
> I'm really new to c programming, so bear with my simple question. I'm a
> Perl programmer, who has just started learning C(not even experienced
> at Perl). I can get around and usually manage through lots of work to get
> what I need done, done! In Perl I can assign a string to an array element.
>
> ex: $array[0] = "example";
>
>
> In C it seems that you can only assign an individual letter to each element.
>
> ex: array[0] = 'c';
>
> Whenever I try to do what I do in Perl all the time, in c I get compile
> errors. What am I doing wrong?
> Can I even do this in c?
>
> If you can't do that in c, that's OK. It's just really convenient to do it
> the Perl way!
Yay Perl! (I'm a big Perl geek also)
It can be done in C, it just takes a little more work. If you want to
declare an array of strings, you will do something like
char *str_array[10];
then you must allocate memory for each array item (using malloc) and
then copy your string into each array element
strcpy(str_array[0], "example");
There are things you can do to make the code more robust (like
verifying the string you are copying is not too long for the allocated
memory, etc). String manipulation is not too fun in C and much more
fun in Perl.
Personally, I'd not even bother with C and use C++ instead, it has a
string class which is much easier to use (and you can do stuff in C++
with container classes than can do things similar to Perl arrays and
hashes).
One thing you should look into learning and using is the pcre library
for C & C++ -- Perl Compatible Regular Expression library!
-- Brett
------------------------------------------------------------
"In the rhythm of music a secret is hidden;
If I were to divulge it, it would overturn the world."
-- Jelaleddin Rumi