Hi:

I want to use calloc with strings so I can read the words read from a datafile
whose size varies. hence, I am using calloc. My ttest file words.dat has 10
words: dynamic, memory,strings,array,character, serious,stat,covariance,correlation,signal.

See below for the code 'scalloc.c' that wrote.
I get this output:-------------------------------------
'words.dat' has 10 lines.

name[ 1] dynamic name[ 2] memory name[ 3] strings name[ 4] array name[ 5] character
name[ 6] serious name[ 7] stat name[ 8] covariance name[ 9] correlation name[10] signal
signal signal signal signal signal signal signal signal signal signal
----------------------------------------------------------

I don't understand how I am not able to read the words once I am outside
of the for loop?

J


Code:
/* scalloc.c */

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

char *read_title(FILE *fp);

/*** Main program ***********************************************/
int main(int argc, char **argv)
{
  char          *filename;                      /* name of input datatfile */
  char          *tempfile;                      /* name of temporary file  */
  char          *shell_cmd;                     /* command line pointer    */
  char          *til;
  FILE          *fp;                            /* pointer to the datafile */
  int Nmax;
  int  i,j,n;                                        /* counters */
  double rms[3];                                  /* pointer to rms values */
  char **Name;                           /* pointer to the pointer of Name */

  filename   = (char *)calloc(CHAR_MAX, sizeof(char));
  tempfile   = (char *)calloc(CHAR_MAX, sizeof(char)); 
  filename = argv[1];
  sprintf(tempfile,"%s.%s", filename,"temp");  
  /* Use word count and know how much data to handle.*/
  /* Determine how many strings there are to be stored! */
  shell_cmd = (char *)calloc(CHAR_MAX, sizeof(char)); 
  sprintf(shell_cmd,"wc -l %s > %s", filename, tempfile);

  if ( -1 == system(shell_cmd) ){
    printf("error with shell_cmd %s", shell_cmd);
    exit(1);
  }


  /* first, determine how many matrices there are to be stored! */
  fp = fopen(tempfile,"r"); 
    
  /* reads the value of n and the name of the temp file*/
  til = (char *)calloc(CHAR_MAX, sizeof(char)); 
  fscanf(fp,"%d",&Nmax);
  fscanf(fp,"%s",til);
  fclose(fp); 

  printf(" '%s' has %d lines.  \n\n",filename,Nmax);

  /* remove temporary file once it is no longer needed */
  sprintf(shell_cmd,"rm -f %s ", tempfile);
  if ( -1 == system(shell_cmd) ){
    printf("error with removing temp file: %s\n", shell_cmd);
    exit(1);
  }

  /*  Allocate Nmax pointers for the pointer to Name  */
  Name = (char **)calloc(Nmax, sizeof(char **));


  fp = fopen(filename,"r"); 
  for (n=0;n<Nmax;n++){
    Name[n]= (char *)calloc(CHAR_MAX, sizeof(char));
    Name[n] = read_title(fp);

    if (n==4) 
      printf("name[%2d] %9s\n",n+1,Name[n]); 
    else 
      printf("name[%2d] %9s\t",n+1,Name[n]); 
  }
  printf("\n");

  for (n=0;n<Nmax;n++)
      printf("%9s\t",Name[n]); 

  printf("\n");
  for (n=0;n<Nmax;n++)
    free(Name[n]);
  free(Name);

  return 0;
}


/*      Read the title for the eqparms.    */
char *read_title(FILE *fp)
{
  char *title;

  fscanf(fp,"%s\t",title);    
  /* printf("title %s  \t", title);*/
  return title;
}