我有两个结构,一个结构创建一个命令。另一个拥有这些命令的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;
然后,我将能够构造一个新的Simple命令,并将其插入第1行。然后,我将具有:Command-> simpleCommands [0] = simpleCommand 0Command-> simpleCommands [1] = simpleCommand 1
到目前为止,我已经能够创建并验证简单的命令是否正常工作。这是我尝试将简单命令插入命令-> 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 "y.tab.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); }
谢谢!
我有两个结构,一个结构创建一个命令。另一个拥有这些命令的2D指针结构。 typedef struct SimpleCommand {//当前可用参数的可用空间...
#include <stdlib.h>
#include <string.h>
typedef struct SimpleCommand {
int id;
// Available space for arguments currently preallocated
int _numberOfAvailableArguments;
// Number of arguments
int _numberOfArguments;
// Array of arguments
char *_arguments[];
}SimpleCommand;
typedef struct Command {
int _numberOfAvailableSimpleCommands;
int _numberOfSimpleCommands;
SimpleCommand * _simpleCommands;
char * _outFile;
char * _inputFile;
char * _errFile;
int _background;
int outputAppend;
}Command;
void insertSimpleCommand(SimpleCommand * simpleCommand, Command * command ){
memcpy(command->_simpleCommands+command->_numberOfSimpleCommands, simpleCommand, sizeof(SimpleCommand));
command->_numberOfSimpleCommands++; //increase the number of simple commands in the commands
}
void main() {
SimpleCommand s1;
Command c;
c._simpleCommands = malloc(sizeof(SimpleCommand)*3);
s1.id = 1;
insertSimpleCommand(&s1, &c);
s1.id = 2;
insertSimpleCommand(&s1, &c);
exit(0);
}
(gdb) r
Starting program: /tmp/s
Breakpoint 1, main () at simplec2.c:38
38 c._simpleCommands = malloc(sizeof(SimpleCommand)*3);
(gdb) n
40 s1.id = 1;
(gdb)
42 insertSimpleCommand(&s1, &c);
(gdb)
44 s1.id = 2;
(gdb)
46 insertSimpleCommand(&s1, &c);
(gdb)
48 exit(0);
(gdb) p c._simpleCommands[0]
$1 = {id = 1, _numberOfAvailableArguments = 32767, _numberOfArguments = 0, _arguments = 0x555555756270}
(gdb) p c._simpleCommands[1]
$2 = {id = 2, _numberOfAvailableArguments = 32767, _numberOfArguments = 0, _arguments = 0x555555756280}
(gdb)