Abhijatya Singh
Abhijatya Singh

Reputation: 642

unexpected initialization of global variable

I have the header file as:

test.h

//Mandatory fields size macro
#define size_uid   65
#define size_txn   33
#define size_adhaarNo 13
#define size_ver   4
#define size_input 65
#define size_photo 1024*1024*10
#define size_pseudonym 50
#define size_name 50

//Mandatory fields declaration
char uid[size_uid];
char txn[size_txn];
char adhaarNo[size_adhaarNo];
char ver [size_ver];
char *photo;
char pseudonym[size_pseudonym];
char name[size_name];
char input[size_input];
void incorrect_val_test(FILE *, FILE *, FILE *, FILE *, FILE *, FILE *,FILE *,FILE *,FILE *,FILE*);

test.c

#include "test.h"

//Mandatory fields declaration
char uid[] = "865A80A01C70A9E0D5FC5F4D354A9155BF58CD483B1397C92614E5BC92317ACC";
char txn[] = "23da7b99-c561-4102-9df8-d37fbfe1";
char adhaarNo[] = "250241800087";
char ver [] = "1.0";
char *photo = (char *)malloc(sizeof(char)*size_photo);
char pseudonym[] = "2b6c55566d14459991513fb00bce34ed";
char name[] = "Amarjeet Sharma";
char input[] = "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af";

void incorrect_val_test(FILE *ver, FILE *ts, FILE *txn, FILE *vtxn, FILE *uid, FILE *input, FILE *adhaarNo, FILE *photo, FILE *name, FILE *pseudonym)
{
       //Some initialization and files are opened here....

    photo = (char *)malloc(sizeof(char)*size_photo);

    FILE *photos = fopen("./input/data.txt","r");

    if(photos == NULL)
    {
        printf("Can't open data file.\n");
        return;
    }

    i=0;

    while((read_char = fgetc(photos))!= EOF)
    {
        if(read_char == '\n')
        continue;

        photo[i]= read_char;
        i++;    
    }


// Some more processing done here
}

main.c

int main()
{

      // Some files are opened here to read data.
      incorrect_val_test(fp_ver, fp_ts, fp_txn, fp_vtxn, fp_uid, fp_input, fp_adhaarNo, fp_photo, fp_name, fp_pseudonym);

       return 0;
}

When I am compiling it gives the following error message

error message

warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
photo = (char *)malloc(sizeof(char)*size_photo);
error: incompatible types when assigning to type ‘FILE {aka struct _IO_FILE}’ from type ‘char’
photo[i]= read_char;

Can anybody point me where I am going wrong?

Upvotes: 0

Views: 149

Answers (2)

Sourav Ghosh
Sourav Ghosh

Reputation: 134346

You can have a function call like

 char *photo = (char *)malloc(sizeof(char)*size_photo);

inside a function. This is not allowed in global scope. Remove the global one, leave the one present inside the incorrect_val_test() function.

Moreover, please do not cast the return value of malloc() and family in C.

Solution:

  1. define char *photo = NULL; in global scope.
  2. As you've done, allocate memory to photo inside your incorrect_val_test(), like

    char *photo = malloc(size_photo); //cast not required
                                      // sizeof(char) is guranteed to be 1
    

Then , you've got FILE *photo in your incorrect_val_test() function in the function parameter list

void incorrect_val_test(..., FILE *photo, ...)

which shadows the global char * photo.

IMHO, what you want is to write

 void incorrect_val_test(..., FILE *photos, ...)
                                         ^
                                         |

and, inside the function body,

 photos = fopen("./input/data.txt","r");

but, then, why to use variables names with a single character difference? Use something more verbose and easy to distinguish.

Note: After you're done using photo, don't forget to free() the allocated memory to avoid memory-leak.

Upvotes: 4

Some programmer dude
Some programmer dude

Reputation: 409206

You have the function

void incorrect_val_test(..., FILE *photo, ...)

Notice that you have an argument that shadows the global variable. That's the cause of your errors in the question.

Upvotes: 3

Related Questions