Why an array of char* and not char?
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
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
Try doing a double-scripted array of chars (char month[][])
-fel
Or just make it char* months[12] = {
"january",
"february",
etc...etc..
}
Edited by - mbarela on 3/2/00 5:16:09 PM
"january",
"february",
etc...etc..
}
Edited by - mbarela on 3/2/00 5:16:09 PM
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:
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 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 topic is closed to new replies.
Advertisement
Popular Topics
Advertisement