2017-08-28 1 views
-3

Ich arbeitete an einem Hobby-Projekt und es erfordert, dass ich Hexadezimalwerte zu einem Array basierend auf bestimmten Hex-Funktionsaufrufen zuweisen. Das Array wird jedoch nicht zurückgegeben und friert einfach das Programm bei der Ausführung ein.Wie können Arrays hexadezimalen Werten zugewiesen werden, die an bestimmten Speicherorten den Schalter oder einen if-else-Schritt verwenden?

Ich muss dann das zurückgegebene unsigned Char-Array nehmen und es dann in ein zweites Array ohne NULL-Werte verschieben. Dies werde ich in meinem Hauptprogramm verwenden.

Danke, Aurelius

//Function to get hex condition 
    unsigned char set_lpx_var(char S0,char S1,char S2,char S3) 
    { 
    unsigned char PIN; 
    if((S0==0)&&(S1==0)&&(S2==0)&&(S3==0)) PIN = 0x00;    
    else if((S0==1)&&(S1==0)&&(S2==0)&&(S3==0)) PIN = 0x01;   
    else if((S0==0)&&(S1==1)&&(S2==0)&&(S3==0)) PIN = 0x02;   
    else if((S0==1)&&(S1==1)&&(S2==0)&&(S3==0)) PIN = 0x03; 
    else if((S0==0)&&(S1==0)&&(S2==1)&&(S3==0)) PIN = 0x04; 
    else if((S0==1)&&(S1==0)&&(S2==1)&&(S3==0)) PIN = 0x05; 
    else if((S0==0)&&(S1==1)&&(S2==1)&&(S3==0)) PIN = 0x06; 
    else if((S0==1)&&(S1==1)&&(S2==1)&&(S3==0)) PIN = 0x07; 
    else if((S0==0)&&(S1==0)&&(S2==0)&&(S3==1)) PIN = 0x08; 
    else if((S0==1)&&(S1==0)&&(S2==0)&&(S3==1)) PIN = 0x09; 
    else if((S0==0)&&(S1==1)&&(S2==0)&&(S3==1)) PIN = 0x0A; 
    else if((S0==1)&&(S1==1)&&(S2==0)&&(S3==1)) PIN = 0x0B; 
    else if((S0==0)&&(S1==0)&&(S2==1)&&(S3==1)) PIN = 0x0C; 
    else if((S0==1)&&(S1==0)&&(S2==1)&&(S3==1)) PIN = 0x0D; 
    else if((S0==0)&&(S1==1)&&(S2==1)&&(S3==1)) PIN = 0x0E; 
    else if((S0==1)&&(S1==1)&&(S2==1)&&(S3==1)) PIN = 0x0F; 
    else PIN = 0x00; 
    printf("\n Obtained hexa digit %u",(unsigned)PIN); 
    return PIN;          
    } 

Funktion Hex-Werte an Array

