Why an array of char* and not char?

Started by
4 comments, last by IWO0v0OWI 24 years, 2 months ago
Can anyone explain why this array of 12 chars is an error: example: char month[12] = { "January", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; I know that it should be like this: char *month[12] = { ... }; But why? Why use pointers? ...and this one is not? int arr[10] = { 1,2,3,4,5,6,7,8,9,10}; Edited by - IWO0v0OWI on 3/2/00 5:25:22 PM Edited by - IWO0v0OWI on 3/2/00 5:25:46 PM
"There's always another way around"
Advertisement
Because for a char, you get exactly 1 letter. So when you try to feed in a whole bunch of letters, it gets a little annoyed.
Try doing a double-scripted array of chars (char month[][])

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Or just make it char* months[12] = {
"january",
"february",
etc...etc..
}

Edited by - mbarela on 3/2/00 5:16:09 PM
Mike BarelaMikeB@yaya.com
When the compiler sees a string, "some text", it puts this in the datamemory and gives a pointer to it. In your example you are initializing month[12] with 12 pointers to char.
I guess I''m a wierdo, but I hate pre-defined (static) arrays.

this is my wierd way of doing it:

// create a pointer to a pointer to a char.char **months = 0;// make the first pointer an array of 13 char pointers.months = new char* [ 13 ];months[ 0 ] = "Febtober";...months[ 12 ] = "Smarch";


heh, i know, there are 13 months there...
Its a joke. See if you can guess where its from?

on a serious note:
Technically, I wouldnt have used native C strings anyhow. I think they are awfully cryptic and not nearly as neat as my own string class

===============================================
I saw a man pursuing the horizon;
Round and round they sped. I was disturbed at this; I accosted the man.
"It is futile," I said, You can never -- "

"You lie," he cried, And ran on.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
Lousy Smarch weather!




This topic is closed to new replies.

Advertisement