Gotchas: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) |
Puppydrum64 (talk | contribs) |
||
Line 203: | Line 203: | ||
As using <code>sizeof</code> on a memory address returns the number of bytes that the CPU's instruction pointer register can hold, you'll get the same return value from <code>gotcha</code> regardless of how many elements the array has. When C programmers talk about "arrays decaying into pointers" this is what they're referring to. |
As using <code>sizeof</code> on a memory address returns the number of bytes that the CPU's instruction pointer register can hold, you'll get the same return value from <code>gotcha</code> regardless of how many elements the array has. When C programmers talk about "arrays decaying into pointers" this is what they're referring to. |
||
In order for a function to use an array's intended size, it must be passed in as a separate argument. |
|||
<syntaxhighlight lang="C">int foo(char buf[],int length){} |
|||
int main() |
|||
{ |
|||
char myArray[30]; |
|||
int j = foo(myArray,sizeof(myArray)); //passes 30 as the length parameter. |
|||
} |
|||
}</syntaxhighlight> |
|||
===gets()=== |
===gets()=== |