• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [OT] An array of C-Strings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [OT] An array of C-Strings


  • Subject: Re: [OT] An array of C-Strings
  • From: email@hidden
  • Date: Sat, 26 Jan 2002 23:35:49 -0500

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];

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.


  • Follow-Ups:
    • Re: [OT] An array of C-Strings
      • From: Sam Goldman <email@hidden>
References: 
 >[OT] An array of C-Strings (From: Sam Goldman <email@hidden>)

  • Prev by Date: [OT] An array of C-Strings
  • Next by Date: Re: [OT] An array of C-Strings
  • Previous by thread: [OT] An array of C-Strings
  • Next by thread: Re: [OT] An array of C-Strings
  • Index(es):
    • Date
    • Thread