`(void*)` usage used as function parameter instead of `struct *`












0














Is there any big difference if I use something like this:



void print_struct( void *ptr_to_struct )


instead of:



void print_struct( struct data *ptr_to_struct )


I am asking this because I just got stuck in something what I cannot understand and why it does not work in the following manner:



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

void print_struct( struct data *ptr_to_struct );
struct data{
char name[ 256 ];
};

int main( void )
{
struct data ptr;
strcpy ( ptr.name, "George" );
print_struct_1( &ptr);
}

void print_struct( struct data *ptr_to_struct )
{
struct data *ptr = ptr_to_struct;
printf("Name = %sn", ptr->name );
}


I get:



error: ‘struct data’ declared inside parameter list will not be visible outside of this definition or declaration [-Werror]


But if I move:



void print_struct( struct data *ptr_to_struct );


After:



struct data{
char name[ 256 ];
};


Compiles fine.



The thing which I do not understand is the following:



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

void print_struct( void *ptr_to_struct );
struct data{
char name[ 256 ];
};
//void print_struct( void *ptr_to_struct );


int main( void )
{
struct data ptr;
strcpy ( ptr.name, "George" );
print_struct_1( &ptr);
}

void print_struct( void *ptr_to_struct )
{
struct data *ptr = ( struct data * )ptr_to_struct;
printf("Name = %sn", ptr->name );
}


No mater if I use:



void print_struct( void *ptr_to_struct );


Before or after:



struct data{
char name[ 256 ];
};


The program works fine.



Why is there this difference?










