help with function to create dynamic two-dimensional char array 
Author Message
 help with function to create dynamic two-dimensional char array

I'm having trouble creating a function that passes a two
dimensional character array back to main.

Here's the goal:  A text file contains a list of items, lets say
color names.  The length of the list of items is unknown (to the
program), and the item names are of unknown length. (I'm going to make
sure the items will be less than 256 characters each).

The data file will look like this:

violet
yellow
aqua
sky blue
fire engine red

(.... etc.)

I want a function that reads in each item, stores them in an array,
and returns the array to the main program. The array needs to be
accessible for the life of the program. The last item will be NULL.

I've tried this code and it appears to work sometimes and not other
times. What am I doing wrong?

Also, what if I don't know that the items are going to be less than
256 characters each -- can I dynamically allocate space for the item
length as well?

#include <stdlib.h>
#include <stdio.h>

char ** get_color_names()
   {
   char ** name_array;
   char input_line[256];
   char *p;
   FILE *in;
   int x=0;

   in=fopen("colors.txt","r");
   if (in==NULL)
      return NULL;
   while (fgets(input_line,255,in)!=NULL)
      {
    /* Add code to get rid of trailing \n */
    p=(char *)malloc (sizeof(char)*strlen(input_line)+1);
    if (p==NULL)
       {
        printf("Can't allocate memory\n");
        return NULL;
       }
    strcpy(p,input_line);
    name_array[x++]=p;
      }
   fclose(in);
   name_array[x][0]='\0';
   return name_array;

   }

main ()
        {
        int x;
        char **colors;

        colors=get_color_names();

        for (x=0;*colors[x]!=NULL;x++)
                printf("%s\n",colors[x]);
        return;
        }

Please don't send me to the FAQ. I've studied Question 6.16
http://www.*-*-*.com/ ~scs/C-faq/q6.16.html and it doesn't seem to
address functions, and I'm having a difficult time translating
the answer to char array from an int array.

Thanks for any help.

--

To reply by e-mail, it's tbarmann instead of nospam

--

To reply by e-mail, it's tbarmann instead of nospam



Tue, 04 Sep 2001 03:00:00 GMT  
 help with function to create dynamic two-dimensional char array
: I'm having trouble creating a function that passes a two
: dimensional character array back to main.

: Here's the goal:  A text file contains a list of items, lets say
: color names.  The length of the list of items is unknown (to the
: program), and the item names are of unknown length. (I'm going to make
: sure the items will be less than 256 characters each).

: The data file will look like this:

: violet
: yellow
: aqua
: sky blue
: fire engine red

: (.... etc.)

: I want a function that reads in each item, stores them in an array,
: and returns the array to the main program. The array needs to be
: accessible for the life of the program. The last item will be NULL.

: I've tried this code and it appears to work sometimes and not other
: times. What am I doing wrong?

Well, I've added a slightly more reliable approach to the problem
to the end of the file.  Your terminating '\0' was writing to
memory you didn't own.

: Also, what if I don't know that the items are going to be less than
: 256 characters each -- can I dynamically allocate space for the item
: length as well?

Handling input lines of indeterminate length is tricky; it's easier
to do as you've done, and allocate a buffer longer than any known
input size.  You can't dynamically allocate space for something
unless you know its size, and the easiest way to do that is to
read it in.

Will

: #include <stdlib.h>
: #include <stdio.h>

: char ** get_color_names()
:    {
:    char ** name_array;
:    char input_line[256];
:    char *p;
:    FILE *in;
:    int x=0;

:    in=fopen("colors.txt","r");
:    if (in==NULL)
:       return NULL;
:    while (fgets(input_line,255,in)!=NULL)
:       {
:     /* Add code to get rid of trailing \n */
:     p=(char *)malloc (sizeof(char)*strlen(input_line)+1);
:     if (p==NULL)
:        {
:       printf("Can't allocate memory\n");
:         return NULL;
:        }
:     strcpy(p,input_line);
:     name_array[x++]=p;
:       }
:    fclose(in);
:    name_array[x][0]='\0';
:    return name_array;

:    }

: main ()
:         {
:       int x;
:       char **colors;

:       colors=get_color_names();

:       for (x=0;*colors[x]!=NULL;x++)
:               printf("%s\n",colors[x]);
:       return;
:       }

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int get_color_names(char *name_array[], int size)
{
  char *p, input_line[256];
  int x;
  FILE *in;

  if ((in = fopen("colors.txt", "r")) == NULL)
        return NULL;
  for (x = 0; x < size && fgets(input_line, sizeof(input_line), in); x++) {
        if ((p = strrchr(input_line, '\n')) != NULL)
                *p = '\0';
        if ((p = malloc(strlen(input_line) + 1)) == NULL) {
                printf("Can't allocate memory\n");
                while (x != 0)
                        free(name_array[--x]);
                return NULL;
        }
        strcpy(p, input_line);
        name_array[x] = p;
  }
  if (x != size) name_array[x] = NULL;
  fclose(in);
  return x;

Quote:
}

#define MAX 3

int main(void)
{
  int x, size;
  char *colors[MAX];

  size = get_color_names(colors, sizeof(colors)/sizeof(colors[0]));

  for (x = 0; x < MAX && colors[x] != NULL; x++)
        printf("%s\n", colors[x]);
  return 0;

Quote:
}



Wed, 05 Sep 2001 03:00:00 GMT  
 help with function to create dynamic two-dimensional char array

Quote:

> Here's the goal:  A text file contains a list of items, lets say
> color names.  The length of the list of items is unknown (to the
> program), and the item names are of unknown length. (I'm going to make
> sure the items will be less than 256 characters each).
>...
> Also, what if I don't know that the items are going to be less than
> 256 characters each -- can I dynamically allocate space for the item
> length as well?

The pages

        http://www.eskimo.com/~scs/cclass/notes/sx11c.html
        http://www.eskimo.com/~scs/cclass/asgn.beg/PS6.html (exercise 7)
and     http://www.eskimo.com/~scs/cclass/asgn.beg/PS6A.html

have some information on this.

(The main problem in your code is that you never allocated any
space for name_array.)

Quote:
> Please don't send me to the FAQ.

Done.

                                        Steve Summit



Wed, 05 Sep 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Dynamic allocation and reallocation of two dimensional arrays

2. TROUBLES WITH TWO-DIMENSIONAL DYNAMIC ARRAY

3. Dynamic memory allocation of two dimensional arrays

4. Strcpy to Two Dimensional Char Array

5. How do I use Malloc to dynamically create a two dimensional array

6. Help with two-dimensional arrays

7. Two dimensional integer array - HELP!

8. Help with two dimensional arrays

9. Two dimensional dynamic allocation using malloc

10. HELP! dynamic array of char*

11. Two-dimensional Arrays

12. fgets and two dimensional arrays

 

 
Powered by phpBB® Forum Software