The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Compiler Error - Incompatible types in assignment??
Discuss Compiler Error - Incompatible types in assignment?? in the C Programming forum on Dev Shed. Compiler Error - Incompatible types in assignment?? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 6th, 2004, 10:02 PM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 4
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(temp, MAX, fp ) != NULL )
{
if ((p = strchr(temp, '\n')) != NULL)
{
lines[k].mismatches = atoi(temp);
k++;
*p = '\0';
if (fgets(lines[k].thepattern, MAX, fp ) != 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??? 
|

December 6th, 2004, 10:10 PM
|
 |
Contributing User
|
|
Join Date: Jun 2003
Location: Baltimore, MD
Posts: 229
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
|

December 6th, 2004, 10:19 PM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 4
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 ^
|

December 6th, 2004, 10:33 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Constant Limbo
|
|
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
|

December 6th, 2004, 11:26 PM
|
 |
Contributing User
|
|
|
|
|

December 6th, 2004, 11:47 PM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 4
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(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...
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

|

December 7th, 2004, 12:48 AM
|
|
Contributing User
|
|
Join Date: Jun 2003
Posts: 705
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....
|

December 7th, 2004, 03:59 AM
|
|
Registered User
|
|
Join Date: Dec 2004
Posts: 4
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...
|

December 7th, 2004, 05:23 AM
|
|
Contributing User
|
|
Join Date: Jun 2003
Posts: 705
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 );
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|