在C中传递指向struct的指针?

时间:2019-03-18 00:36:49

标签: c pointers struct

我正在研究一个C程序来遍历Linux中的文件系统,记录每个文件的内存,并在最后吐出一个直方图。我在传递指向结构的指针时遇到问题,并且对如何在C中传递这些结构不太熟悉。

我试图将头指针传递给我的readDirectory函数,但是以其行为方式,每次调用该函数时,它都会在一个空的链表头中传递。在该函数中,它按预期添加了节点,但是每次它递归调用自身时,似乎列表都消失了,头部又回到了NULL。我认为我给他们传递了错误,所以有人可以告诉我传递它们的正确方法,还是为我提供了很好的解释性很好的资源?

当我将其传递给printHistrogram函数时,也会发生此问题,但是如果我可以在其他地方修复它,我也会在这里知道如何修复它。

先谢谢了。 -克里斯

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

struct memList
{
    int mem;
    struct memList* next;
};

void readDirectory(const char*, struct memList*);
void printHistogram(struct memList*, int binSize);



int main(int argc, char* argv[])
{
    struct memList* head = NULL;

    if (argc != 3)
    {
        perror("Not enough parameters\n");
    }

    int binSize = strtol(argv[2], NULL, 10);

    readDirectory(argv[1], head);

    printHistogram(head, binSize);

    return 0;
}

void readDirectory(const char * passedDir, struct memList* head)
{
    DIR * directory = opendir(passedDir);

    if (directory == NULL)
            printf("Unable to open directory\n");

    while(1)
    {
        struct dirent * current;

        current = readdir(directory);
        if (!current)
            break;

        if ((current->d_type == 4) && (strcmp(current->d_name, ".") != 0) && (strcmp(current->d_name, "..") != 0))  //current path is directory but not the current or parent
        {
            char * path = malloc(sizeof(char) * 300);

            strcpy(path, passedDir);

            if (path[strlen(path) - 1] != '/')
                strcat(path, "/");
            strcat(path, current->d_name);

            readDirectory(path, head);

            free(path);
        }

        else
        {   
            char * path = malloc(sizeof(char) * 300);

            strcpy(path, passedDir);

            if (path[strlen(path) - 1] != '/')
                strcat(path, "/");
            strcat(path, current->d_name);

            struct stat tempStat;
            stat(path, &tempStat);

            free(path);

            int temp = tempStat.st_size;

            if (head == NULL)
            {   
                head = (struct memList*)malloc(sizeof(struct memList));
                head->next = NULL;
                head->mem = temp;
            }
            else
            {
                struct memList * tempStruct = (struct memList*)malloc(sizeof(struct memList));
                tempStruct->next = head;
                tempStruct->mem = temp;
                head = tempStruct;
                //printf("mem is %d\n", head->mem);   //debugging
            }
        }

    }
    closedir(directory);
}

void printHistogram(struct memList* head, int binSize)
{
    int numElements = 10;

    int * array = (int*)malloc(sizeof(int) * numElements);
    for (int i = 0; i < numElements; i++)
        array[i] = 0;

    while(head != NULL)
    {
        //printf("mem is %d\n", head->mem);   //debugging
        int temp = head->mem;
        int temp2 = ((temp - (temp % binSize)) / binSize);

        if ((temp2 + 1) > numElements)
        {
            int * new = realloc(array, (sizeof(int) * (temp2 + 1)));
            array = new;
            for (int i = numElements; i < (temp2 + 1); i++)
                array[i] = 0;
            numElements = (temp2 + 1);
        }

        array[temp2] += 1;      

        head = head->next;
    }

    for (int i = 0; i < numElements; i++)
    {
        printf("%d\n", array[i]);
        printf("Block %d:  ", i + 1);
        for (int j = 0; j < array[i]; j++)
            printf("*");
        printf("\n");
    }
}

1 个答案:

答案 0 :(得分:0)

研究“按引用传递”和“按值传递”。 C是按值传递。

如果您希望修改head,使其值保持在readDirectory之外,则需要将指针传递给指针。