unsigned char* command(unsigned char PIN,int sw,int size) 
    { 
    unsigned char* x; 
    x = (unsigned char*)malloc(size); 
    //select line assignment to array 

    if(sw == 1) 
    { 
    if(PIN==0x00) 
    { 
     x[0]='0'; 
     x[1]='0'; 
     x[2]='0'; 
    } 
    else if(PIN==0X01) 
    { 
     x[0]='1'; 
     x[1]='0'; 
     x[2]='0'; 
    } 
    else if(PIN==0x02) 
    { 
     x[0]='0'; 
     x[1]='1'; 
     x[2]='0'; 
    } 
    else if(PIN==0x03) 
    { 
     x[0]='1'; 
     x[1]='1'; 
     x[2]='0'; 
    } 
    else if(PIN==0x04) 
    { 
     x[0]='0'; 
     x[1]='0'; 
     x[2]='1'; 
    } 
    else if(PIN==0x05) 
    { 
     x[0]='1'; 
     x[1]='0'; 
     x[2]='1'; 
    } 
    else if(PIN==0x06) 
    { 
     x[0]='0'; 
     x[1]='1'; 
     x[2]='1'; 
    } 
    else if(PIN==0x07) 
    { 
     x[0]='1'; 
     x[1]='1'; 
     x[2]='1'; 
    } 
    else 
    { 
     printf("\n Invalid hexa digit %u",(unsigned)PIN); 
    } 
    //selection command 
    return x; 
    } 

    else if(sw == 2) 
    { 
    if(PIN==0x00) 
    { 
     x[3]='0'; 
     x[4]='0'; 
     x[5]='0'; 
    } 
    else if(PIN==0X01) 
    { 
     x[3]='1'; 
     x[4]='0'; 
     x[5]='0'; 
    } 
    else if(PIN==0x02) 
    { 
     x[3]='0'; 
     x[4]='1'; 
     x[5]='0'; 
    } 
    else if(PIN==0x03) 
    { 
     x[3]='1'; 
     x[4]='1'; 
     x[5]='0'; 
    } 
    else if(PIN==0x04) 
    { 
     x[3]='0'; 
     x[4]='0'; 
     x[5]='1'; 
    } 
    else if(PIN==0x05) 
    { 
     x[3]='1'; 
     x[4]='0'; 
     x[5]='1'; 
    } 
    else if(PIN==0x06) 
    { 
     x[3]='0'; 
     x[4]='1'; 
     x[5]='1'; 
    } 
    else if(PIN==0x07) 
    { 
     x[3]='1'; 
     x[4]='1'; 
     x[5]='1'; 
    } 
    else 
    { 
     printf("\n Invalid hexa digit %u",(unsigned)PIN); 
    } 
    //selection command 
    return x; 
    } 

    else if(sw == 3) 
    { 
    if(PIN==0x00) 
    { 
     x[6]='0'; 
     x[7]='0'; 
     x[8]='0'; 
    } 
    else if(PIN==0X01) 
    { 
     x[6]='1'; 
     x[7]='0'; 
     x[8]='0'; 
    } 
    else if(PIN==0x02) 
    { 
     x[6]='0'; 
     x[7]='1'; 
     x[8]='0'; 
    } 
    else if(PIN==0x03) 
    { 
     x[6]='1'; 
     x[7]='1'; 
     x[8]='0'; 
    } 
    else if(PIN==0x04) 
    { 
     x[6]='0'; 
     x[7]='0'; 
     x[8]='1'; 
    } 
    else if(PIN==0x05) 
    { 
     x[6]='1'; 
     x[7]='0'; 
     x[8]='1'; 
    } 
    else if(PIN==0x06) 
    { 
     x[6]='0'; 
     x[7]='1'; 
     x[8]='1'; 
    } 
    else if(PIN==0x07) 
    { 
     x[6]='1'; 
     x[7]='1'; 
     x[8]='1'; 
    } 
    else 
    { 
     printf("\n Invalid hexa digit %u",(unsigned)PIN); 
    } 
    //selection command 
    return x; 
    } 

    else if(sw == 4) 
    { 
    if(PIN==0x00) 
    { 
     x[9]='0'; 
     x[10]='0'; 
     x[11]='0'; 
    } 
    else if(PIN==0X01) 
    { 
     x[9]='1'; 
     x[10]='0'; 
     x[11]='0'; 
    } 
    else if(PIN==0x02) 
    { 
     x[9]='0'; 
     x[10]='1'; 
     x[11]='0'; 
    } 
    else if(PIN==0x03) 
    { 
     x[9]='1'; 
     x[10]='1'; 
     x[11]='0'; 
    } 
    else if(PIN==0x04) 
    { 
     x[9]='0'; 
     x[10]='0'; 
     x[11]='1'; 
    } 
    else if(PIN==0x05) 
    { 
     x[9]='1'; 
     x[10]='0'; 
     x[11]='1'; 
    } 
    else if(PIN==0x06) 
    { 
     x[9]='0'; 
     x[10]='1'; 
     x[11]='1'; 
    } 
    else if(PIN==0x07) 
    { 
     x[9]='1'; 
     x[10]='1'; 
     x[11]='1'; 
    } 
    else 
    { 
     printf("\n Invalid hexa digit %u",(unsigned)PIN); 
    } 
    //selection command 
    return x; 
    } 

    else if(sw == 5) 
    { 
    if(PIN==0x00) 
    { 
     x[12]='0'; 
     x[13]='0'; 
     x[14]='0'; 
    } 
    else if(PIN==0X01) 
    { 
     x[12]='1'; 
     x[13]='0'; 
     x[14]='0'; 
    } 
    else if(PIN==0x02) 
    { 
     x[12]='0'; 
     x[13]='1'; 
     x[14]='0'; 
    } 
    else if(PIN==0x03) 
    { 
     x[12]='1'; 
     x[13]='1'; 
     x[14]='0'; 
    } 
    else if(PIN==0x04) 
    { 
     x[12]='0'; 
     x[13]='0'; 
     x[14]='1'; 
    } 
    else if(PIN==0x05) 
    { 
     x[12]='1'; 
     x[13]='0'; 
     x[14]='1'; 
    } 
    else if(PIN==0x06) 
    { 
     x[12]='0'; 
     x[13]='1'; 
     x[14]='1'; 
    } 
    else if(PIN==0x07) 
    { 
     x[12]='1'; 
     x[13]='1'; 
     x[14]='1'; 
    } 
    else 
    { 
     printf("\n Invalid hexa digit %u",(unsigned)PIN); 
    } 
    //selection command 
    return x; 
    } 

    else if(sw == 6) 
    { 
    if(PIN==0x00) 
    { 
     x[15]='0'; 
     x[16]='0'; 
     x[17]='0'; 
     x[18]='0'; 
    } 
    else if(PIN==0x01) 
    { 
     x[15]='1'; 
     x[16]='0'; 
     x[17]='0'; 
     x[18]='0'; 
    } 
    else if(PIN==0x02) 
    { 
     x[15]='0'; 
     x[16]='1'; 
     x[17]='0'; 
     x[18]='0'; 
    } 
    else if(PIN==0x03) 
    { 
     x[15]='1'; 
     x[16]='1'; 
     x[17]='0'; 
     x[18]='0'; 
    } 
    else if(PIN==0x04) 
    { 
     x[15]='0'; 
     x[16]='0'; 
     x[17]='1'; 
     x[18]='0'; 
    } 
    else if(PIN==0x05) 
    { 
     x[15]='1'; 
     x[16]='0'; 
     x[17]='1'; 
     x[18]='0'; 
    } 
    else if(PIN==0x06) 
    { 
     x[15]='0'; 
     x[16]='1'; 
     x[17]='1'; 
     x[18]='0'; 
    } 
    else if(PIN==0x07) 
    { 
     x[15]='1'; 
     x[16]='1'; 
     x[17]='1'; 
     x[18]='0'; 
    } 
    else if(PIN==0x08) 
    { 
     x[15]='0'; 
     x[16]='0'; 
     x[17]='0'; 
     x[18]='1'; 
    } 
    else if(PIN==0x09) 
    { 
     x[15]='1'; 
     x[16]='0'; 
     x[17]='0'; 
     x[18]='1'; 
    } 
    else if(PIN==0x0A) 
    { 
     x[15]='0'; 
     x[16]='1'; 
     x[17]='0'; 
     x[18]='1'; 
    } 
    else if(PIN==0x0B) 
    { 
     x[15]='1'; 
     x[16]='1'; 
     x[17]='0'; 
     x[18]='1'; 
    } 
    else if(PIN==0x0C) 
    { 
     x[15]='0'; 
     x[16]='0'; 
     x[17]='1'; 
     x[18]='1'; 
    } 
    else if(PIN==0x0D) 
    { 
     x[15]='1'; 
     x[16]='0'; 
     x[17]='1'; 
     x[18]='1'; 
    } 
    else if(PIN==0x0E) 
    { 
     x[15]='0'; 
     x[16]='1'; 
     x[17]='1'; 
     x[18]='1'; 
    } 
    else if(PIN==0x0F) 
    { 
     x[15]='1'; 
     x[16]='1'; 
     x[17]='1'; 
     x[18]='1'; 
    } 
    else 
    { 
     printf("\n Invalid hexa digit %u",(unsigned)PIN); 
    } 
    return x; 
    } 

    else if(sw == 7) 
    { 
    if(PIN==0x00) 
    { 
     x[19]='0'; 
     x[20]='0'; 
     x[21]='0'; 
    } 
    else if(PIN==0X01) 
    { 
     x[19]='1'; 
     x[20]='0'; 
     x[21]='0'; 
    } 
    else if(PIN==0x02) 
    { 
     x[19]='0'; 
     x[20]='1'; 
     x[21]='0'; 
    } 
    else if(PIN==0x03) 
    { 
     x[19]='1'; 
     x[20]='1'; 
     x[21]='0'; 
    } 
    else if(PIN==0x04) 
    { 
     x[19]='0'; 
     x[20]='0'; 
     x[21]='1'; 
    } 
    else if(PIN==0x05) 
    { 
     x[19]='1'; 
     x[20]='0'; 
     x[21]='1'; 
    } 
    else if(PIN==0x06) 
    { 
     x[19]='0'; 
     x[20]='1'; 
     x[21]='1'; 
    } 
    else if(PIN==0x07) 
    { 
     x[19]='1'; 
     x[20]='1'; 
     x[21]='1'; 
    } 
    else 
    { 
     printf("\n Invalid hexa digit %u",(unsigned)PIN); 
    } 
    //selection command 
    return x; 
    } 

    } 

