C and Call by Value query

Started by
3 comments, last by ToohrVyk 16 years, 1 month ago
I'm just reviewing C and its Call by Value mechanism. I've stumble upon the wglUseFontOutlines function - http://msdn2.microsoft.com/en-us/library/ms537567.aspx When I examined the wglUseFontOutlines function signature/parameter, I've notice that the HDC and LPGLYPHMETRICSFLOAT parameters are NOT declared as pointers but rather as regular/normal parameters. With that in mind, can someone please educate me on how wglUseFontOutlines is modifying/updating the actual parameters passed in for HDC and LPGLYPHMETRICSFLOAT when according to the K & R book HDC and LPGLYPHMETRICSFLOAT should be only "LOCAL" copies to the wglUseFontOutlines function? Many thanks.
Advertisement
HDC is a handle - "Handle to Display Context".

LPGLYPHMETRICSFLOAT is hungarian notation for "pointer to array of GLYPHMETRICSFLOAT" - as the documentation describes it.
So the Call by Value concept only work for struct, union, primitives and NOT for Array, Pointers in C?
Quote:Original post by gp343
NOT for Array, Pointers in C?


Without going into pedantic details...

Arrays are almost identical to pointers, and dynamically allocated arrays are pointers.

Pointers get passed by value, but their contents don't. If you pass a struct, you pass contents. If you pass a pointer to struct, you copy (by value) value of pointer (usually a memory address), but not the actual contents. Same for arrays.

If you have pointer to array of 4 ints, the memory layout looks like this:
0x00360x0040  ptr_to_0x0680  // pointer to array0x0044......0x06760x0680 80x0684 90x0688 100x0672 11


Then, of you read memory address of 0x0040, you get value of 0x0680. That is pointer to array of 4 ints (8,9,10,11).

When you pass this pointer to a function, the value 0x0680 is copied effectively as int, and by value, but the contents of array are not.

PS: simplified version, in reality there's a few details, but the above concept is universal.

In memory managed languages, pointer are handles, and are not required to be related to actual memory layout, or memory addresses.


Quote:Original post by gp343
So the Call by Value concept only work for struct, union, primitives and NOT for Array, Pointers in C?


Call by value applies to everything in C: if you pass a value as an argument, a copy of that value will be available in the called function.

The only exception here is arrays: arrays cannot be passed as arguments in C. If something looks like you're passing an array as an argument, in fact the compiler will have converted it to a pointer argument and silently converted your array to a pointer to its first element. However, even then, the resulting pointer is passed by value.

This topic is closed to new replies.

Advertisement