gpt4 book ai didi

c - 结构和字符**

转载 作者:太空宇宙 更新时间:2023-11-04 01:51:40 24 4
gpt4 key购买 nike

我最近完全不熟悉 C 编程,我开始更多地研究指针,但我的代码遇到了问题。

所以我尝试将字符串传递给结构中的 char** 但我总是遇到错误。在此先感谢这里是一些代码

这是我使用的结构。

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

这就是我试图放入其中的内容。在第一个 if you can see 我尝试 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);

最佳答案

很容易将结构的内存分配和释放搞乱。所以最好总是为每个结构编写新的和销毁的函数。

/* 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 );
}

现在的大部分工作是确定要使用的队列。

关于c - 结构和字符**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41989528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com