Frage

The following code prints out 5. Can someone explain it to me? I think I am mostly confused about the math; for example, using '0' instead of 0 and how I can do that math on paper...

      #include <stdio.h>

      int main (int argc , char * argv [])
      {
       char * c_pt ;
       int n = 0;
       if( argc == 2)
       {
         c_pt = argv [1];
         while (* c_pt )
         {
           if (* c_pt < '0' || * c_pt > '1') break ;
           n = n*2 + * c_pt - '0';
           c_pt ++;
         }
         printf ("%d\n", n);
       }
     }
War es hilfreich?

Lösung 2

  void main ( void )
  {
    int a[] = {22, 33,44};

'a' is a static array (or string) of 3 int, 22, 33, and 44.

    int *x = a;

'x' is an int pointer, initialized to point to the same static array as 'a'.

    printf (" sizeof ( int )=% lu ", sizeof (int ));

Prints the number of bytes [4] required to represent an int type on this system.

    printf ("x=%p, x [0]=% d\n", x, x [0]);

Prints the memory address where the int pointer 'x' is currently pointing[0x7fff29af6530], then also prints the integer value [22] stored in the [4] bytes starting at that address. (Note: 'x[0]' is the same as '*x').

    x = x + 2;

    x   [0x7fff29af6530]
   +2               + 8 (or (2 * 4) or (2 * sizeof(int)))
  ----  ----------------
new x   [0x7fff29af6538]

Advance the pointer 'x' 8 bytes.

The effect on 'x' is that it will now be pointing at its original memory address plus '(2 * sizeof(int))' bytes. 'a[2]' resolves to the same location.

    printf ("x=%p, x [0]=% d\n", x, x[0]);

Prints the memory address where the int pointer 'x' is currently pointing [0x7fff29af6538] then also prints the integer value [44] stored in the [4] bytes starting at that address.

Hence; 'x' now resolves to the same address as '&a[2]'; and '*x' resolves to the same number as 'a[2]'.

   }

Andere Tipps

When you access the characters in a string containing digits you might get the character '0' or '1' for example. But the integer value of '0' is 48 and the integer value of '1' is 49 so if you want to turn them into the integers 0 and 1 for arithmetic you need to substract something. You could subtract 48 but then the next person that reads your code has no idea why you did that. So nornally you subtract the character represented by the value: '0' - '0' = 0 and '1' - '0' = 1

This particular program reads a string containing a binary number from the command line (so the string contains the characters '0' and '1') and converts that binary number to a decimal number by first converting the '0' and '1' at *c_pt to 0 and 1 and then adding it to the decimal number being built in n. It does that by starting with 0 and then on each iteration multiplying the number by two (which just shifts everything left one bit) and then adding the next digit. So if the string contained 10101 it would follow these steps:

number  = 0

input   = 10101
pointer = ^
number  = number*2 + *pointer = 0*2 + ('1'-'0')  = 0*2 + 1  = 1  (in binary: 1)

input   = 10101
pointer =  ^
number  = number*2 + *pointer = 1*2 + ('0'-'0')  = 1*2 + 0  = 2  (in binary: 10)

input   = 10101
pointer =   ^
number  = number*2 + *pointer = 2*2 + ('1'-'0')  = 2*2 + 1  = 5  (in binary: 101)

input   = 10101
pointer =    ^
number  = number*2 + *pointer = 5*2 + ('0'-'0')  = 5*2 + 0  = 10 (in binary: 1010)

input   = 10101
pointer =     ^
number  = number*2 + *pointer = 10*2 + ('1'-'0') = 10*2 + 1 = 21 (in binary: 10101)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top