Thread Dezimalzahl in Dualzahl umwandeln (37 answers)
Opened by SirLant at 2003-09-08 18:55

esskar
 2003-09-11 21:33
#11044 #11044
User since
2003-08-04
7321 articles
ModeratorIn

user image
[quote=kabel,08.09.2003, 22:35]dez_to_bin_functional (int zahl) {
       int i = 0, j = 0;
       char *binary_rep = NULL;
       binary_rep = (char *) calloc (sizeof (char), i);
       for (i=7, j=0; i >= 0; i--, j++) {
               binary_rep[j] = (zahl&(1<<i)) ? '1' : '0';
       }
       return binary_rep;
}[/quote]
calloc
Allocates an array in memory with elements initialized to 0.

void *calloc( size_t num, size_t size );

Parameters
num
Number of elements
size
Length in bytes of each element


Code: (dl )
binary_rep = (char*)calloc (sizeof (char), i);


alloziert also ein char-array von 4 Elementen, wobei jedes Element i groß.
da i = 0, könnte das ganze eskalieren... :)

View full thread Dezimalzahl in Dualzahl umwandeln