Thread: Help with calloc and pointers to strings.

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Help with calloc and pointers to strings.



    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;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You should pass the allocated memory to read_title():
    Code:
        read_title(fp, Name[n]);
    Since title is a local variable in read_title(), you cannot return it's address. Instead declare read_title() like this:
    Code:
    void read_title(FILE *fp, char *title)
    {
    
      fscanf(fp,"%s\t",title);    
      /* printf("title %s  \t", title);*/
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing # from .dat file in Matrix using Pointers
    By dakarn in forum C Programming
    Replies: 5
    Last Post: 11-29-2008, 06:46 PM
  2. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. need help with structs and pointers
    By rozner in forum C Programming
    Replies: 4
    Last Post: 04-07-2003, 03:33 PM