Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
The C programming language is very flexible and allows many things, including allowing the programmer who is not very careful to get into trouble.
The follow code shows how a simple variable called x1 can be passed by reference to two procedures.
Procedure printArray1has an array a1 as a formal parameter.
Procedure printArray2has a pointer variable a1 as a formal parameter.
The length n1 is passed by value.
The code of both procedures is the same. Note how the procedures assume they have an array but the passed variable is only a simple variable!
2. Body
Here is the code of the function body - the same for both procedures.
Here is the C code.
Here is the output of the C code.
3. Compiler differences
Note that even though the printArray1 and printArray2 procedures access memory as if it were an array, the compiler may not assign local variables x1, x2 , x3 and x4 to contiguous memory locations. And the exact relative locations may change from compiler to compiler.
4. Formal parameters
In the C programming language, the following formal parameters are considered the same in the body in that they both can be used as an array.
int a1[] is a formal parameter considered an array passed by reference.
int *a1 is a formal parameter considered a variable passed by reference but which can be accessed as if it were an array.
So, a pointer reference to memory can be accessed as a variable or as an array where the address is a pointer into an array of memory.
5. Implications
If you are a computer engineer or a software engineer coding at a low level, this is good news. You can access computer memory via pointers as if it were an array.
If you are coding at a high level, this is not good news. There are many things that C will allow you to do which may cause strange issues or problems with your code.