malloc; multidimensional array; c - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

malloc; multidimensional array; c

abonghit1abonghit1 Posts: 71Member
Hello,

I am always making a workaround so I don't have to allocate memory for a multidimensional array in C. I always just make an array like
[code]
char array[MAX_ELEMENTS][2+1];
[/code]
if I need an unknown number of 2 position strings.
How would I work this out if I had some function like:
[code]
char *array[2];
count = GetNumberOfElementsIneed();
*array = malloc(count);
[/code]
and then be able to access the array contents like array[1][0] etc..?

any help would be great. i don't care how i have to do it.

Thank you!



Comments

  • abcabc Posts: 443Member
    [b][red]This message was edited by abc at 2004-5-28 8:8:3[/red][/b][hr]
    : Hello,
    :
    : I am always making a workaround so I don't have to allocate memory for a multidimensional array in C. I always just make an array like
    : [code]
    : char array[MAX_ELEMENTS][2+1];
    : [/code]
    : if I need an unknown number of 2 position strings.
    [green]then you need to hold an array of pointers to your strings[/green]
    : How would I work this out if I had some function like:
    : [code]
    : [red]char **array;[/red]
    : count = GetNumberOfElementsIneed();
    : array = [red](char**)[/red]malloc(count[red]*sizeof(char*)[/red]);
    [red]//then allocate the individual arrays
    for(int i = 0; i < count; ++i)
    array[i] = (char*)malloc((2+1)*sizeof(char) );
    [/red]
    : [/code]
    : and then be able to access the array contents like array[1][0] etc..?
    [green]Don't forget to free each pointer of array and then array itself when you're done with it. Also, C does not require casting the pointer returned by malloc(), but C++ does, so it's an useful habit to pick.[/green]


  • DarkLord2003DarkLord2003 Posts: 174Member
    [b][red]This message was edited by DarkLord2003 at 2004-5-27 22:28:54[/red][/b][hr]
    : : Hello,
    : :
    : : I am always making a workaround so I don't have to allocate memory for a multidimensional array in C. I always just make an array like
    : : [code]
    : : char array[MAX_ELEMENTS][2+1];
    : : [/code]
    : : if I need an unknown number of 2 position strings.
    : [green]then you need to hold an array of pointers to your strings[/green]
    : : How would I work this out if I had some function like:
    : : [code]
    : : [red]char **array;[/red]
    : : count = GetNumberOfElementsIneed();
    : : array = [red](char**)[/red]malloc(count[red]*sizeof(char*)[/red]);
    : [red]//then allocate the individual arrays
    : for(int i = 0; i < count; ++i)
    : array[i] = (char*)malloc(2*sizeof(char) );
    : [/red]
    : : [/code]
    : : and then be able to access the array contents like array[1][0] etc..?
    : [green]Don't forget to free each pointer of array and then array itself when you're done with it. Also, C does not require casting the pointer returned by malloc(), but C++ does, so it's an useful habit to pick.[/green]
    :

    I think you have count and 2 mixed up, they should be interchanged. What you have would create an array for count number of strings and each string would be 2 chars longs



  • abcabc Posts: 443Member
    : I think you have count and 2 mixed up, they should be interchanged. What you have would create an array for count number of strings and each string would be 2 chars longs

    Well, there was one mistake - the strings should have had size of 3, not 2. For your point, look at his initial static allocation and tell me what you see.
  • abonghit1abonghit1 Posts: 71Member
    : : I think you have count and 2 mixed up, they should be interchanged. What you have would create an array for count number of strings and each string would be 2 chars longs
    :
    : Well, there was one mistake - the strings should have had size of 3, not 2. For your point, look at his initial static allocation and tell me what you see.
    :
    Thanks folks! I've never thought to use a pointer type in a call to malloc, but it makes sense.
    I appreciate the help.
    -Mike
Sign In or Register to comment.