C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 6th, 2004, 10:02 PM
cproghelp cproghelp is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 4 cproghelp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Compiler Error - Incompatible types in assignment??

Hello, Im trying to make a program that groups a number and a string together from a text file. Heres the text file:
-----------------
32
abcdefg
22
hijklm
45
nopqr
-----------------
I want to store the '32' as an integer so I can perform comparisons and math on it and i want to store the 'abcdefg' as a string/character array so i can compare it with another string. The most important thing is that I want the '32' to be associated with the 'abcdefg'.

Here is my code:

PHP Code:
[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[MAX];
    
char thepattern[MAX];
};
int main(void)
{
    
struct patterninput lines[MAX];
    
char temp[MAX];
    
char *p;
    
int k=0;
    
FILE *fp fopen(SOURCE2"r");
    
FILE *fp2 fopen(SOURCE3"w");
    
    while (
fgets(tempMAXfp ) != NULL )
    {
       if ((
strchr(temp'\n')) != NULL)
          {
    
          
lines[k].mismatches atoi(temp);
          
k++;
          *
'\0';

            if (
fgets(lines[k].thepatternMAXfp ) != NULL )
           
fprintf(fp2,"%d %s"lines[k].mismatches
                            
lines[k].thepattern);
          }
       
k++;
    }

    
fclose(fp);

    
fclose(fp2);

    return 
0;
    
}[/
CODE


In the above, I'm reading a line and then storing it. But when i try to convert the string into and integer with this

lines[k].mismatches = atoi(temp);

it gives the 'incompatible types in assignment' error.
Can someone offer some help please???

Reply With Quote
  #2  
Old December 6th, 2004, 10:10 PM
arpia's Avatar
arpia arpia is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Baltimore, MD
Posts: 229 arpia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 13 m 58 sec
Reputation Power: 11
Code:
fgets(temp, MAX, fp ) != NULL 


why don't you use fscanf()
it will allow you to save directly to ints or strings

Reply With Quote
  #3  
Old December 6th, 2004, 10:19 PM
cproghelp cproghelp is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 4 cproghelp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
yes i tried that too, i did this

fscanf(fp, "%d", &lines[k].mismatches) != NULL

but then i got the warning "comparison between pointer and integer", and I dont know anything equivalent to NULL (im newb!)

maybe you can help me on that ^

Reply With Quote
  #4  
Old December 6th, 2004, 10:33 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
in your struct you create an array of ints.....
I think you were looking for something like:
Code:
struct patterninput {
   int mismatches;
   char thepattern[MAX];
}

//...

struct patterninput lines[MAX];


That way, lines[k].mismatches is in fact an int,
not a pointer to an array of ints....
__________________
True happiness is not getting what you want, it's wanting what you've already got.

My Blog

Reply With Quote
  #5  
Old December 6th, 2004, 11:26 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
This was of no help?

Reply With Quote
  #6  
Old December 6th, 2004, 11:47 PM
cproghelp cproghelp is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 4 cproghelp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
what the!
well of course that helped, username, look at my code!
im just posting here because i dont want to be bugging the same board over and over

And thanks l7sqr, that was my mistake and now i fixed it up and it works!!!

now i got a new problem when im trying to test if i can call it it in a main function

PHP 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(tempMAXfp) != NULL)
    {
       if ((
strchr(temp'\n')) != NULL)
          {
                        
          
lines[k].mismatches atoi(temp); 
          *
'\0';

           if (
fgets(lines[k].thepatternMAXfp ) != NULL )
           
fprintf(fp2,"%d %s"lines[k].mismatches
                   
lines [k].thepattern);
          }
       if(
== 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(tempMAXfp) != NULL)
    {
       if ((
strchr(temp'\n')) != NULL)
          {                
    
          
lines[k].mismatches atoi(temp); 
          *
'\0';

           if (
fgets(lines[k].thepatternMAXfp ) != NULL )
           
fprintf(fp2,"%d %s"lines[k].mismatches
                   
lines[k].thepattern);
          }
       if(
== 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...

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


Reply With Quote
  #7  
Old December 7th, 2004, 12:48 AM
jasonvene jasonvene is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2003
Posts: 705 jasonvene User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 27 sec
Reputation Power: 11
lines evaporates after the return (that's the "local variable" warning). You'll get segment faults or GPF's with that.

Also, your signature returns a char, not a char *. thepattern is an array, so returning that is essentially returning a char *, not a char.

You must figure out what to return.

If you're returning a character array, this gets sticky. Who owns it? Who deletes it?

The one you've created on the stack doesn't survive - you'll have to copy it to something....

Reply With Quote
  #8  
Old December 7th, 2004, 03:59 AM
cproghelp cproghelp is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 4 cproghelp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
yup, segmentation errors alright.
since one of my lines in the txt is like this
32
abcdefg
i think im doing the *char array, but i dont really understand the owning/deleting part.
And whats a stack? Are you saying I should make a temp variable to store the return value, and then use return value after the while loop? Ill go try that...

Reply With Quote
  #9  
Old December 7th, 2004, 05:23 AM
jasonvene jasonvene is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2003
Posts: 705 jasonvene User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 27 sec
Reputation Power: 11
When you declare the variable at the top of the function like you did, that's "on the stack". When the function returns, all that stuff disappears.

As a result, you have to return something that survives. You could allocate a character array and return that, but then your caller has to understand that it now owns the array and must delete it. It's not a good practice.

You could create a string object, and return that. It's like returning an int or a double - you're returning a copy. Copies are generated on the return so the caller gets a string object, something like:

Code:
string returns_pattern( int array_num)
{
 string rval;

 rval = whatever_character_array;

 return rval;
}


Or, pass a pointer reference:

Code:

bool return_pattern( int array_num, char *&result)
{
 result = new char[ size ];

 strcpy( result, from_source_char_array );
 
 return true;
}


This latter is a little clearer, but not by much, that the caller owns the char * array.

You could pass a string reference

bool returns_pattern( int array_num, string &result );

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Compiler Error - Incompatible types in assignment??

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap