Ryan Li
Ryan Li

Reputation: 9330

What return type should I declare a function with if I don't care about the return value?

In my C file, I am calling a function that is defined in a generated file, lex.yy.c:

yy_scan_string(input);

which is declared as following in lex.yy.c:

YY_BUFFER_STATE yy_scan_string(yyconst char *yystr);

And in turn YY_BUFFER_STATE is an typedef:

typedef struct yy_buffer_state *YY_BUFFER_STATE;

None of these are available to include from a header file. While in order to call yy_scan_string I will have to declare it first.

What would be the best way to declare this function in my source file, when I don't care about the return value at all, and it's infeasible to include the type definition of the return type?

Upvotes: 3

Views: 241

Answers (3)

Abhyudit Jain
Abhyudit Jain

Reputation: 3748

Just make its type void

For example:

#include <stdio.h>
void helloworld();

void main()
{
  helloworld();
}

void helloworld()
{
    printf("Hello World");
}

Upvotes: -4

kaylum
kaylum

Reputation: 14046

Declare it as exactly how you have described but with the struct defined as an opaque type. As long as you don't dereference the return value (and it sounds like you won't) then that'll work just fine.

struct yy_buffer_state;
typedef struct yy_buffer_state *YY_BUFFER_STATE;
YY_BUFFER_STATE yy_scan_string(yyconst char *yystr);

Upvotes: 4

Persixty
Persixty

Reputation: 8589

The universally accepted procedure is to define the function in a header-file and include that file at the start of the file you call it from.

If for some reason that header file hasn't be provided you would make it.

If it's only a handful of functions you could declare them locally in the .c file.

The function returns a pointer to the structure so you can use the forward declaration:

typedef struct yy_buffer_state *YY_BUFFER_STATE;

However I can only recommend very strongly you remove those declarations to their own file.

Upvotes: 2

Related Questions