share|improve this question



























    0














    Is there any big difference if I use something like this:



    void print_struct( void *ptr_to_struct )


    instead of:



    void print_struct( struct data *ptr_to_struct )


    I am asking this because I just got stuck in something what I cannot understand and why it does not work in the following manner:



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

    void print_struct( struct data *ptr_to_struct );
    struct data{
    char name[ 256 ];
    };

    int main( void )
    {
    struct data ptr;
    strcpy ( ptr.name, "George" );
    print_struct_1( &ptr);
    }

    void print_struct( struct data *ptr_to_struct )
    {
    struct data *ptr = ptr_to_struct;
    printf("Name = %sn", ptr->name );
    }


    I get:



    error: ‘struct data’ declared inside parameter list will not be visible outside of this definition or declaration [-Werror]


    But if I move:



    void print_struct( struct data *ptr_to_struct );


    After:



    struct data{
    char name[ 256 ];
    };


    Compiles fine.



    The thing which I do not understand is the following:



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

    void print_struct( void *ptr_to_struct );
    struct data{
    char name[ 256 ];
    };
    //void print_struct( void *ptr_to_struct );


    int main( void )
    {
    struct data ptr;
    strcpy ( ptr.name, "George" );
    print_struct_1( &ptr);
    }

    void print_struct( void *ptr_to_struct )
    {
    struct data *ptr = ( struct data * )ptr_to_struct;
    printf("Name = %sn", ptr->name );
    }


    No mater if I use:



    void print_struct( void *ptr_to_struct );


    Before or after:



    struct data{
    char name[ 256 ];
    };


    The program works fine.



    Why is there this difference?










    share|improve this question

























      0












      0








      0







      Is there any big difference if I use something like this:



      void print_struct( void *ptr_to_struct )


      instead of:



      void print_struct( struct data *ptr_to_struct )


      I am asking this because I just got stuck in something what I cannot understand and why it does not work in the following manner:



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

      void print_struct( struct data *ptr_to_struct );
      struct data{
      char name[ 256 ];
      };

      int main( void )
      {
      struct data ptr;
      strcpy ( ptr.name, "George" );
      print_struct_1( &ptr);
      }

      void print_struct( struct data *ptr_to_struct )
      {
      struct data *ptr = ptr_to_struct;
      printf("Name = %sn", ptr->name );
      }


      I get:



      error: ‘struct data’ declared inside parameter list will not be visible outside of this definition or declaration [-Werror]


      But if I move:



      void print_struct( struct data *ptr_to_struct );


      After:



      struct data{
      char name[ 256 ];
      };


      Compiles fine.



      The thing which I do not understand is the following:



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

      void print_struct( void *ptr_to_struct );
      struct data{
      char name[ 256 ];
      };
      //void print_struct( void *ptr_to_struct );


      int main( void )
      {
      struct data ptr;
      strcpy ( ptr.name, "George" );
      print_struct_1( &ptr);
      }

      void print_struct( void *ptr_to_struct )
      {
      struct data *ptr = ( struct data * )ptr_to_struct;
      printf("Name = %sn", ptr->name );
      }


      No mater if I use:



      void print_struct( void *ptr_to_struct );


      Before or after:



      struct data{
      char name[ 256 ];
      };


      The program works fine.



      Why is there this difference?










      share|improve this question













      Is there any big difference if I use something like this:



      void print_struct( void *ptr_to_struct )


      instead of:



      void print_struct( struct data *ptr_to_struct )


      I am asking this because I just got stuck in something what I cannot understand and why it does not work in the following manner:



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

      void print_struct( struct data *ptr_to_struct );
      struct data{
      char name[ 256 ];
      };

      int main( void )
      {
      struct data ptr;
      strcpy ( ptr.name, "George" );
      print_struct_1( &ptr);
      }

      void print_struct( struct data *ptr_to_struct )
      {
      struct data *ptr = ptr_to_struct;
      printf("Name = %sn", ptr->name );
      }


      I get:



      error: ‘struct data’ declared inside parameter list will not be visible outside of this definition or declaration [-Werror]


      But if I move:



      void print_struct( struct data *ptr_to_struct );


      After:



      struct data{
      char name[ 256 ];
      };


      Compiles fine.



      The thing which I do not understand is the following:



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

      void print_struct( void *ptr_to_struct );
      struct data{
      char name[ 256 ];
      };
      //void print_struct( void *ptr_to_struct );


      int main( void )
      {
      struct data ptr;
      strcpy ( ptr.name, "George" );
      print_struct_1( &ptr);
      }

      void print_struct( void *ptr_to_struct )
      {
      struct data *ptr = ( struct data * )ptr_to_struct;
      printf("Name = %sn", ptr->name );
      }


      No mater if I use:



      void print_struct( void *ptr_to_struct );


      Before or after:



      struct data{
      char name[ 256 ];
      };


      The program works fine.



      Why is there this difference?







      c






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 19:16









      Michael B.

      1288




      1288
























          1 Answer
          1






          active

          oldest

          votes


















          2
















          1. When you refer to an unknown struct something type in a function prototype, it effectively declares a new struct type. However, the new type will be local to that prototype. The new type will not be related to any identically names struct something type declared afterwards. For example



            void foo(struct S *p) {}
            struct S { int i; };

            int main(void) {
            struct S s;
            foo(&s); // ERROR: the pointer types are unrelated
            }


            Basically, don't do that. Don't use unknown struct something types in function prototypes. It is pointless. It is virtually always an indication of a typo or some other error. This is why the compiler is issuing a warning.



            Such struct types have to be declared before the prototype. You can rearrange your declarations (as you already tried). Or you can pre-declare the struct type



            struct data;
            void print_struct( struct data *ptr_to_struct );
            struct data{
            char name[ 256 ];
            };


            This will also work properly.



          2. Declaring a function prototype with a parameter type void * and then defining the same function with parameter of struct something * type leads to undefined behavior, even if your program appears to "work fine".







          share|improve this answer























          • So If I use it Like this it is fine, or can lead to UB ?
            – Michael B.
            Nov 20 at 19:27












          • @Michael B.: What you have at that link looks fine. Some const qualifiers are completely unjustified though.
            – AnT
            Nov 20 at 19:43












          • Some const qualifiers are completely unjustified Why is that? I mean The purpose there is to tell the Compiler that I am not touching that pointer or its pointed value. Or I am wrong?
            – Michael B.
            Nov 20 at 19:48










          • @Michael B: Well, in that case make it consistent: inside your print_struct declare ptr as const struct data *ptr. That will also make the cast unnecessary. Currently you use that cast to cast some of your consts away. What's the point of using const if you are going to immediately cast it away anyway?
            – AnT
            Nov 20 at 19:59












          • I understand now. Thank you.
            – Michael B.
            Nov 20 at 20:43











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400023%2fvoid-usage-used-as-function-parameter-instead-of-struct%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2
















          1. When you refer to an unknown struct something type in a function prototype, it effectively declares a new struct type. However, the new type will be local to that prototype. The new type will not be related to any identically names struct something type declared afterwards. For example



            void foo(struct S *p) {}
            struct S { int i; };

            int main(void) {
            struct S s;
            foo(&s); // ERROR: the pointer types are unrelated
            }


            Basically, don't do that. Don't use unknown struct something types in function prototypes. It is pointless. It is virtually always an indication of a typo or some other error. This is why the compiler is issuing a warning.



            Such struct types have to be declared before the prototype. You can rearrange your declarations (as you already tried). Or you can pre-declare the struct type



            struct data;
            void print_struct( struct data *ptr_to_struct );
            struct data{
            char name[ 256 ];
            };


            This will also work properly.



          2. Declaring a function prototype with a parameter type void * and then defining the same function with parameter of struct something * type leads to undefined behavior, even if your program appears to "work fine".







          share|improve this answer























          • So If I use it Like this it is fine, or can lead to UB ?
            – Michael B.
            Nov 20 at 19:27












          • @Michael B.: What you have at that link looks fine. Some const qualifiers are completely unjustified though.
            – AnT
            Nov 20 at 19:43












          • Some const qualifiers are completely unjustified Why is that? I mean The purpose there is to tell the Compiler that I am not touching that pointer or its pointed value. Or I am wrong?
            – Michael B.
            Nov 20 at 19:48










          • @Michael B: Well, in that case make it consistent: inside your print_struct declare ptr as const struct data *ptr. That will also make the cast unnecessary. Currently you use that cast to cast some of your consts away. What's the point of using const if you are going to immediately cast it away anyway?
            – AnT
            Nov 20 at 19:59












          • I understand now. Thank you.
            – Michael B.
            Nov 20 at 20:43
















          2
















          1. When you refer to an unknown struct something type in a function prototype, it effectively declares a new struct type. However, the new type will be local to that prototype. The new type will not be related to any identically names struct something type declared afterwards. For example



            void foo(struct S *p) {}
            struct S { int i; };

            int main(void) {
            struct S s;
            foo(&s); // ERROR: the pointer types are unrelated
            }


            Basically, don't do that. Don't use unknown struct something types in function prototypes. It is pointless. It is virtually always an indication of a typo or some other error. This is why the compiler is issuing a warning.



            Such struct types have to be declared before the prototype. You can rearrange your declarations (as you already tried). Or you can pre-declare the struct type



            struct data;
            void print_struct( struct data *ptr_to_struct );
            struct data{
            char name[ 256 ];
            };


            This will also work properly.



          2. Declaring a function prototype with a parameter type void * and then defining the same function with parameter of struct something * type leads to undefined behavior, even if your program appears to "work fine".







          share|improve this answer























          • So If I use it Like this it is fine, or can lead to UB ?
            – Michael B.
            Nov 20 at 19:27












          • @Michael B.: What you have at that link looks fine. Some const qualifiers are completely unjustified though.
            – AnT
            Nov 20 at 19:43












          • Some const qualifiers are completely unjustified Why is that? I mean The purpose there is to tell the Compiler that I am not touching that pointer or its pointed value. Or I am wrong?
            – Michael B.
            Nov 20 at 19:48










          • @Michael B: Well, in that case make it consistent: inside your print_struct declare ptr as const struct data *ptr. That will also make the cast unnecessary. Currently you use that cast to cast some of your consts away. What's the point of using const if you are going to immediately cast it away anyway?
            – AnT
            Nov 20 at 19:59












          • I understand now. Thank you.
            – Michael B.
            Nov 20 at 20:43














          2












          2








          2








          1. When you refer to an unknown struct something type in a function prototype, it effectively declares a new struct type. However, the new type will be local to that prototype. The new type will not be related to any identically names struct something type declared afterwards. For example



            void foo(struct S *p) {}
            struct S { int i; };

            int main(void) {
            struct S s;
            foo(&s); // ERROR: the pointer types are unrelated
            }


            Basically, don't do that. Don't use unknown struct something types in function prototypes. It is pointless. It is virtually always an indication of a typo or some other error. This is why the compiler is issuing a warning.



            Such struct types have to be declared before the prototype. You can rearrange your declarations (as you already tried). Or you can pre-declare the struct type



            struct data;
            void print_struct( struct data *ptr_to_struct );
            struct data{
            char name[ 256 ];
            };


            This will also work properly.



          2. Declaring a function prototype with a parameter type void * and then defining the same function with parameter of struct something * type leads to undefined behavior, even if your program appears to "work fine".







          share|improve this answer
















          1. When you refer to an unknown struct something type in a function prototype, it effectively declares a new struct type. However, the new type will be local to that prototype. The new type will not be related to any identically names struct something type declared afterwards. For example



            void foo(struct S *p) {}
            struct S { int i; };

            int main(void) {
            struct S s;
            foo(&s); // ERROR: the pointer types are unrelated
            }


            Basically, don't do that. Don't use unknown struct something types in function prototypes. It is pointless. It is virtually always an indication of a typo or some other error. This is why the compiler is issuing a warning.



            Such struct types have to be declared before the prototype. You can rearrange your declarations (as you already tried). Or you can pre-declare the struct type



            struct data;
            void print_struct( struct data *ptr_to_struct );
            struct data{
            char name[ 256 ];
            };


            This will also work properly.



          2. Declaring a function prototype with a parameter type void * and then defining the same function with parameter of struct something * type leads to undefined behavior, even if your program appears to "work fine".








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 19:25

























          answered Nov 20 at 19:20









          AnT

          257k32409654




          257k32409654












          • So If I use it Like this it is fine, or can lead to UB ?
            – Michael B.
            Nov 20 at 19:27












          • @Michael B.: What you have at that link looks fine. Some const qualifiers are completely unjustified though.
            – AnT
            Nov 20 at 19:43












          • Some const qualifiers are completely unjustified Why is that? I mean The purpose there is to tell the Compiler that I am not touching that pointer or its pointed value. Or I am wrong?
            – Michael B.
            Nov 20 at 19:48










          • @Michael B: Well, in that case make it consistent: inside your print_struct declare ptr as const struct data *ptr. That will also make the cast unnecessary. Currently you use that cast to cast some of your consts away. What's the point of using const if you are going to immediately cast it away anyway?
            – AnT
            Nov 20 at 19:59












          • I understand now. Thank you.
            – Michael B.
            Nov 20 at 20:43


















          • So If I use it Like this it is fine, or can lead to UB ?
            – Michael B.
            Nov 20 at 19:27












          • @Michael B.: What you have at that link looks fine. Some const qualifiers are completely unjustified though.
            – AnT
            Nov 20 at 19:43












          • Some const qualifiers are completely unjustified Why is that? I mean The purpose there is to tell the Compiler that I am not touching that pointer or its pointed value. Or I am wrong?
            – Michael B.
            Nov 20 at 19:48










          • @Michael B: Well, in that case make it consistent: inside your print_struct declare ptr as const struct data *ptr. That will also make the cast unnecessary. Currently you use that cast to cast some of your consts away. What's the point of using const if you are going to immediately cast it away anyway?
            – AnT
            Nov 20 at 19:59












          • I understand now. Thank you.
            – Michael B.
            Nov 20 at 20:43
















          So If I use it Like this it is fine, or can lead to UB ?
          – Michael B.
          Nov 20 at 19:27






          So If I use it Like this it is fine, or can lead to UB ?
          – Michael B.
          Nov 20 at 19:27














          @Michael B.: What you have at that link looks fine. Some const qualifiers are completely unjustified though.
          – AnT
          Nov 20 at 19:43






          @Michael B.: What you have at that link looks fine. Some const qualifiers are completely unjustified though.
          – AnT
          Nov 20 at 19:43














          Some const qualifiers are completely unjustified Why is that? I mean The purpose there is to tell the Compiler that I am not touching that pointer or its pointed value. Or I am wrong?
          – Michael B.
          Nov 20 at 19:48




          Some const qualifiers are completely unjustified Why is that? I mean The purpose there is to tell the Compiler that I am not touching that pointer or its pointed value. Or I am wrong?
          – Michael B.
          Nov 20 at 19:48












          @Michael B: Well, in that case make it consistent: inside your print_struct declare ptr as const struct data *ptr. That will also make the cast unnecessary. Currently you use that cast to cast some of your consts away. What's the point of using const if you are going to immediately cast it away anyway?
          – AnT
          Nov 20 at 19:59






          @Michael B: Well, in that case make it consistent: inside your print_struct declare ptr as const struct data *ptr. That will also make the cast unnecessary. Currently you use that cast to cast some of your consts away. What's the point of using const if you are going to immediately cast it away anyway?
          – AnT
          Nov 20 at 19:59














          I understand now. Thank you.
          – Michael B.
          Nov 20 at 20:43




          I understand now. Thank you.
          – Michael B.
          Nov 20 at 20:43


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400023%2fvoid-usage-used-as-function-parameter-instead-of-struct%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python