Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Why is C not consistent

Name: Anonymous 2012-05-10 1:43

Take the following two declarations "int arr[3]" and "int *ptr".

Both require different storage, but in usage both are identical. This behaviour  is known, and it is not the consistency I am talking about.

Take the following two declarations:

void funcarr(int a, int arr[3], int c)
{ printf("%d\n", &c-&a); }

void funcptr(int a, int *ptr, int c)
{ printf("%d\n", &c-&a); }

For funcarr, all three arguments are intended to be call by value. So I would expect the stack to contain
a,arr[0],arr[1],arr[2],c
but it does not. The stack contains
a,&arr[0],c

This is *NOT* consistent, and gives a false impression that the elements of arr[] will not change within the lifetime of the function.

Who failed, so I knows whose ass to kick.

Name: Anonymous 2012-05-14 2:17

>>38,41-42
The terms pass-by-value and pass-by-reference existed long before C++ or even C were created. They have long been used in programming language research to specify the semantics of a function call. Imagine a programming language that isn't C and where there are no pointers and you stumble upon a function call that looks like this:

f(x)

If x is passed by reference, it means that f operates on the same value. So if f modifies the structure of its argument in any way, the changes can be seen in x after the function returns.

On the other hand, if x is passed by value, then no matter what f does to its argument, you'll notice no change in the value of x.

If an argument is never modified within a function, there is no semantic difference between the two, and either can be used. Whether or not the compiler always makes a copy when a function is pass-by-value is an implementation detail.

However, there are pointers in C, so I understand that you are confused. Yes, everything is pass-by-value in C. Yet, when a parameter is a pointer, you sometimes say that the argument is passed-by-reference. But you might also pass a function a pointer to a struct and still say it's semantically call-by-value if the function doesn't modify the struct (and this is what const parameters are used for). So pointers can be used on a higher level to implement both pass-by-reference and pass-by-value. Your confusion stems from the fact that in C, the way parameters are passed is made explicit and you might have to take a step back to see the semantics. It might not be seen if you look too closely at the code (and more so if you think of programming as syntax).

>>44-45
And I'll have you know that my code has been burned to ROM and shipped on consumer devices all over the world. The devices are vibrators, one of which accidentally got your mother pregnant with you (oh wow, it's the 12th anniversery already).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List