Hauptfunktion

int main() 
    { 
    int sw = 1,i,size=22; 
    char S3=0,S2=1,S1=0,S0=1; 
    unsigned char PIN; 
    PIN=set_lpx_var(S0,S1,S2,S3); 
    unsigned char *x; 
    unsigned char *d; 
    x=command(PIN,sw,size);//sw = 1 
    for (i = 0; i < size; i++) 
    { 
    if (x[i]!='\0') 
    { 
    d[i]=x[i]; 
    } 
    } 
    sw = 2; 
    S3=0,S2=1,S1=1,S0=1; 
    PIN=set_lpx_var(S0,S1,S2,S3); 
    x=command(PIN,sw,size); 
    for (i = 0; i < size; i++) 
    { 
    if (x[i]!='\0') 
    { 
    d[i]=x[i]; 
    } 
    } 
    sw = 3; 
    S3=0,S2=1,S1=0,S0=0; 
    PIN=set_lpx_var(S0,S1,S2,S3); 
    x=command(PIN,sw,size); 
    for (i = 0; i < size; i++) 
    { 
    if (x[i]!='\0') 
    { 
    d[i]=x[i]; 
    } 
    } 
    sw = 4; 
    S3=0,S2=0,S1=1,S0=1; 
    PIN=set_lpx_var(S0,S1,S2,S3); 
    x=command(PIN,sw,size); 
    for (i = 0; i < size; i++) 
    { 
    if (x[i]!='\0') 
    { 
    d[i]=x[i]; 
    } 
    } 
    sw = 5; 
    S3=0,S2=1,S1=1,S0=0; 
    PIN=set_lpx_var(S0,S1,S2,S3); 
    x=command(PIN,sw,size); 
    for (i = 0; i < size; i++) 
    { 
    if (x[i]!='\0') 
    { 
    d[i]=x[i]; 
    } 
    } 
    sw = 6; 
    S3=1,S2=1,S1=1,S0=0; 
    PIN=set_lpx_var(S0,S1,S2,S3); 
    x=command(PIN,sw,size); 
    for (i = 0; i < size; i++) 
    { 
    if (x[i]!='\0') 
    { 
    d[i]=x[i]; 
    } 
    } 
    sw = 7; 
    S3=0,S2=1,S1=1,S0=0; 
    PIN=set_lpx_var(S0,S1,S2,S3); 
    x=command(PIN,sw,size); 
    for (i = 0; i < size; i++) 
    { 
    if (x[i]!='\0') 
    { 
    d[i]=x[i]; 
    } 
    } 
    for (i = 0; i < size; i++) 
    { 
    { 
    printf("Value at %i is %c",i,d[i]); 
    } 
    } 
    }  
