Help with calloc (size of the elements)

This is a discussion on Help with calloc (size of the elements) within the C Programming forums, part of the General Programming Boards category; Hi everybody, I am new to this forum so I apologize in advance for any mistake. Here is my problem: ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    Post Help with calloc (size of the elements)

    Hi everybody, I am new to this forum so I apologize in advance for any mistake.

    Here is my problem:

    I am writting a code in C that reads words from a txt document and puts them in an array. I don't know the number of the words so the size of the array must be dynamic. So I use the command calloc. The problem I think comes from the fact that I make a new type of element named word in order to make the size of the elements of the dynamic array word byte long. Here is my code:

    Code:
    #include <stdio.h>
    #define IN 1
    #define OUT 0
    
    main() {
           int nc, nw, state;
           char c;
           FILE *fp;
           
           int i, n;
           char * DAT;
           typedef char word[20];
           
           
           state = OUT;
           fp = fopen("C:naasas.txt", "r");
           nw = nc = 0;
           while (fscanf(fp, "%c", &c) &&  !feof(fp)) {
                 ++nc;
                 if(c == ' ' || c == '\n' || c == '\t') {
                      state = OUT;
                 }
                 else if(state == OUT) {
                      state = IN;
                      ++nw;
                 }
                 if(c == EOF) {
                      printf("the end\n");
                 }
           }
           fclose (fp);
           printf("%d %d\n", nc, nw);
          
           fp = fopen("C:naasas.txt", "r");
           DAT = (char*) calloc (nw, sizeof(word));
           if (DAT == NULL) {
                   printf("XXXXX");
           }
           else {
                printf("YYYYYY");
                for (i = 0; i <= nw-1; i++) {
                                             fscanf(fp, "%s", &DAT[i]);
                }
           }
           fclose (fp);
            
           
           for (i = 0; i <= nw-1; i++) {
               printf("%s \n", DAT[i]);
           }
    
           system("PAUSE");
           return 0;
    }
    I would appreciate it if you can tell me my mistake and help me fix it. I am using Dev-C++ on windows 7.
    Thank you in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
           char c;
    ...
                 if(c == EOF) {
    EOF is not a valid char type value. You need c to be an int.
    Code:
                for (i = 0; i <= nw-1; i++) {
                                             fscanf(fp, "%s", &DAT[i]);
                }
    DAT is just a pointer to a character. You are treating it as if it were an array of pointers to characters (or a char ** which has a series of char* allocated for it), which it is not.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You also need to check the return value of fopen() and exit if it doesn't work. I'm betting the file never gets opened because your file path is invalid in Windows...

    Code:
    fp = fopen("C:naasas.txt", "r");
    
    // should be --------------------------
    
    FILE *fp = NULL;  // initialize file handle
    
    
    fp = fopen("C:\\naasas.txt", "r");   // open file
    if (fp == NULL)                    // check for errors
      { printf("The file did not open correctly! \n\n");
         exit(1); }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count how many elements used in calloc
    By dunsta in forum C Programming
    Replies: 3
    Last Post: 05-05-2010, 03:17 AM
  2. calloc
    By goran00 in forum C Programming
    Replies: 27
    Last Post: 04-07-2008, 01:50 PM
  3. Replies: 5
    Last Post: 12-05-2007, 04:39 PM
  4. Why use calloc()?
    By dwks in forum C Programming
    Replies: 8
    Last Post: 07-20-2005, 09:22 AM
  5. calloc in C++
    By k_w_s_t_a_s in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2003, 08:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21