0

我正在运行这个示例程序,并且我应该能够从输出中判断它是什么机器类型。我确定它来自检查一两个值,但我应该如何执行此检查?

/* pointers.c - Test pointers
 * Written 2012 by F Lundevall
 * Copyright abandoned. This file is in the public domain.
 *
 * To make this program work on as many systems as possible,
 * addresses are converted to unsigned long when printed.
 * The 'l' in formatting-codes %ld and %lx means a long operand. */

#include <stdio.h>
#include <stdlib.h>

int * ip; /* Declare a pointer to int, a.k.a. int pointer. */
char * cp; /* Pointer to char, a.k.a. char pointer. */

/* Declare fp as a pointer to function, where that function
 * has one parameter of type int and returns an int.
 * Use cdecl to get the syntax right, http://cdecl.org/ */
int ( *fp )( int );

int val1 = 111111;
int val2 = 222222;

int ia[ 17 ]; /* Declare an array of 17 ints, numbered 0 through 16. */
char ca[ 17 ]; /* Declare an array of 17 chars. */

int fun( int parm )
{
  printf( "Function fun called with parameter %d\n", parm );
  return( parm + 1 );
}

/* Main function. */
int main()
{
  printf( "Message PT.01 from pointers.c: Hello, pointy World!\n" );

  /* Do some assignments. */
  ip = &val1;
  cp = &val2; /* The compiler should warn you about this. */
  fp = fun;

  ia[ 0 ] = 11; /* First element. */
  ia[ 1 ] = 17;
  ia[ 2 ] = 3;
  ia[ 16 ] = 58; /* Last element. */

  ca[ 0 ] = 11; /* First element. */
  ca[ 1 ] = 17;
  ca[ 2 ] = 3;
  ca[ 16 ] = 58; /* Last element. */

  printf( "PT.02: val1: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val1, val1, val1 );
  printf( "PT.03: val2: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val2, val2, val2 );
  printf( "PT.04: ip: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &ip, (long) ip, (long) ip );
  printf( "PT.05: Dereference pointer ip and we find: %d \n", *ip );
  printf( "PT.06: cp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &cp, (long) cp, (long) cp );
  printf( "PT.07: Dereference pointer cp and we find: %d \n", *cp );

  *ip = 1234;
  printf( "\nPT.08: Executed *ip = 1234; \n" );
  printf( "PT.09: val1: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val1, val1, val1 );
  printf( "PT.10: ip: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &ip, (long) ip, (long) ip );
  printf( "PT.11: Dereference pointer ip and we find: %d \n", *ip );
  printf( "PT.12: val1: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val1, val1, val1 );

  *cp = 1234; /* The compiler should warn you about this. */
  printf( "\nPT.13: Executed *cp = 1234; \n" );
  printf( "PT.14: val2: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val2, val2, val2 );
  printf( "PT.15: cp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &cp, (long) cp, (long) cp );
  printf( "PT.16: Dereference pointer cp and we find: %d \n", *cp );
  printf( "PT.17: val2: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val2, val2, val2 );

  ip = ia;
  printf( "\nPT.18: Executed ip = ia; \n" );
  printf( "PT.19: ia[0]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ia[0], ia[0], ia[0] );
  printf( "PT.20: ia[1]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ia[1], ia[1], ia[1] );
  printf( "PT.21: ip: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &ip, (long) ip, (long) ip );
  printf( "PT.22: Dereference pointer ip and we find: %d \n", *ip );

  ip = ip + 1; /* add 1 to pointer */
  printf( "\nPT.23: Executed ip = ip + 1; \n" );
  printf( "PT.24: ip: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &ip, (long) ip, (long) ip );
  printf( "PT.25: Dereference pointer ip and we find: %d \n", *ip );

  cp = ca;
  printf( "\nPT.26: Executed cp = ca; \n" );
  printf( "PT.27: ca[0]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ca[0], ca[0], ca[0] );
  printf( "PT.28: ca[1]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ca[1], ca[1], ca[1] );
  printf( "PT.29: cp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &cp, (long) cp, (long) cp );
  printf( "PT.30: Dereference pointer cp and we find: %d \n", *cp );

  cp = cp + 1; /* add 1 to pointer */
  printf( "\nPT.31: Executed cp = cp + 1; \n" );
  printf( "PT.32: cp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &cp, (long) cp, (long) cp );
  printf( "PT.33: Dereference pointer cp and we find: %d \n", *cp );

  ip = ca; /* The compiler should warn you about this. */
  printf( "\nPT.34: Executed ip = ca; \n" );
  printf( "PT.35: ca[0]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ca[0], ca[0], ca[0] );
  printf( "PT.36: ca[1]: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &ca[1], ca[1], ca[1] );
  printf( "PT.37: ip: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &ip, (long) ip, (long) ip );
  printf( "PT.38: Dereference pointer ip and we find: %d \n", *ip );

  cp = ia; /* The compiler should warn you about this. */
  printf( "\nPT.39: Executed cp = ia; \n" );
  printf( "PT.40: cp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &cp, (long) cp, (long) cp );
  printf( "PT.41: Dereference pointer cp and we find: %d \n", *cp );

  printf( "\nPT.42: fp: stored at %lx (hex); value is %ld (dec), %lx (hex)\n",
          (long) &fp, (long) fp, (long) fp );
  printf( "PT.43: Dereference fp and see what happens.\n" );
  val1 = (*fp)(42);
  printf( "PT.44: Executed val1 = (*fp)(42); \n" );
  printf( "PT.45: val1: stored at %lx (hex); value is %d (dec), %x (hex)\n",
          (long) &val1, val1, val1 );

  return( 0 );
}

输出

Message PT.01 from pointers.c: Hello, pointy World!
PT.02: val1: stored at 21e50 (hex); value is 111111 (dec), 1b207 (hex)
PT.03: val2: stored at 21e54 (hex); value is 222222 (dec), 3640e (hex)
PT.04: ip: stored at 21eb8 (hex); value is 138832 (dec), 21e50 (hex)
PT.05: Dereference pointer ip and we find: 111111
PT.06: cp: stored at 21e6c (hex); value is 138836 (dec), 21e54 (hex)
PT.07: Dereference pointer cp and we find: 0

PT.08: Executed *ip = 1234;
PT.09: val1: stored at 21e50 (hex); value is 1234 (dec), 4d2 (hex)
PT.10: ip: stored at 21eb8 (hex); value is 138832 (dec), 21e50 (hex)
PT.11: Dereference pointer ip and we find: 1234
PT.12: val1: stored at 21e50 (hex); value is 1234 (dec), 4d2 (hex)

PT.13: Executed *cp = 1234;
PT.14: val2: stored at 21e54 (hex); value is -771529714 (dec), d203640e (hex)
PT.15: cp: stored at 21e6c (hex); value is 138836 (dec), 21e54 (hex)
PT.16: Dereference pointer cp and we find: -46
PT.17: val2: stored at 21e54 (hex); value is -771529714 (dec), d203640e (hex)

PT.18: Executed ip = ia;
PT.19: ia[0]: stored at 21e74 (hex); value is 11 (dec), b (hex)
PT.20: ia[1]: stored at 21e78 (hex); value is 17 (dec), 11 (hex)
PT.21: ip: stored at 21eb8 (hex); value is 138868 (dec), 21e74 (hex)
PT.22: Dereference pointer ip and we find: 11

PT.23: Executed ip = ip + 1;
PT.24: ip: stored at 21eb8 (hex); value is 138872 (dec), 21e78 (hex)
PT.25: Dereference pointer ip and we find: 17

PT.26: Executed cp = ca;
PT.27: ca[0]: stored at 21e58 (hex); value is 11 (dec), b (hex)
PT.28: ca[1]: stored at 21e59 (hex); value is 17 (dec), 11 (hex)
PT.29: cp: stored at 21e6c (hex); value is 138840 (dec), 21e58 (hex)
PT.30: Dereference pointer cp and we find: 11

PT.31: Executed cp = cp + 1;
PT.32: cp: stored at 21e6c (hex); value is 138841 (dec), 21e59 (hex)
PT.33: Dereference pointer cp and we find: 17

PT.34: Executed ip = ca;
PT.35: ca[0]: stored at 21e58 (hex); value is 11 (dec), b (hex)
PT.36: ca[1]: stored at 21e59 (hex); value is 17 (dec), 11 (hex)
PT.37: ip: stored at 21eb8 (hex); value is 138840 (dec), 21e58 (hex)
PT.38: Dereference pointer ip and we find: 185664256

PT.39: Executed cp = ia;
PT.40: cp: stored at 21e6c (hex); value is 138868 (dec), 21e74 (hex)
PT.41: Dereference pointer cp and we find: 0

PT.42: fp: stored at 21e70 (hex); value is 69288 (dec), 10ea8 (hex)
PT.43: Dereference fp and see what happens.
Function fun called with parameter 42
PT.44: Executed val1 = (*fp)(42);
PT.45: val1: stored at 21e50 (hex); value is 43 (dec), 2b (hex)
4

2 回答 2

5

为什么不只是

unsigned short u = 0xABCD;
if ( *(const char *)&u == 0xAB )
   /*big endian*/;
else
   /*little endian*/;

这就是它起作用的原因:在大端架构中,最高有效字节具有变量(原始类型)中所有字节的最低地址,IOW 最高有效字节排在第一位。在小端架构中则相反:最低有效字节在前。这段代码只是检查地址最低的字节是否包含数字的最高有效字节。

更多关于字节序的信息

于 2012-10-18T10:58:38.740 回答
3

是的,您可以通过查看输出行来判断此代码​​是在大端还是小端机器上运行PT.07: Dereference pointer cp and we find: 0。如果取消引用的指针cp0一个大端机器,如果它是 14 它是一个小端机器。

原因:

cp是一个指向 char 的指针,它被分配了val2具有值的 int 变量的地址222222。如果你取消引用cp,你会得到第一个字节在大端但在小端val2中是二进制的。0000 00000000 1110

于 2012-10-18T11:54:11.400 回答