Compiling Errors - return makes integer..., function returns address...

This is a discussion on Compiling Errors - return makes integer..., function returns address... within the C Programming forums, part of the General Programming Boards category; Hi, Im having trouble debugging my program. My program is suppose to read a txt list that consists of a ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    6

    Compiling Errors - return makes integer..., function returns address...

    Hi, Im having trouble debugging my program.
    My program is suppose to read a txt list that consists of a number followed by letters

    82
    fsdfkd
    0
    fdsfds

    I got that part to work thanks to some help, but now i got a problem when i try to call it into the main function

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 100
    #define SOURCE2 "pattern2.txt"
    #define SOURCE3 "fp2.txt"
    
    struct patterninput
    {
    	int mismatches;						
    	char thepattern[MAX];
    };
    
    int main(void)
    {
    	int  returns_mismatches(int array_num);
    	char returns_pattern(int array_num);
    	int array_num;
    
                 printf("enter the number for the array \n");
                 scanf(" %d ", &array_num);
    						
    	printf("mis: %d", returns_mismatches(array_num));
    	printf("pat: %s", returns_pattern(array_num));
    
    	return 0;
    }
    
    int  returns_mismatches(int array_num)
    {
    	struct patterninput lines[MAX];
    	char temp[MAX];						
    	char *p;
    	int k=0;
    	FILE *fp = fopen(SOURCE2, "r");
    	FILE *fp2 = fopen(SOURCE3, "w");
    	
    	while (fgets(temp, MAX, fp) != NULL)
    	{
    	   if ((p = strchr(temp, '\n')) != NULL)
    	      {
    						
    	      lines[k].mismatches = atoi(temp); 
    	      *p = '\0';
    
     	      if (fgets(lines[k].thepattern, MAX, fp ) != NULL )
     	      fprintf(fp2,"%d %s", lines[k].mismatches, 
                       lines [k].thepattern);
    	      }
    	   if(k == array_num)
    	   return(lines[k].mismatches);
    	   k++;
    	}					
    
    	fclose(fp);
    
    	fclose(fp2);
    	
    }	
    
    char returns_pattern(int array_num)
    {
    	struct patterninput lines[MAX];				
    	char temp[MAX];
    	char *p;
    	int k=0;
    	FILE *fp = fopen(SOURCE2, "r");
    	FILE *fp2 = fopen(SOURCE3, "w");
    	
    	while (fgets(temp, MAX, fp) != NULL)
    	{
    	   if ((p = strchr(temp, '\n')) != NULL)
    	      {				
    	
    	      lines[k].mismatches = atoi(temp); 
    	      *p = '\0';
    
     	      if (fgets(lines[k].thepattern, MAX, fp ) != NULL )
     	      fprintf(fp2,"%d %s", lines[k].mismatches, 
                       lines[k].thepattern);
    	      }
    	   if(k == array_num)
    	   return(lines[k].thepattern);
    	   k++;							
    	}
    
    	fclose(fp);
    
    	fclose(fp2);
    	
    }
    I know it looks crappy but i dont know how to call seperate elements from one function so im just going to do two seperate function with them returning one value each...

    If this program works, it should ask user for a number, and then print the corresponding number and pattern....

    the problem im getting is in the compiling again
    this line near the end:
    Code:
    return(lines[k].thepattern);
    gives me these compile errors
    warning: return makes integer from pointer without a cast
    warning: function returns address of local variable

    thanks for any help!

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    You have declared the "thepattern" member of "struct patterninput" as an array of chars ("char thepattern[MAX];") and the function "returns_pattern()" is declared as returning a char ("char returns_pattern(int array_num);"), so this is the first warning. You probably want do declare that function as "char *returns_pattern(int array_num);", to return a pointer to char.
    The second warning comes from "struct patterninput lines[MAX];" being local scope to the "returns_pattern()" function. You could "fix" that by declaring "static struct patterninput lines[MAX];", but your code has some other errors.
    You don't need an array of patterninput struct's if you end up by only using one (BTW, if the SOURCE2 file have more than MAX lines your program might act funny). Also, both functions "returns_mismatches()" and "returns_pattern()" are similar, so why don't you just return a pointer to that "struct patterninput"?
    You should also check the return value of "fopen()" functions.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    6

    Arrow



    thanks for giving me some tips, i tried some of those ways and now i get segmentation errors. I havent learned pointers yet so the pointer in my program i just copied from the FAQ.

    maybe theres another way to do this? any other ideas?
    i was thinking since the numbers are on odd lines, and the letters
    are on even lines, i could use some sort of count?

    oh and i was told that i wouldnt be given a SOURCE2 with more than 30-40 numbers along with patterns

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21