Thread: gcc compiler warning

  1. #1
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67

    gcc compiler warning

    I wrote a wrapper function to either read or write to a file stream based on the 'flag' argument passed to the function:

    Code:
    FILE *openfile(char *filename, char flag) {
    	FILE *fin = NULL, *fout = NULL;
    	
    	if (flag == 'd') {
    		fin = fopen(filename, "r");
    		if (fin == NULL) {
    			fprintf(stderr, "Error: Unable to open %s\n", filename);
    			exit(EXIT_FAILURE);
    		}
    		
    		return fin;
    	}
    	else if (flag == 'e') {
    		fout = fopen(filename, "w");
    		if (fout == NULL) {
    			fprintf(stderr, "Error: Unable to create %s\n", filename);
    			exit(EXIT_FAILURE);
    		}
    		
    		return fout;
    	}
    }
    When I compile the program, I am getting a warning:

    dna1.c:150: warning: control reaches end of non-void function

    I understand the warning. It's being thrown because it doesn't think I'm returning a file pointer. The thing is I am returning a file pointer based on a condition. Is this normal? The program works fine....but I was wondering if there is anything I could do with the code to get rid of the warning.

    Thanks!

  2. #2
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    What does openfile return if flag is not 'd' or 'e'?

  3. #3
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    I programmed it statically so the calling function always passes either 'd' or 'e'.

    Examples:
    Code:
    fin = openfile(filename, 'd');
    fout = openfile(filename, 'e');
    EDIT: I updated the function to return NULL if the flag isn't 'd' or 'e'.

    Code:
    FILE *openfile(char *filename, char flag) {
    	FILE *fin = NULL, *fout = NULL;
    	
    	if (flag == 'd') {
    		fin = fopen(filename, "r");
    		if (fin == NULL) {
    			fprintf(stderr, "Error: Unable to open %s\n", filename);
    			exit(EXIT_FAILURE);
    		}
    		
    		return fin;
    	}
    	else if (flag == 'e') {
    		fout = fopen(filename, "w");
    		if (fout == NULL) {
    			fprintf(stderr, "Error: Unable to create %s\n", filename);
    			exit(EXIT_FAILURE);
    		}
    		
    		return fout;
    	}
    	else
    		return NULL;
    }
    This wouldn't happen, but it does seem to have fixed the warning. Is this the only way to get rid of the warning?
    Last edited by matrixx333; 07-14-2010 at 04:42 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It's because if flag is neither d nor e, your function is not returning anything as it should.


    Code:
    FILE *openfile(char *filename, char flag) {
    	FILE *fin = NULL, *fout = NULL;
    	
    	if (flag == 'd') {
    	     ...
    		return fin;
    	}
    	else if (flag == 'e') {
    	    ...
    		return fout;
    	}
            return NULL;             // invalid option return NULL
          // or use else return NULL;
    }

  5. #5
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    Thanks Bayint Naung,

    I edited my previous post at the same time you replied. That seems to have done it. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about a compiler warning
    By sashaKap in forum C Programming
    Replies: 4
    Last Post: 03-17-2009, 09:47 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM