How to use void pointers in C

1 Answer

0 votes
#include <stdio.h>
 
void printBytes(void *arr, int n) {
    for (int i = 0; i < n; i++) {
        printf("%02x ", ((char*)arr)[i]);
    }
}
 
    
int main() {
    int arr[] = { 15, 31, 32, 92 };
     
    printBytes(arr, 4 * sizeof(int));
 
    return 0;
}
     
     
          
/*
run:
       
0f 00 00 00 1f 00 00 00 20 00 00 00 5c 00 00 00 
    
*/

 



answered Dec 25, 2020 by avibootz

Related questions

2 answers 126 views
126 views asked May 12, 2023 by avibootz
1 answer 109 views
109 views asked May 30, 2023 by avibootz
1 answer 132 views
1 answer 140 views
140 views asked Jun 21, 2017 by avibootz
1 answer 167 views
167 views asked May 5, 2017 by avibootz
...