Reputation:
Is the following syntax correct for returning a pointer from a function?
bmpfile_t*
bmp_create_from_file(const char *filename, bmpfile_t *bmp)
{
FILE *fp;
fp = fopen(filename, "r");
bmp_get_header_from_file(fp, bmp);
bmp_get_dib_from_file(fp, bmp);
bmp_get_pixels_from_file(fp, bmp);
fclose(fp);
return &bmp;
}
Upvotes: 0
Views: 160
Reputation: 5067
bmp
already has type bmpfile_t *
, which is what you want to return. So just return bmp
.
Edit: What you are currently trying to return, &bmp
, is the address of a bmpfile_t *
, and hence has type bmpfile_t **
, a pointer to a pointer. Furthermore, as others have pointed out, &bmp
would be the address of bmp
, which is a local stack variable in your function. This will be popped when you return from the function, and hence that pointer will point at something useless. So not only are you returning something with the wrong type, you are also returning something dangerous. So just return bmp
.
Upvotes: 4