Я следующий код:Является ли INT на вкладке структуры malloc?
int parser_start(t_control *shell)
{
int i;
int c;
int count;
int separator;
count = 0;
shell->command_index = 1;
shell->commands = malloc(parser_count_separators(shell));
shell->commands[count] = malloc(sizeof(shell->commands));
shell->commands[count]->command = malloc(my_strlen(shell->cli) * sizeof(char));
shell->commands[count]->op = 0;
i = 0;
c = 0;
while (shell->cli[i] != '\0')
{
separator = parser_is_separator(shell->cli[i], shell->cli[i + 1]);
if (separator == -1)
shell->commands[count]->command[c++] = shell->cli[i];
else
{
shell->command_index++;
count++;
shell->commands[count] = malloc(sizeof(shell->commands));
shell->commands[count]->command = malloc(my_strlen(shell->cli) * sizeof(char));
shell->commands[count]->op = separator;
my_printf("%d\n", shell->commands[count]->op);
c = 0;
}
i = i + parser_separator_get_size(separator);
}
}
С следующей структуры:
typedef struct s_command
{
char *command;
char *program;
char **argv;
int argc;
pid_t pid;
int op;
} t_command;
typedef struct s_control
{
pid_t pid;
char **env;
char **path;
char *cli;
int fd_history;
int power;
int pipefd[2];
int command_index;
char **builtins;
int (*builtins_fptr[6])(struct s_control *, int);
t_command **commands;
} t_control;
Когда я дамп shell->commands[count]->op
с моим printf
, распечатать 1
как я хочу.
Когда я сбрасываю ту же установку в другую функцию, я получил 7564320
. Почему? Должно ли мое поле op
быть malloc?
Благодаря
Вы не указали очень важные определения 'struct', также вы предполагаете, что' malloc() 'всегда возвращает допустимый указатель, что является неправильным и, следовательно, опасным, вам нужно опубликовать определения' struct'. Кроме того, _ Когда я удаляю одну и ту же установку в другой функции, я получил '7564320'_, что вы подразумеваете, что неясно, нужен и код, а ответ - нет, вам не нужно' malloc() 'an 'int', почему он не работает, необходим код, в котором вы получаете' 7564320'. –
Я добавил определение структуры. – Teldar