+0

Bitte formatieren Sie Ihren Code mit richtiger Einrückung, wenn Sie eine Frage stellen. Ihr Code ist sehr schwer zu lesen. Außerdem ist es wahrscheinlich besser, die Haupt- und die globalen Funktionen in einen Codeblock zu stellen, damit jemand, der Ihnen helfen kann, leichter in ein Projekt kopieren und einfügen und es kompilieren kann. Verwenden Sie einen Debugger? Wenn ja, wie weit hast du es geschafft, bevor das Programm begann sich schlecht zu benehmen? – shawn1874

+0

Es tut mir leid für das Chaos wirklich, da ich den Code auf die 8-Raum-Regel zugeschnitten hatte. Das Code-Block-Symbol wurde komplett verpasst. Der Fehler beginnt, nachdem Sie den PIN-Hexadezimalwert erhalten haben. Sobald die Befehlsfunktion startet, erstarrt der Bildschirm. Ich denke, es ist irgendwo ein lockerer Zeiger, aber ich kann mir nicht sicher sein. Würde wirklich ein frisches Paar Augen auf dieses Ding schätzen. – aurelius

+0

Erstellen Sie eine Tabelle mit 'PIN' Werten im Vergleich zu' S0', 'S1',' S2' und 'S3' Werten. Sie werden ein Muster sehen oder eine Karnough-Karte erstellen, um es zu reduzieren. –

