C Prototype Area

I learned that

a type specifier that declares an identifier in the list of declaration parameters in the function prototype (is not part of the function definition), the identifier has a function prototype volume that ends at the end of the function declarator.

Please see program C below.

void fn (struct st {int a;} a, struct st b) ;

struct st obj ;

Compilers promptly throw an error, because the size of "obj" is unknown (or) struct st is not a "type". This is true! the declaration of the "struct st" structure ends in the prototype declaration.

I believe the prototype had this limit because we can also use some variable names in prototype declarations. These names may conflict with variables in the same scope (as with the function prototype). Like below.

void fn (int a) ;
int a ;

, , . ( , )

. , " "? ? ()?

+3
4

.

:

void mem_set(void *, int, int);

void mem_set(void *buffer, int value, int nbytes);

?


, , . , , (, ), , . , :

struct st {int a;};
void fn(struct st a, struct st b);

:

, , . ( ). .

void fn(int a);
int a;

, , . ( , )

, GCC '-Wshadow' 'a', 'a' - , , . ; C - - .


" C ", " ++ ":

/**/, ( ), . , "-", . -

- . , ++, , . . " ++" Stroustrup. , - . . -

, , , - " ?". , , . , . ( ), OP, , ( ++, ). - AndreyT

@AndreyT: ( ). , , - , , , , , , , . -

@Jonathan Leffler: , , ( " ++" - ). , , " ". ? ++ . ++ . - AndreyT

@AndreyT Yeh! :) -

Counter-

, ++ " ".

#include <cstdio>
using namespace std;

extern void x(struct c {int y;} b);

void x(struct c b)
{
    printf("b.y = %d\n", b.y);
}

int main()
{
    struct c a;
    a.y = 0;
    x(a);
    return(0);
}

g++ (4.0.1 MacOS X 10.5.8). :

$ g++ -o xx xx.cpp
xx.cpp:4: error: types may not be defined in parameter types
$

/ - .

, , "C ++" . , ++, , ++ ?

+4

, chrisharris . , . , . , , , , , , .

, , , , , .

, - , , ( ), . - " ". , , .

, , C - :

struct {
    int obj;
};

.

+2

, void *, . :

int f(void *);

. :

int f(struct dummy *);

, void *.

- , , - , .

+1

Two - let's say there are three obvious reasons for having parameter names in function prototypes:

  • Names indicate the purpose of each parameter.
  • Names make it easier to refer to parameters in the API documentation (for example, in Doxygen).
  • You can easily generate prototypes (cut, paste, or automate) from definitions.
0
source

Source: https://habr.com/ru/post/1722900/


All Articles