如何在结构中将 char[] 分配给 null
How to assign char[] to null inside the struct
我正在尝试将 null 分配给结构内的 char name[];但是,它不起作用
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define name_size 30
#define bloc_entry_number 20
#define file_list_number 10
#define direction_list_number 10
struct File {
char name[name_size];
int block_entry[bloc_entry_number];
};
struct Direction{
char name[name_size];
int current_index;
int previous_index;
struct File* file_list[file_list_number];
int file_list_tracking[file_list_number];
struct Direction* next_entry_direction[direction_list_number];
int next_entry_direction_tracking[direction_list_number];
};
struct Block{
int index;
int size_remain;
};
int main(){
struct Direction *dir = (struct Direction*) malloc(sizeof(struct Direction));
for (int i = 0; i < 10; i ++ ){
dir->next_entry_direction[i]->name[0] = '[=10=]'; // error occur here
printf("%s",dir->next_entry_direction[i]->name);// error occur here;
}
return 0;
}
我无法将 NULL 分配给 char name[];
我该如何解决?我已经尝试过 name[0] = '\0', name = NULL, strcpy(name,NULL)
不能设置为NULL。它是一个数组而不是指针。你可以做的是:
dir->next_entry_direction[i]->name[0] = '[=10=]';
我正在尝试将 null 分配给结构内的 char name[];但是,它不起作用
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define name_size 30
#define bloc_entry_number 20
#define file_list_number 10
#define direction_list_number 10
struct File {
char name[name_size];
int block_entry[bloc_entry_number];
};
struct Direction{
char name[name_size];
int current_index;
int previous_index;
struct File* file_list[file_list_number];
int file_list_tracking[file_list_number];
struct Direction* next_entry_direction[direction_list_number];
int next_entry_direction_tracking[direction_list_number];
};
struct Block{
int index;
int size_remain;
};
int main(){
struct Direction *dir = (struct Direction*) malloc(sizeof(struct Direction));
for (int i = 0; i < 10; i ++ ){
dir->next_entry_direction[i]->name[0] = '[=10=]'; // error occur here
printf("%s",dir->next_entry_direction[i]->name);// error occur here;
}
return 0;
}
我无法将 NULL 分配给 char name[]; 我该如何解决?我已经尝试过 name[0] = '\0', name = NULL, strcpy(name,NULL)
不能设置为NULL。它是一个数组而不是指针。你可以做的是:
dir->next_entry_direction[i]->name[0] = '[=10=]';