Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I have a doubt, basically i have inlined functions and while calling them, i was not able to call them from the same source file. So i moved them to a header file.

Is that the same case with structures. We were planning to inline a structure. So is it possible to do so? or is it a good practice?

I would request any suggestion or help.


Thanks and Regards,
Rahul

What I have tried:

I tried the below code:

#include <stdio.h>

inline struct{
    int m;
    int j;
}book;

int main()
{
    printf("Hello World");
    int a = 0;

    return 0;
}


OUTPUT : Warning, the struct has been declared inline.
Posted
Updated 6-Feb-21 21:25pm

1 solution

What on earth use do you think that an inline struct would be? What difference would it make?

When you declare a function as inline you are asking teh compiler nicely to consider replacing calls to the function with the code directly:
C++
inline int foo(int bar) { return bar + 1; }
...
   x = foo(y);
becomes the same thing as writing this:
C++
x = y + 1;
and it's used for performance reasons to try to avoid the stacking of parameters, the machine calls subroutine call, the unstacking, and the machine code return from subroutine - and it can make a big difference in a tight loop.

But a struct doesn't - and can't - contain any code. It allocates memory space, and that only when a variable is declared. So what possible use would trying to inline it be?

And that's what the compiler is telling you: "This is a pointless thing to do, so I'm going to ignore it". That's why it's a warning rather than an error: it has no effect on the eventual program, but it's something you should remove!
 
Share this answer
 
Comments
Rahul VB 7-Feb-21 4:06am    
Hello OG,

Thanks for the answer, i was just playing arround with some stuff, so thought of asking.

Thanks,
Rahul

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900