Antwort

0

Eine schnelle Art und Weise Sie erste Funktion zu optimieren und gleichzeitig die Möglichkeit, Fehler zu reduzieren:

//Function to get hex condition 
unsigned char set_lpx_var(char s0, char s1, char s2, char s3) 
{ 
    unsigned char result = (unsigned char)(((s3 != 0) << 3) + ((s2 != 0) << 2) 
             + ((s1 != 0) << 1) + (s0 != 0)); 
    printf("\n Obtained hexa digit %u", result); 
    return result; 
} 

Funktion # 2:

unsigned char* command(unsigned char PIN,int sw,int size) 
{ 
    unsigned char* x; 
    size_t offset = sw - 1; 

    if (PIN & 0x08) // hard to know from your code what to do in case 
        // of error here. 
    { 
    printf("\n Invalid hexa digit %u", PIN); 
    return NULL; 
    } 

    if (size < 22 || (sw < 1 || 7 < sw)) // avoid out-of-bounds errors 
    { 
    printf("\n Invalid buffer size %d", size); 
    return NULL; 
    } 
    x = (unsigned char*)malloc(size);  // why not new ? 
             // why not passing an array for result? 
    if (!x) 
     return NULL; 
    memset(x, 0, size); // dynamic memory needs to be initialized. 

    //select line assignment to array 

    size_t offset = sw - 1; 

    x[offset + 0] = (PIN & 0x01) ? '1' : '0'; 
    x[offset + 1] = (PIN & 0x02) ? '1' : '0'; 
    x[offset + 2] = (PIN & 0x04) ? '1' : '0'; 
    return x; 
} 

Wie für main(), überprüfen Sie nicht für Zuordnungsfehler Im Rückgabewert von command() leckst du jeden einzelnen Puffer, der von command() zurückgegeben wird, und du gibst d Werte zu, was ein nicht initialisierter Zeiger ist, der sehr wahrscheinlich einen Segmentierungsfehler erzeugt.

+0

Das neue, das nicht benutzt wird, ist, weil ich Keil benutze und der Albtraum sonst wegen meiner schwachen Zeigerfähigkeiten ist. Danke für die Hilfe! – aurelius

0
unsigned char *d; // <-- d is an uninitialized pointer 
x=command(PIN,sw,size);//sw = 1, ****x is assigned a block of memory within command so that's good**** 
for (i = 0; i < size; i++) 
{ 
    if (x[i]!='\0') 
    { 
     d[i]=x[i]; // ****<-- where did you assign d to a block of memory?**** 
    } 
} 

Die zuweisen offensichtliches Problem, das ich sehe, ist die Zuweisung zum nicht initialisierten Zeiger d. Sie müssen dafür einen Speicherblock reservieren. Es könnte auch andere Probleme in dem Programm geben, die Sie debuggen müssen, um zu finden.

+0

Verpasste das auf Anhieb! Ich muss müde werden. Der Code gibt jetzt ein vollständiges Array von 0 und 1 zurück, zusammen mit einigen Werten wie y und einem Smiley. – aurelius

Verwandte Themen