簡體   English   中英

在C中傳遞指向struct的指針?

[英]Passing pointer to struct in C?

我正在做一個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");
    }
}

研究“按引用傳遞”和“按值傳遞”。 C 是按值傳遞。

如果您希望修改head以使其將其值保留在readDirectory之外,那么您需要將指針傳遞給一個指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM