2017-05-22 5 views
-1

リンクリストを作成する必要があります。これらのリストでは、関数へのポインタを定義します。私のエラーは、私が関数を呼び出すたびに、私はセグメンテーションフォールトエラーがあります。誰もが、以下の、その中で助けてくださいすることができます私のコードです:C:structre内の関数へのポインタがセグメンテーションフォールトを起こす

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 


typedef struct CommandStructure{ 
    char CommandName[10]; 
    char ShortKey; 
    void (* CommandAction)(void); 
} CommandFrame; 

typedef struct LinkedCommandsStructure{ 
    CommandFrame * Entity; 
    struct LinkedCommandsStructure * NextCommand; 
} Firmware_Command; 


void PrintSEQHelp(){ 
    printf("HelloPrint \n"); 
} 


CommandFrame * SEQ_Help(){ 
    CommandFrame * Entity = malloc(sizeof(Entity)); 

    strcpy(Entity->CommandName, "help"); 

    Entity->ShortKey = 'h'; 

    Entity->CommandAction = PrintSEQHelp; 

    return Entity; 
} 



Firmware_Command * SEQ_CommandsInit(){ 

    Firmware_Command * HeadOfCommands = malloc(sizeof(HeadOfCommands)); 
    Firmware_Command * HelpCommand  = malloc(sizeof(HelpCommand)); 

    HelpCommand->Entity = SEQ_Help(); 

    HelpCommand->NextCommand = NULL; 

    HeadOfCommands = HelpCommand; 

    return HeadOfCommands; 
} 


void callcommand(Firmware_Command * ActiveCommands){ 
    ActiveCommands = malloc(sizeof * ActiveCommands); 
    printf("inside callcommand \n"); 

    (ActiveCommands->Entity->CommandAction)();  

} 


int ModulesInit() { 

    int ParseRet; 

    Firmware_Command * ActiveCommands = malloc(sizeof(ActiveCommands)); 

    ActiveCommands = SEQ_CommandsInit(); 
    callcommand(ActiveCommands); 

    return 1; 
} 



void main(void){ 
    int cmdInitRet; 

    cmdInitRet = ModulesInit(); 
} 
+1

デバッグを行う必要があります。 –

+0

私は – moibrahim

+2

CommandFrame * Entity = malloc(sizeof(Entity)); ' - >' CommandFrame * Entity = malloc(sizeof(* Entity)); 'など多くの同様の間違いがありました。 – BLUEPIXY

答えて

0

あなたが変数を持っていて、それのためにメモリを割り当てるコードでこれを数回行うが、その後、他の値で上書き。または、以前の値以上に割り当てる。決定的には、callcommand関数では、変数を渡し、その値を初期化されていないメモリの新しいブロックで上書きし、それを使ってみます。メモリを割り当てる必要はありません。渡された変数はすでに有効なポインタである必要があります。

void callcommand(Firmware_Command * ActiveCommands){ 
    // ActiveCommands = malloc(sizeof * ActiveCommands); Not needed at all 
    printf("inside callcommand \n"); 

    (ActiveCommands->Entity->CommandAction)();  

} 
+0

ご協力いただきありがとうございます – moibrahim

関連する問題