Thursday 28 December 2017

Function definition in C struct?

#include <stdio.h>
#include <conio.h>

struct temp
{
  int a;
  void get_data()
  {
    printf("Enter value of a : ");
    scanf("%d",&a);
  }
}T;

int main()
{
   T.get_data();
}
This works fine in C++ i.e test.cpp (g++ compiler) , but fails in C i.e test.c(gcc compiler) showing error:

8:field `get_data' declared as a function 
8:[Warning] no semicolon at end of struct or union

examples:

Below code is not valid C code (i.e. valid C11) code but it is valid C++ (i.e. C++14) code.

typedef struct someStruct {
    int i; 
    char c; 
    someStruct() {
        i = 0;
        c = 'c';
    }
    someStruct(char inpChar) {
        i = 1;
        c = inpChar;
    }
} t_someStruct;

No comments:

Post a Comment