Re: [OT] An array of C-Strings
Re: [OT] An array of C-Strings
- Subject: Re: [OT] An array of C-Strings
- From: Sam Goldman <email@hidden>
- Date: Sat, 26 Jan 2002 20:37:45 -0800
>
Um,
>
You need to allocate space for your days array on the stack or on the
>
heap.
>
>
When you say:
>
>
char **days;
>
>
That just declares a pointer to a char pointer, which points into No
>
Man's Land when you say:
>
>
days [i] = word;
>
>
To make this sample work, you should probably declare the days array as:
>
>
char *days [7];
I know that, but if you read carefully I said that in the actual use I don't
know how many 'word's there are and instead of putting a really big number
into days, I want to have it get bigger every time an entry is put into it.
>
Then days points to an array of 7 char pointers on the stack. This is
>
true in any flavor of C/ObjC/C++. Its a memory thing, not a C++ thing.
>
>
On Saturday, January 26, 2002, at 11:22 PM, Sam Goldman wrote:
>
>
> Sorry for being off topic, but I don't have a C++ book yet
>
> (recommendations?) and I need a pretty simple question answered.
>
>
>
> I want to have an array of C-Strings (which as you know are arrays
>
> themselves). The C-Strings in the array would be added at runtime and I
>
> don't know how many there will be or how long each one will be.
>
>
>
> Here's what I have come up with so far.
>
>
>
> #include <iostream.h>
>
>
>
> int main()
>
> {
>
> char **days;
>
> char *word = "The length of the word would be variable";
>
> int i;
>
>
>
> for (i = 0; i < 7; i++)
>
> {
>
> days[i] = word;
>
> }
>
> for (i = 0; i < 7; i++)
>
> {
>
> cout << days[i] << endl;
>
> }
>
>
>
> return 0;
>
> }
>
>
>
> It compiles with gcc, but during runtime it quits with a Bus Error
>
> message.
>
>
>
> Thanks,
>
> Sam
>
> _______________________________________________
>
> cocoa-dev mailing list | email@hidden
>
> Help/Unsubscribe/Archives:
>
> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
> Do not post admin requests to the list. They will be ignored.