7

This code is just to test my knowledge on structure array access. When I executed this code it gives me the error two many initializes for param. Please help me to understand the error and fixing this problem. I was trying to reuse the code which is already solved by someone. My question about fill the Struct with parameters Param_u param

#include <iostream>
#include <stdio.h>
#include <string.h>


#define ARRAY_COUNT(arr) (sizeof (arr) / sizeof *(arr))

typedef union {
    struct {    // Function parameters
        int *array;
        size_t size;
    };
    struct {    // Function return value
        float mean;
        int Median;
    };
} Param_u;

int main() {
    int array_1[] = {1, 2, 3, 4, 5};
    int  ret1, ret2;

    // Fill the Struct with parameters
    Param_u param = {
        .array = array_1,
        .size = ARRAY_COUNT(array_1),
    };
    return 0;
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138

1 Answers1

0

This is not standard C++. You are using anonymous struct & designated initializers (C99 feature). C++ doesn't support that. Enable -pedantic-errors option on clang++ & g++. See this question for more details. You are using compiler specific extensions so your program is not portable.

See live demo here.

clang++ gives following diagnostics:

Error(s):

source_file.cpp:12:5: error: anonymous structs are a GNU extension [-Werror,-Wgnu-anonymous-struct]
    struct {    // Function parameters
    ^
source_file.cpp:16:5: error: anonymous structs are a GNU extension [-Werror,-Wgnu-anonymous-struct]
    struct {    // Function return value
    ^
source_file.cpp:28:9: error: designated initializers are a C99 feature [-Werror,-Wc99-extensions]
        .array = array_1,
        ^~~~~~~~~~~~~~~~
source_file.cpp:29:9: error: designated initializers are a C99 feature [-Werror,-Wc99-extensions]
        .size = ARRAY_COUNT(array_1),
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
source_file.cpp:24:16: warning: unused variable 'ret2' [-Wunused-variable]
    int  ret1, ret2;
               ^
source_file.cpp:27:13: warning: unused variable 'param' [-Wunused-variable]
    Param_u param = {
            ^
source_file.cpp:24:10: warning: unused variable 'ret1' [-Wunused-variable]
    int  ret1, ret2;
         ^
3 warnings and 4 errors generated.
Community
  • 1
  • 1
Destructor
  • 14,123
  • 11
  • 61
  • 126