Pregunta

Why is the passing of char array not showing? The location of the pointer is passed to a function.

char plaintext[] = {
         0xCD,  0x76,  0x43,  0xF0,
         0x72,  0xA4,  0xA0,  0x82,
}

Given

 void displayArray(char** plaintext, int size) {
 // int newSize = sizeof(**plaintext);
 int i;

 for(i=0; i < size; ++i) {
     printf("%02X ", (0xff & *plaintext[i]));
    if (((i+1)% 8) == 0)    // as index starts from 0, (i+1)
        printf("\n");
 }
}

in main()

        char* plaintextCopy;
        plaintextCopy = (char*) malloc(numberOfItems*sizeof(char));

        memcpy(plaintextCopy, plaintext, numberOfItems);

        displayArray(&plaintextCopy, numberOfItems);
¿Fue útil?

Solución

Based on your code :

void displayArray(char** plaintext, int size)
{
    int i;

    for(i=0; i < size; i++) 
    {
        printf("%02X ", (0xff & (*plaintext)[i]));
        if(((i+1)% 8) == 0)    // as index starts from 0, (i+1)
            printf("\n");
    }
}

int main(void)
{


    char plaintext[] = {
         0xCD,  0x76,  0x43,  0xF0,
         0x72,  0xA4,  0xA0,  0x82,
    };

    int numberOfItems = sizeof(plaintext);

     char* plaintextCopy;
     plaintextCopy = (char*) malloc(numberOfItems*sizeof(char));

     memcpy(plaintextCopy, plaintext, numberOfItems);

     displayArray(&plaintextCopy, numberOfItems);

    return 0;
}

It outputs :

CD 76 43 F0 72 A4 A0 82

Also, if you're sending an array that you want to display or change the values of, you don't need to send a double pointer to a function. A regular pointer would do. You should only use double pointers if the original array changes it's location in memory, that is, it's getting a new pointer after the function returns.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top