在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/C/ char* 在struct中為什么會這樣

char* 在struct中為什么會這樣

問題描述

我希望把一個文本文件里面的內(nèi)容讀出來,但是不知道為什么讀取的那一行存放到自己自定義的struct類型的char *類型中總是把前面的讀取的char *給內(nèi)容給修改掉。出問題的地方我用注釋標(biāo)出來了

相關(guān)代碼


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 100
#define QUEUE_SIZE 100
#define TRUE 1
#define FALSE 0
typedef int bool;

struct Customer
{
    char *name;
    int arrivedTime;
    int priority;
    int age;
    int requireTime;
};

struct Queue
{
    struct Customer customers[QUEUE_SIZE];
    int size;
};

bool addCustomer(struct Queue* queue,struct Customer customer) {
    if (queue->size >= QUEUE_SIZE) {
        return FALSE;
    }
    else {
        queue->customers[queue->size] = customer;
        queue->size++;
        return TRUE;
    }
}

struct Customer getCustomer(struct Queue* queue,int index) {
    return queue->customers[index];
    for (int i = index; i < queue->size-1; i++) {
        queue->customers[i] = queue->customers[i + 1];
    }
    queue->size--;
}

int main() {
    struct Queue queue;
    queue.size = 0;
    FILE * file=fopen("C:\\Users\\zjb52\\Desktop\\cs\\sample-input.txt", "r");
    if (file == NULL) {
        printf("File not found");
    }
    else {
        char buffer[SIZE];
        while(fgets(buffer, SIZE, file) != NULL) {
            struct Customer customer;
                        
            //問題出現(xiàn)在這里,第二次循環(huán)的時候讀取的內(nèi)容是s1,但是queue里面本是s0也會變成s1
            customer.name = strtok(buffer, " ");
            customer.arrivedTime = atoi(strtok(NULL, " "));
            customer.priority = atoi(strtok(NULL, " "));
            customer.age = atoi(strtok(NULL, " "));
            customer.requireTime = atoi(strtok(NULL, " "));
            addCustomer(&queue, customer);    
        }        
    }
    fclose(file);
    system("pause");
    return 0;
}

sample-input.txt

s0 0 3 0 10
s1 0 6 0 20
s2 0 5 0 11
s3 0 6 0 20
s4 67 2 0 25
s5 5 4 0 1
s6 0 2 0 5
s7 0 4 0 28
s8 0 3 0 20
s9 45 5 0 6
s10 103 3 0 2

你期待的結(jié)果是什么?實(shí)際看到的錯誤信息又是什么?

while循環(huán)中customer.name = strtok(buffer, " ");我期望的結(jié)果是讀取之后queue中customer.name應(yīng)該是s0,s1,s2....s9,s10,但是結(jié)果卻都是s10。為什么struct里面的char *類型的name會這樣?我該怎么改呢?謝謝啦

回答
編輯回答
瘋子范

每一次while循環(huán)里定義的*char都是同一個地址,所以數(shù)據(jù)被覆蓋掉了

測試代碼,gcc編譯,Ubuntu運(yùn)行

#include<stdio.h>
#include<string.h>
typedef struct student{
  char *name;
  char num;
}student;
int main()
{ 
  char i = 0;
  while(i < 10){
  student xie;
    xie.name = &i;
    xie.num = i;
    printf("0x%p\n",xie.name);
    printf("%d\n",xie.num);
    i++;
  }

}

運(yùn)行結(jié)果:

0x0x7ffc7f2ca04f
0
0x0x7ffc7f2ca04f
1
0x0x7ffc7f2ca04f
2
0x0x7ffc7f2ca04f
3
0x0x7ffc7f2ca04f
4
0x0x7ffc7f2ca04f
5
0x0x7ffc7f2ca04f
6
0x0x7ffc7f2ca04f
7
0x0x7ffc7f2ca04f
8
0x0x7ffc7f2ca04f
9

2017年8月30日 10:33
編輯回答
忠妾

你給的代碼不能解決問題,謝謝你給出的原因,讓我知道問題出在哪里了,
我把Customer的name由char*換成了char[]
while循環(huán)的代碼做了如下修改:

char buffer[SIZE];
        
while(fgets(buffer, SIZE, file) != NULL) {
            
    struct Customer customer;
    strcpy(customer.name,strtok(buffer, " "));
    customer.arrivedTime = atoi(strtok(NULL, " "));
    customer.priority = atoi(strtok(NULL, " "));
    customer.age = atoi(strtok(NULL, " "));
    customer.requireTime = atoi(strtok(NULL, " "));
    addCustomer(&queue, customer);    
    }        
2018年8月17日 06:34
編輯回答
刮刮樂

因?yàn)?strtok 會修改原字符串, strok 返回的值總是 buffer, 所以你的所有 name 字段都指向 buffer. 而 strok 每次都在修改這個 buffer.

char temp[SIZE];
customer.name = strtok(strncpy(temp, buffer, SIZE), " ");
2018年1月19日 06:30