Pointer size and Pass

I have two questions:

  • Am I right that on 4-bit systems, a pointer is 4 bytes?

  • Are "pass by reference" and "pass by pointer the same thing, just different wording?


  • Am I right that on 4-bit system a pointer size is 4

    If your system has 1-bit bytes, then for sure it is. (But C doesn't support platforms where bytes are shorter than 8 bit anyway.)

    Is "pass by reference" and "pass by pointer" the same thing, but different wording?

    No. Pass-by-pointer is a C approach for emulating pass-by-reference. The concept is different.


    The size of a pointer doesn't necessarily correlate to the native word size of the CPU; for example, the original Macintosh ran on a 32-bit processor (Motorola 68000) which only had 24 address lines, so pointers were limited to 24 bits. The pointer value was stored in a 32-bit word, but the top 8 bits weren't used. Some enterprising programmers used those top 8 bits to store other data with the pointer. This caused some heartburn when the 68020 came out, which had 32 address lines. It took a while to rewrite that code so that it was "32-bit clean".

    Note also that pointers to different types need not be the same size.

    In practice, on any modern desktop system (read: x86) all pointer types will be either 32 or 64 bits wide. But don't rely on that being true for all architectures.

    As for pass by "pass by reference" and "pass by pointer", no, they are not simply different wordings of the same concept.

    In a pass-by-reference system, the formal parameter in the function definition and the actual parameter in the function call designate the same memory (or at least changes to one are reflected in the other). Looking at some old-school Fortran code:

    C234567890
          PROGRAM CALLSW
          INTEGER M, N
    
          M = 1
          N = 2
    
          WRITE(*,*) M, N
          CALL ISWAP(M, N)
          WRITE(*,*) M, N
    
          STOP
          END
    
    C234567890
          SUBROUTINE ISWAP(A, B)
          INTEGER A, B
          INTEGER TMP
    
          TMP = A
          A = B
          B = TMP
    
          RETURN
          END
    

    The formal parameter A in ISWAP designates the same object in memory as M in the main program, so writing to A changes the value in M . You can think of A as a pointer to M (or that A and M are both pointers to the same object), but the language hides that pointer-ness from the programmer.

    C passes everything by value; the formal parameter and the actual parameter always designate different objects in memory, so writing to one doesn't affect the other. When we want to modify an object in a subroutine, we must explicitly pass its address using the & operator, and then dereference it in the subroutine with the * operator:

    void iswap(int *a, int *b)
    {
      int tmp;
    
      tmp = *a;
      *a = *b;
      *b = tmp;
    }
    
    int main(void)
    {
      int m, n;
    
      m = 1;
      n = 2;
    
      printf("m = %d, n = %dn", m, n);
      iswap(&x, &y);
      printf("m = %d, n = %dn", m, n);
    
      return 0;
    }
    

    Instead of passing m and n to iswap , we pass the results of the expressions &m and &n , which are pointers to the two objects. Similarly, in the iswap function, we don't write to a or b , we write to the results of the expressions *a and *b . a and m refer to two completely different objects in memory, as do b and n . Writing to a will not affect m at all.


    Regarding parameter passing in C, Wikipedia's entry on C states:

    Function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values.

    链接地址: http://www.djcxy.com/p/20734.html

    上一篇: (C ++)将指针传递给模板

    下一篇: 指针大小和通行证