-3

I got a snippet of C code from online, and I am trying to compile it. I am getting the following error.

error: expected declaration specifiers or '...' before ')' token
    6 | extern FUNC(void, MY_CODE) MyLatestUpdate()(void);
      |                          ^
ERROR!
/tmp/RkrMFH0deT.c: In function 'main':
/tmp/RkrMFH0deT.c:11:41: error: called object is not a function or function pointer
   11 | #define STD_ON                          1u
      |                                         ^~

Below is the snippet of the code

In .h file

#define MY_CODE

extern FUNC(void, MY_CODE) MyLatestUpdate()(void);

In another .h file

#define MyLatestUpdate()    MyPreValue()
#define MyPreValue()        UPDATE_VALUE
#define UPDATE_VALUE        STD_ON
#define STD_ON              1u

in .c file

int main() {
    // Write C code here
    MyLatestUpdate()();
    printf("test code");
    return 0;
}

Seems like I am getting the error in the .h file

extern FUNC(void, MY_CODE) MyLatestUpdate()(void);

error: expected declaration specifiers or '...' before ')' token

Is the function declared as a function pointer or a function macro? How can I resolve these issues? Any help

4
  • 1
    Get your code snippets from elsewhere. This is just godawful. Check if your compiler has an option to display preprocessor output. Commented Aug 22, 2024 at 10:05
  • 2
    Those days when you look for a code online rather than designing and coding yourself, and then you pick a weird code, and then have to troubleshoot all sorts of warning and errors it brings in! Commented Aug 22, 2024 at 10:07
  • 1
    I am sure I am drunk right now, but won't MyLatestUpdate()(); be in fact 1u(); which causes the error "called object is not a function or function pointer." ? This appears clear as a daylight to me. Commented Aug 22, 2024 at 10:12
  • 1
    @WedaPashi If that’s what you read, you are sober. If you wrote code like that I’d suspect you are drunk :-) Commented Aug 22, 2024 at 10:15

2 Answers 2

3

Function declarations

A modern function declaration in C will at least specify

  • the return value type or void
  • the name
  • argument types with their (optional) names or void

The specification of extern binding is implied for function declarations. The return value is missing (which is acceptable until C23) but the comma after the void parameter specification is syntactically wrong.

Preprocessor macros

In your example, MY_CODE is defined but doesn't have a text to be replaced with (which can be used to branch in the preprocessor for creating code variants). So will have a hard time to use it productively between a comma and a closing parenthesis in C. You may, on the other hand, include also commas into macro definitions.

Function pointers (as arguments)

From your comment on the accepted answer, I read that you are trying to call a function with a function pointer as an argument. Function pointers look a bit complicated, so they are often made more accessible via typedef:

/* translate1: pointer to a function that takes and returns a char */
char (*translate1)(char);

/* translate_function_t is a name for above function type 
   translate2 is again a pointer to a function that takes and returns a char
*/
typedef char (*translate_function_t)(char);
translate_function_t translate2;

/* transliterate: replaces each char in str by calling your_translate */
void transliterate(char* str, translate_function_t your_translate);

The transliterate function in the above example takes function pointer arguments in the second parameter your_translate.

Simple function pointer example

#include <stdio.h>

/* translate_function_t: pointer to a function that takes and returns a char */
typedef char (*translate_t)(char);

/* transliterate: replaces each char in str by calling translate */
void transliterate(char* str, translate_t translate);

char my_trans(char a)
{
    if (a == 'x')
        return 'u';
    if (a == 'u')
        return 'x';
    return a;
}

void transliterate(char* str, translate_t translate)
{
    while (*str) {
        *str = translate(*str);
        str++;
    }
}

int main()
{
    char msg[] = "Hello world! (xu)";
    printf("%s\n", msg);
    transliterate(msg, my_trans);
    printf("%s\n", msg);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

MyLatestUpdate is turned into 1u by the preprocessor. You try to compile

extern FUNC (void, ) 1u()(void);

Which is complete nonsense.

3 Comments

How can I pass a function instead of 1u ?
1/ Write a function that isn't a random assembly of preprocessor garbage, but just a function. 2/ Write a new question which explains what you want to pass that function to, and what you're trying to achieve.
@user2986042 When calling a function which takes a function pointer as argument, you pass the name of the function, see my answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.