4

I was just wondering if a variable declared and defined inside a structure can be initialized to a certain value, was planning on using function pointers to mimic the classes in OOP.

Example COde:

typedef struct{
int x;
int (*manipulateX)(int) = &manipulateX;
}x = {0};

void main()
{
    getch();
}

int manipulateX(int x)
{
    x = x + 1;
return x;
}
lemoncodes
  • 2,371
  • 11
  • 40
  • 66
  • 1
    Take a look at http://stackoverflow.com/questions/225089/why-cant-we-initialize-members-inside-a-structure – cjm Jan 06 '13 at 01:06
  • 1
    i don't understand this part in your link "The direct answer is because the structure definition declares a type and not a variable that can be initialized. Your example is: struct s { int i=10; }; This does not declare any variable - it defines a type. To declare a variable, you would add a name between the } and the ;, and then you would initialize it afterwards: struct s { int i; } t = { 10 };" – lemoncodes Jan 06 '13 at 01:12
  • specifically the one that says " To declare a variable, you would add a name between the } and the ;, and then you would initialize it afterwards: struct s { int i; } t = { 10 };" – lemoncodes Jan 06 '13 at 01:16
  • Take a look at http://stackoverflow.com/questions/5465896/about-default-c-struct-values-what-about-this-code – pmg Jan 06 '13 at 01:19
  • Please see my edited post, added some sample codes – lemoncodes Jan 06 '13 at 01:19
  • (Also, for mimicking "object oriented" objects in C, see [GObject](http://en.wikipedia.org/wiki/GObject) and similar libraries.) –  Jan 06 '13 at 01:20

4 Answers4

9

Starting with C99, you can use designated initializers to set fields of structures to values, as follows:

struct MyStruct {
    int x;
    float f;
};

void test() {
    struct MyStruct s = {.x=123, .f=456.789};
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • no i mean i want to initialize the members without making any functions to initialize its members – lemoncodes Jan 06 '13 at 01:13
  • @lemoncodes There is no dynamic initialization facilities in C. You need C++ for that :( – Sergey Kalinichenko Jan 06 '13 at 01:16
  • That's what C++ is for - it allows you to have functions in classes and structures. In C, you can't associate functions with classes without writing code to intialize the structure (of course, you can do the same ting as in C++, use a "vtable", which means just one initialization per struct-type, to a constant array of functions) – Mats Petersson Jan 06 '13 at 01:16
  • hmmmm so there is no way to completely mimic the classes in OOP without making any additional function to initialize the members of the structures in C? i mean strictly speaking about C not C++ – lemoncodes Jan 06 '13 at 01:22
  • 1
    @lemoncodes Correct, plain C does not offer a way to execute initialization code without a function. – Sergey Kalinichenko Jan 06 '13 at 01:30
0
StructName s; // receives no initialization
StructName s = {x,y}; // value initializes all members 

struct Child { StructName s; };
Child c; // receives no initialization
Child c = {x,y}; // value initializes all members

For Example :

struct Child{

int c;
int d;

}

Child childstruct ={10,20};

then childstruct.c will be 10 and childstruct.d will be 20.

http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc03strin.htm

Rohit Vincent
  • 66
  • 1
  • 1
  • 6
  • i already know this part, what i want to do is initialize the members inside the structure. Just like classes in OOP but im implementing this on C not C++ – lemoncodes Jan 06 '13 at 01:15
0

One approach which I've used when programming an embedded system where memory was somewhat tight was to use some macros to both define a structure type and define its initial value. The definition looked something like:

#define CREATE_FOO_STRUCT \
  X(int age, 34) Y \
  X(int height, 65) Y \
  X(char const * favoritecolor,"blue") Y \
  X(int some_things[5],ARR5(1,2,3,4,5))

The ARR5 macro was needed to allow the initialization values for that array to be passed as a single parameter; an alternative would have been to write the last line as X(int some_things[5],{1 COMMA 2 COMMA 3 COMMA 4 COMMA5}). Given a macro like the above, it is possible to define X and Y so X returns expands to first item and Y to semicolon, in which case typedef {CREATE_FOO_STRUCT} FOO; will define structure FOO, and then redefine X and Y so that X returns the second item and Y a comma, in which case const FOO default_FOO = {CREATE_FOO_STRUCT}; will define a default instance. It's too bad I don't know any way to avoid the need to have either the last line differ from the others (the last line could match the others if each line but the first was preceded by X, and if the last line was followed by a //this line must be left blank line.

On the particular processor where I implemented the above constructs, defining default_FOO in that way would consume zero RAM, and consume ROM equal to the size of default_FOO. A function which would load a static instance of FOO with the default values, using conventional assignment statement, would consume four bytes of ROM for each non-zero byte in default_FOO, and would also require that there be a static instance it could use. A C-language function which would accept a FOO* and load the pointed-to instance with default values would take a monstrous amount of code (each assignment would have to separately compute the address of the data to be stored).

supercat
  • 77,689
  • 9
  • 166
  • 211
0

it is illegal to initialize a structure member inside the structure template in C, and is not permitted.. as a struct template doesnot hold any memory of its own, and all the members of struct are allocated a memory space only when they are associated with a struct type of variable, and since now they are allocated a memory they can be initialized.. but not before getting associated with a variable: e.g

    struct tag_name
    { 
    int x;
    int y;
    };

the above struct template doesnot hold a memory space.. unless a variable is associated to it as:

    struct tag_name variable1;

now the struct type of variable variable1 will be allocated a memory which is large enough to hold two int values(for variable1.x and variable1. y) and now only you can initialize the members x and y as e.g.:

    variable1.x=10;
    variable1.y=20;

and hence the following would be illegal

    struct tag_name
    { 
    int x=10;
    int y=20;
    };
r_goyal
  • 1,117
  • 1
  • 11
  • 20