Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

malloc multidimensional array question

Status
Not open for further replies.

bobetko

Programmer
Jan 14, 2003
155
US
Several malloc questions:

1. How I do malloc for this:

char *mystr[1];

(In my program *mystr is part of structure)
I want durring execution to increase (malloc) number of array elements to 5. and allocate enough space so I can have strings up to 5 characters.
Something like this:

mystr[0]="abcde";
mystr[1]="12345";
mystr[2]="fghij";
mystr[3]="aa";
mystr[4]="bb";


When I type above lines into program and compile it it's usually working fine, but when I don't know string values (I am reading them from external file) I get strange results. That's telling me that I don't know how to properly do malloc.

2. or malloc for this:
char mystr[1][1];
how to malloc this to have space for, let's say, mystr[5][5]

3. Let's say I have structure
struct list {
int counter;
char *mystr[1];
};

When I do malloc I need only to think about *mystr since it's only element of the structure whose size will change?

Thanks.
 
A bit like this
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* we're going to make a char mystr[counter][6] array here */
struct list {
    int counter;
    char (*mystr)[6]; /* a number of strings, max length of 5 (and a \0) */
};

void extend ( struct list *list ) {
  int newcounter = list->counter + 5;
  void *p = realloc ( list->mystr, sizeof(*list->mystr) * newcounter );
  if ( p != NULL ) {
    /* success, update our values */
    list->mystr   = p;
    list->counter = newcounter;
  } else {
    printf( "Couldn't extend\n" );
  }
}

int main ( ) {
  struct list list = { 0, 0 };        /* an empty list */

  extend ( &list );                   /* add some elements */

  strcpy ( list.mystr[0], "hello" );  /* use some */
  strcpy ( list.mystr[1], "world" );  /* or strcpy them from data from a file */
  printf( "%s %s\n", list.mystr[0], list.mystr[1] );

  return 0;
}
The mystr pointer is a bit weird, but it is useful when you know that all your array elements are the same size. It does save having to call malloc to allocate space for each string as well.

Like this example
Code:
/* we're going to make a char *mystr[counter] array here */
struct list {
  int counter;
  char **mystr;
};

void extend ( struct list *list ) {
  int newcounter = list->counter + 5;
  void *p = realloc ( list->mystr, sizeof(*list->mystr) * newcounter );
  if ( p != NULL ) {
    int i;
    /* success, update our values */
    list->mystr = p;
    for ( i = list->counter ; i < newcounter ; i++ ) {
      /* allocate each string */
      list->mystr[i] = malloc ( sizeof(*list->mystr[i]) * 6 );
    }
    list->counter = newcounter;
  } else {
    printf( "Couldn't extend\n" );
  }
}
As far as main is concerned, the result is still the same, in that you can now copy up to 5 strings, each up to 5 characters in length.
Just call extend() when you want more strings.

--
 
1 and 2

If you declare your array that way, you're using static memory. It's reserved at the beginning and cannot be changed.

If you want to have it dynamic, you must declare it as a pointer

char **mystr

and allocate the memory as you want to use it.

Take a look a calloc and realloc.

3

Yes, assuming you've already reserved memory for the structure, you just need to take care of the pointer.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top