user10533142
user10533142

Reputation:

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

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 = %s\n", 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 = %s\n", 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?

Upvotes: 0

Views: 40

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320797

  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".

Upvotes: 1

Related Questions