0

there is a line in c code:

struct name_1 name2;

what this line implies?

my understanding about above line is name_1 is the name of the structure and name2 is the object variable. However name_1 should have been defined somewhere, but I could not find the definition of name_1.

so my question is; is there anything like this where we can have an object of a structure which is not defined anywhere.

rama
  • 43
  • 2
  • 6
  • 2
    Perhaps `name_1` is defined in a system header file, or a header file not directly in your project? – Some programmer dude Jun 14 '18 at 04:30
  • Note that you could have a definition `struct SomeThing *sommat;` without a definition of the content of `struct SomeThing` in the file — you can have pointers to incomplete types. However, you cannot define actual variables as in your question — `struct name_1 name_2;` requires a definition of what's in `struct name_1`. – Jonathan Leffler Jun 14 '18 at 04:40
  • Also compare with [Which part of the C standard allows this to compile?](https://stackoverflow.com/questions/12200096/which-part-of-the-c-standard-allows-this-code-to-compile) – Jonathan Leffler Jun 14 '18 at 04:51

2 Answers2

2

At file scope, this is the definition of a variable called name2 whose type is struct name_1. If there has not been a previous declaration of struct name_1; then this line also declares that type.

Being a file scope variable definition with no initializer nor storage class specifier, this is a tentative definition. Tentative definitions may have incomplete type so long as the type is completed by the end of the file, e.g.:

#include <stdio.h>

struct foo bar;

void f();

int main()
{
    printf("%p\n", (void *)&bar);
    f();

    // cannot do this
    // printf("%d\n", bar.x);
}

struct foo { int x; };

void f()
{
    bar.x = 5;
}

This sort of code would be uncommon however. If you see struct foo bar; in real code it is more likely that struct foo was previously defined somewhere that you are overlooking.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • `struct foo bar; void f() { struct foo foo_1; bar = foo_1; }` what this implies? I could not find the definition of foo anywhere. – rama Jun 14 '18 at 05:22
  • @rama that code would only work if `struct foo` is defined elsewhere. If the compiler accepts the code it proves there is a definition somwhere. If you are using an IDE to code in then it may be able to find the definition for you. – M.M Jun 14 '18 at 05:24
  • @M.M can tentative definitions be there at the local scope? – Ajay Brahmakshatriya Jun 14 '18 at 05:46
  • ok , I got it , it has been defined somewhere , perhaps I am not able to find . – rama Jun 14 '18 at 05:46
  • @AjayBrahmakshatriya no, they can only be at file scope – M.M Jun 14 '18 at 07:56
0

Is there a header file (name.h) you have included in your c code? Usually, structure, constant and function declarations are in the header file, which is 'included' (at the top of your c file), and you could then use those structures and constants in your c file without defining them

Shaun Jose
  • 66
  • 5