Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Nothing, they work in the same way. Here a short example:</p> <pre><code>int WithArray(int array[10]) { return array[1] + array[2]; // breakpoint 1 } int WithPointer(int *pointer) { return *(pointer + 1) + *(pointer + 2); // breakpoint 2 } void main() { int array[] = {0,1,2,3,4,5,6,7,8,9}; int b = WithPointer(array); int a = WithArray(array); printf("a = %d\nb = %d\n", a, b); } </code></pre> <p>Ok, I'll call WithPointer() first, just in case WIthArray() copies the array on the stack. Here's the stack at the second breakpoint:</p> <pre><code>Breakpoint 2, WithPointer (pointer=0xbffff418) at prova.c:10 10 return *(pointer + 1) + *(pointer + 2); (gdb) x/20x ($esp - 8) 0xbffff404: 0x08049ff4 0xbffff418 0xbffff448 0x0804843b 0xbffff414: 0xbffff418 0x00000000 0x00000001 0x00000002 0xbffff424: 0x00000003 0x00000004 0x00000005 0x00000006 0xbffff434: 0x00000007 0x00000008 0x00000009 0x08048460 0xbffff444: 0x00000000 0xbffff4c8 0x00144bd6 0x00000001 </code></pre> <p>As expected, there's our pointer (0xbffff418, the first value on the second line) and, right after that, array[] (which is on main()'s stack frame). Let's check the stack inside WithArray():</p> <pre><code>(gdb) continue Continuing. Breakpoint 1, WithArray (array=0xbffff418) at prova.c:5 5 return array[1] + array[2]; (gdb) x/20x ($esp - 8) 0xbffff404: 0x08049ff4 0xbffff418 0xbffff448 0x08048449 0xbffff414: 0xbffff418 0x00000000 0x00000001 0x00000002 0xbffff424: 0x00000003 0x00000004 0x00000005 0x00000006 0xbffff434: 0x00000007 0x00000008 0x00000009 0x08048460 0xbffff444: 0x00000003 0xbffff4c8 0x00144bd6 0x00000001 </code></pre> <p>Exactly the same thing! So there's no difference about how they're passed to functions. And they're handled in the same way too, look:</p> <pre><code>(gdb) disass WithPointer Dump of assembler code for function WithPointer: 0x080483cc &lt;+0&gt;: push %ebp 0x080483cd &lt;+1&gt;: mov %esp,%ebp 0x080483cf &lt;+3&gt;: mov 0x8(%ebp),%eax # get base address 0x080483d2 &lt;+6&gt;: add $0x4,%eax # compute offset 0x080483d5 &lt;+9&gt;: mov (%eax),%edx # dereference and get val. 0x080483d7 &lt;+11&gt;: mov 0x8(%ebp),%eax # base address 0x080483da &lt;+14&gt;: add $0x8,%eax # offset (2 * sizeof(int)) 0x080483dd &lt;+17&gt;: mov (%eax),%eax # get *eax 0x080483df &lt;+19&gt;: lea (%edx,%eax,1),%eax # tricky way to add them 0x080483e2 &lt;+22&gt;: pop %ebp 0x080483e3 &lt;+23&gt;: ret End of assembler dump. (gdb) disass WithArray Dump of assembler code for function WithArray: 0x080483b4 &lt;+0&gt;: push %ebp 0x080483b5 &lt;+1&gt;: mov %esp,%ebp 0x080483b7 &lt;+3&gt;: mov 0x8(%ebp),%eax # first element of array 0x080483ba &lt;+6&gt;: add $0x4,%eax # move to the second 0x080483bd &lt;+9&gt;: mov (%eax),%edx # and get its value 0x080483bf &lt;+11&gt;: mov 0x8(%ebp),%eax # base of array 0x080483c2 &lt;+14&gt;: add $0x8,%eax # compute address of second 0x080483c5 &lt;+17&gt;: mov (%eax),%eax # element and get load it 0x080483c7 &lt;+19&gt;: lea (%edx,%eax,1),%eax # compute sum 0x080483ca &lt;+22&gt;: pop %ebp 0x080483cb &lt;+23&gt;: ret End of assembler dump. </code></pre> <p>Codes are identical. Note that the array is handled as a pointer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload