NEWBIE: malloc an array????

From: mike79 (mike79_at_iprimus.com.au)
Date: 11/05/03


Date: 4 Nov 2003 16:37:46 -0800

Hi all!

I am having problems properly malloc'ing an array. The problem is, I
have a structure called "array" which contains 2 elements, "int
arraySize" (how many elements are in the array) and "wordArray" (the
actual array itself).

The "word structure" consists of 2 elements, "int occurrence" (the
number of times that particular word occurs in say a sentence, and
"char *string" (the actual word itself).

So the two structures look like this:

typedef struct words
{
        int occurrence;
        char *string;
}word;

typedef struct arrays
{
        int arraySize;
        word *wordArray;
}array;

So what I am trying to do is create 1 structure of "array" to keep
track of the number of elements(words) contained in that array, and
the array itself.

The "array" structure contains an array of "word" structures. And the
"word" structure contains an array of "chars" i.e. a string.

So just say I wish to allocate memory for an array of 20 "word"
elements, each word element has a string of 30 chars. How would I do
this?

I tried the code below but i keep getting errors.. please help me!

        array *array1;
        int element, number;

        array1 = malloc(1); // Allocating memory to the array
        
        number = 20; // 20 word elements in the array
        array1->wordArray = malloc(number * sizeof *array->wordArray);

        element = 0;
        while (element < number)
        {
                array1->wordArray[element].string = malloc(30);
                element++;
        }

Thank you all so much for your help!
mike79



Relevant Pages

  • Re: FASTEST way to try all strings (a until ZZZZZZZZZZZZZZZZZZZZZZZZ)
    ... > It will be a very huge table so I in my opinion. ... > When it would be used, than it should be converted to a string, however ... >> How would an array of Byte be any faster then an array of Char? ... >> array of Byte is needed, however the OP suggested Chars (A to Z, a to z ...
    (microsoft.public.dotnet.languages.vb)
  • Re: FASTEST way to try all strings (a until ZZZZZZZZZZZZZZZZZZZZZZZZ)
    ... > It will be a very huge table so I in my opinion. ... > When it would be used, than it should be converted to a string, however ... >> How would an array of Byte be any faster then an array of Char? ... >> array of Byte is needed, however the OP suggested Chars (A to Z, a to z ...
    (microsoft.public.dotnet.general)
  • Re: communication via gpib-drivers in c#
    ... > is there a way converting this decimal type with the ascii-code in it into ... > string or an array of chars? ...
    (microsoft.public.dotnet.languages.csharp)
  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)
  • Re: CreateMapFile e LPCWSTR
    ... An alternative is to calculate the size of each string and then allocate enough memory for those strings + 2-4 bytes for a counter preceding each string. ... each string and placing them in an array, ...
    (microsoft.public.pocketpc.developer)