簡體   English   中英

結構和char **

[英]Struct and char**

我最近在C編程中是全新的,我開始更多地研究指針,我遇到了代碼問題。

所以我嘗試將一個字符串傳遞給一個結構中的char** ,但我總是遇到錯誤。 在此先感謝這里是一些代碼

這是我使用的結構。

typedef struct Queue {
int capacity;
int front;
int rear;
int size;
char **car_id;
};

這就是我試圖加入的內容。 如果你能看到我嘗試strcpy但失敗了。 即使我把&或*。

    printf("Printing the contect of the TXT file\n");
int countS=-1,countQ=-1;
char line[10];
FILE *fp;
fp = fopen("car.txt","r");
if (fp == NULL) {
    printf("Error: File doesn't exist exiting the programm!");
    exit(1);
}
while(!feof(fp)){
      fgets(line,10,fp);
      printf("%s",line);

       if(line[(strlen(line))-2]=='Q'){
        countQ++;
        line[strlen(line)-2] = 0;
        strcpy(car_id,line);
        strcpy(QBoat->car_id[countQ],car_id);
        QBoat->rear=countQ;
        QBoat->front=0;
        QBoat->size=countQ;
       }else if(line[(strlen(line))-2]=='S'){
        countS++;
        line[strlen(line)-2] = 0;
        SBoat->top = countS;
        //strcpy(SBoat->car_id[countS],line);
       }

}
fclose(fp);

使用結構很容易搞亂內存分配和釋放。 因此,最好始終為每個結構編寫new和destroy函數。

/* To avoid confusion, it's best to declare structs as type definitions
   and name them Something_t. Capital letter to avoid conflict with
   built in types, _t to note that it is a type, and to avoid conflict
   with other names. */
typedef struct {
    /* Using size_t means less type casting */
    size_t capacity;
    int front;
    int rear;
    int size;
    char **items;
} Queue_t;

Queue_t *Queue_new( const size_t capacity ) {
    /* Use calloc because we have to zero everything anyway */
    Queue_t *queue = calloc( 1, sizeof(Queue_t) );

    /* Allocate enough space to store a list of whatever
       queue->items is a list of */
    queue->capacity = capacity;
    queue->items = malloc( capacity * sizeof(*(queue->items)) );

    return queue;
}

void Queue_destroy( Queue_t *queue ) {
    /* Since items were copied into the list, it's the queue's responsibility
       to free them */
    for( int i = queue->front; i < queue->rear; i++ ) {
        free( queue->items[i] );
    }

    /* Now free the list of items */
    free( queue->items );

    /* And finally the struct itself */
    free( queue );
}

現在已經分配了結構及其項目列表,您必須確保不添加太多項目。 添加到隊列的代碼永遠不會檢查它是否超出了隊列的容量。 因此,最好編寫一個函數來添加能夠正確檢查容量的項目。

void Queue_add( Queue_t *queue, const char *item ) {
    if( queue->size >= (int)queue->capacity ) {
        fprintf(stderr, "Queue capacity of %zu exceeded!\n", queue->capacity);
        exit(1);
    }

    /* You used strcpy in your example, so I'm following that.
       You could also not copy and store the original pointer. */
    queue->items[ queue->rear ] = strdup( item );

    /* I don't know if this is right */
    queue->rear++;
    queue->size++;
}

排隊邏輯可能是錯誤的,我對隊列不好,但你明白了。 您甚至可以稍后擴展隊列以自動調整自身大小。

現在,您可以單獨測試這些工作。 一旦確定它們有效,您可以嘗試從文件中讀取並使用隊列功能。

int main() {
    char filename[] = "car.txt";
    FILE *fp = fopen(filename,"r");
    if (fp == NULL) {
        fprintf(stderr, "Couldn't read '%s': %s\n", filename, strerror(errno));
        exit(1);
    }

    /* Just picked a number out of thin air */
    Queue_t *qboats = Queue_new(256);
    Queue_t *sboats = Queue_new(256);

    char line[10];
    while( fgets( line, 10, fp) != NULL ) {
        size_t len = strlen(line);

        /* Choose the right queue to use */
        Queue_t *queue;
        switch ( line[len-2] ) {
            case 'S':
                queue = sboats;
                break;
            case 'Q':
                queue = qboats;
                break;
            default:
                fprintf( stderr, "Can't understand line '%s'\n", line );
                continue;
                break;
        }

        /* Chop off the identifier */
        line[len - 2] = '\0';

        /* Add to the queue */
        Queue_add( queue, line );
    }

    /* Do something with the queues. This should probably be Queue_print(). */
    for( int i = qboats->front; i < qboats->rear; i++ ) {
        printf("qboat: %s\n", qboats->items[i]);
    }

    for( int i = sboats->front; i < sboats->rear; i++ ) {
        printf("sboat: %s\n", sboats->items[i]);
    }

    /* Now clean them up */
    Queue_destroy( sboats );
    Queue_destroy( qboats );
}

現在大部分工作都在確定要使用的隊列。

暫無
暫無

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

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