Recent

Author Topic: C header function with only a type but no var input?  (Read 641 times)

jamie

  • Hero Member
  • *****
  • Posts: 7701
C header function with only a type but no var input?
« on: April 15, 2026, 12:57:17 pm »
I have seen this before but the body function always had a variable name so i could use that hower in this case both are void of having a variable name?

Example:

Virtural void function name(TheType*,.........);

Normally when i see this the body has a variable but this they are both the same?
Also i noticed there is no references being made of corse.

 I translating this, does the c compiler just use what ever you put in there on the fly?
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19129
  • Glad to be alive.
Re: C header function with only a type but no var input?
« Reply #1 on: April 15, 2026, 01:08:46 pm »
I need to know what comes after, because the * means nothing in the context you gave.
In C Virtural virtual void function name(TheType*)
The comma you put in is certainly not C so I deleted that part.
The * part means a dereferenced pointer type will follow (righthand side).
If the * is closer to the lefthand side it only means, but has otherwise no effect, that it notationally belongs to the left side.
IOW: it has no effect on compilation.

Therefor I need the righthand side. (C is not Python)
« Last Edit: April 15, 2026, 01:14:44 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

Fibonacci

  • Hero Member
  • *****
  • Posts: 938
  • Behold, I bring salvation - FPC Unleashed
Re: C header function with only a type but no var input?
« Reply #2 on: April 15, 2026, 01:25:15 pm »
  • TheType* means pointer to TheType - the * is part of the type, not a dereference. Dereference uses the same symbol but in an expression context: *param.
  • In C, parameter names are optional in both declarations and definitions.
  • Unnamed parameter in a body is still passed, just inaccessible. Common in callbacks.
  • The comma in the original likely means more parameters or variadic (...) - valid C.
  • When translating to Pascal, just invent names yourself. Only types and calling convention matter.
FPC Unleashed - inline vars, tuples, statement expressions, array equality, compound assignments, indexed/lazy labels, no-RTTI & more. ⭐ Star it on GitHub!

Thaddy

  • Hero Member
  • *****
  • Posts: 19129
  • Glad to be alive.
Re: C header function with only a type but no var input?
« Reply #3 on: April 15, 2026, 02:19:16 pm »
  • TheType* means pointer to TheType - the * is part of the type, not a dereference. Dereference uses the same symbol but in an expression context: *param.
Yeah, right, but it does not tell the compiler that....Sorry, but it is convention, not compiler.
Quote
  • In C, parameter names are optional in both declarations and definitions.
  • Unnamed parameter in a body is still passed, just inaccessible. Common in callbacks.
Correct.
Quote
  • The comma in the original likely means more parameters or variadic (...) - valid C.
  • When translating to Pascal, just invent names yourself. Only types and calling convention matter.
Then he uses/posted/wrote the wrong syntax. Variadics can be the issue, but not with that syntax.
I agree with var renaming.
« Last Edit: April 15, 2026, 02:21:51 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

440bx

  • Hero Member
  • *****
  • Posts: 6472
Re: C header function with only a type but no var input?
« Reply #4 on: April 15, 2026, 02:55:25 pm »
Got to be careful with that stuff because the "*" binds to the variable _not_ the type.  This is crucial when declaring more than one variable in a single line, e.g, int* a, b; the "*" associates with the variable "a" not the type "int", therefore that statement declares "a" to be a pointer to "int" and "b" to be an "int", not a pointer to "int" as is "a".

Unfortunately, seeing the "*" glued to the type is quite common but strictly speaking it is incorrect and a mistake that's quite easy for a Pascal programmer to make because in Pascal the "^" when declaring a type binds to the type, not the variable, e.g, a, b : ^integer; { both a and b are pointers to an integer }



FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Khrys

  • Sr. Member
  • ****
  • Posts: 434
Re: C header function with only a type but no var input?
« Reply #5 on: April 15, 2026, 02:58:15 pm »
Unused but nonetheless required parameters (e.g. when dealing with callbacks or virtual methods in C++) can be marked as such by not giving them names.

Got to be careful with that stuff because the "*" binds to the variable _not_ the type.  This is crucial when declaring more than one variable in a single line, e.g, int* a, b; the "*" associates with the variable "a" not the type "int", therefore that statement declares "a" to be a pointer to "int" and "b" to be an "int", not a pointer to "int" as is "a".

Unfortunately, seeing the "*" glued to the type is quite common but strictly speaking it is incorrect (and a mistake that's quite easy for a Pascal programmer to make because in Pascal the "^" when declaring a type binds to the type, not the variable, e.g, a, b : ^integer; { both a and b are pointers to an integer }

Yeah, type declarations (especially involving function pointers) in C are so bewildering that an entire website was made just to help demystify them.

440bx

  • Hero Member
  • *****
  • Posts: 6472
Re: C header function with only a type but no var input?
« Reply #6 on: April 15, 2026, 05:43:53 pm »
I'm not sure I understand what Jamie's question is but, maybe what follows is the answer...

Code: C  [Select][+][-]
  1. Virtual void function name(TheType*,.........);
  2.  
That's a function prototype and, prototypes don't need parameter names because the function's implementation is missing, therefore parameter names are irrelevant.

Pascal doesn't allow that.  A forward declaration, which is what a prototype essentially is, must have parameter names for every parameter, not just a data type (unlike in C.)

Whereever the function is implemented, parameter names will be present so they can be used in the function's body. 

Disclaimer: I don't know if C allows omitting the parameter name in the function's implementation if the parameter is unused.  My guess is that it does not, otherwise there would be no need to declare that some parameter is unused (it could have simply been left un-named if it were possible but, that's a guess on my part.)
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Khrys

  • Sr. Member
  • ****
  • Posts: 434
Re: C header function with only a type but no var input?
« Reply #7 on: April 16, 2026, 07:03:04 am »
I don't know if C allows omitting the parameter name in the function's implementation if the parameter is unused.  My guess is that it does not, otherwise there would be no need to declare that some parameter is unused (it could have simply been left un-named if it were possible but, that's a guess on my part.)

C does allow that - believe me, I was surprised too when I found out...

440bx

  • Hero Member
  • *****
  • Posts: 6472
Re: C header function with only a type but no var input?
« Reply #8 on: April 16, 2026, 09:13:38 am »
C does allow that - believe me, I was surprised too when I found out...
There is no limit to the non-sense they included in that language and, in spite of that, it is one of the most popular languages around. 
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018