-4

I wrote a simple function to count the number of non-hidden files in a directory. However I noticed that when I used ++ to increment the count value I got weird results, like negative numbers and really large numbers. When I switch the line *count++; to *count = *count + 1; the function behaves as I expected. Can someone explain this behavior?

To use this example program pass the path to the directory as the first argument.

#include <stdio.h>
#include <dirent.h>

int count_files_directory(unsigned int *count, char *dir_path)
{
    struct dirent *entry;
    DIR *directory;

    /* Open the directory. */
    directory = opendir(dir_path);
    if(directory == NULL)
    {
        perror("opendir:");
        return -1;
    }

    /* Walk the directory. */
    while((entry = readdir(directory)) != NULL)
    {
        /* Skip hidden files. */
        if(entry->d_name[0] == '.')
        {
            continue;
        }

        printf("count: %d\n", *count);

        /* Increment the file count. */
        *count++;
    }

    /* Close the directory. */
    closedir(directory);

    return 0;
}

int main(int argc, char *argv[])
{
    int rtrn;
    unsigned int count = 0;

    rtrn = count_files_directory(&count, argv[1]);
    if(rtrn < 0)
    {
        printf("Can't count files\n");
        return -1;
    }

    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2trill2spill
  • 1,333
  • 2
  • 20
  • 41
  • Downvoter mind explaining? – 2trill2spill Sep 11 '15 at 18:13
  • 1
    I think `*count++` means `*(count++)` (increment the pointer), but you want `(*count)++` (increment the pointed value) – ikegami Sep 11 '15 at 18:14
  • 1
    Take a look at operator precedence – 7VoltCrayon Sep 11 '15 at 18:14
  • 3
    Most of your code is completely irrelevant. – juanchopanza Sep 11 '15 at 18:14
  • 1
    Yes, while it's easy to just copy and paste, it would be better to provide a minimal code sample for these kinds of questions. – Zong Sep 11 '15 at 18:17
  • 1
    *When in doubt, paren it out.* – Qix - MONICA WAS MISTREATED Sep 11 '15 at 18:18
  • 1
    Since he didn't know it was about operator precedence, the full code provides context useful in answering the question. – Dmitri Sep 11 '15 at 18:18
  • 1
    To someone stumbling across it on google, the way this question is posed may be too involved/convoluted to be useful given the simplicity of the answers, but I don't get why people are downvoting it... and on top of that no one explained their -1, so the OP has no chance to improve it... unnecessary hate/condescention IMO. IT only makes sense to mark it as a duplicate given prior knowledge of the answer... which defeats the purpose of asking the question in the first place. – Dmitri Sep 11 '15 at 18:22
  • @ juanchopanza Providing the full code is not related to problem solving skills, it just means I don't fully understand how to format a question for stack overflow. – 2trill2spill Sep 11 '15 at 18:33
  • 1
    @juanchopanza Gees... remind me never to make a silly mistake or unintentionally display ignorance on this Q & A site.. – Dmitri Sep 11 '15 at 18:37
  • 2
    Isolating the scope of your problem and asking for help *effectively* is certainly related to problem solving skills. Playing around with small toy programs is a good way to explore surprising behaviors, and it also more effectively conveys the problem to others. – Zong Sep 11 '15 at 18:38
  • 1
    @Dmitri I think some people here are annoyed at a general trend on this site where people paste all their code with minimal effort and expect others to debug it for them. It's hostility towards laziness, which is understandable. – Zong Sep 11 '15 at 18:42
  • @ZongZhengLi I do agree with hostility toward laziness in general. While the OP did meet my elementary due-dilligence threshold (he at least tried it without the ++ operator), I was surprised by how much higher that threshold is for the majority of other people around here. – Dmitri Sep 11 '15 at 18:49
  • @ Zong Zheng Li I asked why one line works and why another line does not works, I never asked anyone to debug anything. I understand now that most of what I posted was not needed, so why all the hostility? Either edit the question or tell me to instead of downvoting. – 2trill2spill Sep 11 '15 at 18:51

4 Answers4

7

*count++ expands to *(count++), not (*count)++ like you were expecting. You're incrementing the address, not the file count.

Matt Olson
  • 409
  • 5
  • 13
4

I believe, as per the operator precedence,

 *count++;

should have been written as

(*count)++;

Otherwise, you're having the exact opposite (or, for that matter, invalid) action than what you're expecting.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

You are incrementing the pointer not the variable. You need to deference the pointer (count). A better and cleaner solution is to simply return the number of files rather than passing an argument. This removes any need for a pointer to hold the count and makes the method signature easier to use.

int count_files_directory(char *dir_path)
{
   int noOfFiles = 0;
   // Count files (omitted here)

   return noOfFiles;
}
MikeJ
  • 2,367
  • 3
  • 18
  • 23
0

Assume your count has a value of 10 and is stored at memory location 100. When you do something like

  *count++

you are now pointing at memory location 101. What you wanted to do was change the value of memory location 100 not go to memory location 101. Hence you would first de-reference it and then increment whatever was stored there.

  (*count)++;

When you do

  *count = *count + 1;

you are de-referencing and then incrementing the value followed by storing it back into memory location 100.

Rahul Kadukar
  • 858
  • 3
  • 15
  • 35