【发布时间】:2020-03-19 19:05:43
【问题描述】:
我有两个结构,一个用于创建命令。另一个包含这些命令的 2D 指针结构。
typedef struct SimpleCommand {
// Available space for arguments currently preallocated
int _numberOfAvailableArguments;
// Number of arguments
int _numberOfArguments;
// Array of arguments
char *_arguments[];
}SimpleCommand;
例如: 简单命令0:ls -la
现在我需要将此命令插入到该结构内的指针中,以便在第 0 行中键入简单命令。
typedef struct Command {
int _numberOfAvailableSimpleCommands;
int _numberOfSimpleCommands;
SimpleCommand ** _simpleCommands;
char * _outFile;
char * _inputFile;
char * _errFile;
int _background;
int outputAppend;
}Command;
然后,我将能够构造一个新的简单命令,并将其插入到第 1 行。然后我会: 命令->simpleCommands[0] = simpleCommand 0 命令->simpleCommands[1] = simpleCommand 1
到目前为止,我能够创建并验证简单的命令是否有效。这是我尝试将一个简单命令插入到 commands->simplecommands 指针的操作:
void insertSimpleCommand(SimpleCommand * simpleCommand, Command * command ){
command->_simpleCommands = realloc(command->_simpleCommands, sizeof(simpleCommand)*3);
SimpleCommand * tempCommand = simpleCommand;
command->_simpleCommands[command->_numberOfSimpleCommands] = tempCommand;
command->_numberOfSimpleCommands++; //increase the number of simple commands in the commands
}
当我这样做时,接下来发生的事情是旧的命令->simpleCommands[0] 被新的 simpleCommand 覆盖。我可以做些什么不同的事情,以便这些简单的命令被正确地一一添加并且之后都可以访问?
编辑: 这是我正在尝试做的最小可重复示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
// Describes a simple command and arguments
typedef struct SimpleCommand {
// Available space for arguments currently preallocated
int _numberOfAvailableArguments;
// Number of arguments
int _numberOfArguments;
// Array of arguments
char *_arguments[];
}SimpleCommand;
// Describes a complete command with the multiple pipes if any
// and input/output redirection if any.
typedef struct Command {
int _numberOfAvailableSimpleCommands;
int _numberOfSimpleCommands;
SimpleCommand ** _simpleCommands;
char * _outFile;
char * _inputFile;
char * _errFile;
int _background;
int outputAppend;
}Command;
//FUNCTION DEFINITIONS
void insertSimpleCommand( SimpleCommand * simpleCommand, Command * command );
void insertArgument(SimpleCommand *command, char * argument );
int main(int argc, char * argv[]){
//initialize the global currentsimplecommand struct
SimpleCommand *_currentSimpleCommand = malloc(sizeof(*_currentSimpleCommand) + sizeof(char[50]));
_currentSimpleCommand->_numberOfArguments = 0;
_currentSimpleCommand->_numberOfAvailableArguments = 1;
_currentSimpleCommand->_arguments[_currentSimpleCommand->_numberOfArguments] = (char*)malloc(sizeof(char*) * 50);
//SET UP COMMAND struct
Command *_currentCommand = (Command*)malloc(2 * sizeof(Command));
_currentCommand->_numberOfSimpleCommands = 0;
_currentCommand->_background = 0;
_currentCommand->outputAppend = 0;
insertArgument(_currentSimpleCommand,"ls");
insertArgument(_currentSimpleCommand,"-la");
insertSimpleCommand(_currentSimpleCommand, _currentCommand);
clear(_currentSimpleCommand);
insertArgument(_currentSimpleCommand,"head");
insertSimpleCommand(_currentSimpleCommand, _currentCommand);
for (int i = 0; i < _currentCommand->_numberOfSimpleCommands; i++){
printf("\n\nTEST: %d %s\n\n", i, _currentCommand->_simpleCommands[i]->_arguments[0]);
}
/*
THE OUTPUT OF THIS WILL PRODUCE THE SAME ARGUMENT[0] regardless fo the row we are on!
*/
}
void insertSimpleCommand(SimpleCommand * simpleCommand, Command * command ){
command->_simpleCommands = realloc(command->_simpleCommands, sizeof(simpleCommand)* command->_numberOfSimpleCommands);
SimpleCommand * tempCommand = simpleCommand;
command->_simpleCommands[command->_numberOfSimpleCommands] = tempCommand;
command->_numberOfSimpleCommands++; //increase the number of simple commands in the commands
}
//insert argument into simple command
void insertArgument(SimpleCommand *command, char * argument ) {
//allocate space for the new command
command->_arguments[command->_numberOfArguments] = (char*)realloc(command->_arguments[command->_numberOfArguments], sizeof(char*) * strlen(argument));
strcpy(command->_arguments[command->_numberOfArguments], argument);
command->_arguments[command->_numberOfArguments+1] = NULL;
command->_numberOfArguments++;
}
//clears the simple command for the next one
void clear(SimpleCommand *command){
free(command);
SimpleCommand *_currentSimpleCommand = malloc(sizeof(*_currentSimpleCommand) + sizeof(char[50]));
_currentSimpleCommand->_numberOfArguments = 0;
_currentSimpleCommand->_numberOfAvailableArguments = 1;
_currentSimpleCommand->_arguments[_currentSimpleCommand->_numberOfArguments] = (char*)malloc(sizeof(char*) * 50);
}
谢谢!
【问题讨论】:
-
乘以这个幻数
*3的目的是什么?为什么不使用command->_numberOfSimpleCommands -
我可以,但这不是这里的问题!
-
为了让我们回答而无需猜测,您至少想向我们展示所有涉及的变量是如何初始化的,以及函数是如何被调用的。您可能还想看看这里:stackoverflow.com/help/minimal-reproducible-example 就目前而言,这个问题无法认真回答。
-
SimpleCommand * tempCommand = simpleCommand;不创建simpleCommand的副本。 -
我已经尽我所能在上面添加了一个最小的可重现示例。 @user3386109
标签: c shell struct command malloc