Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, guys
I am experiencing a really odd situation
here. Take a look at the following code:
///////////////////////////////
int text;
int iframe1;
int iframe2;
while (!feof(source)){
fgets(line, MAXLINELENGTH, source);
printf("%s",line);
if (strlen(line) == 0){
printf("stopping right now!\n");
}
iframe1 = strtok(line,"{}");
iframe2 = strtok(NULL,"{}");
text = strtok(NULL,"{}");
printf("line: {%s}{%s}%s", iframe1,
iframe2, text);
}
////////////////////////////////
I mention that the line format is:
{123}{124}some text here.
This is jost for some tests, I will actually
do some changes in iframe1 and iframe2
before typing them again.
Well, this code should NOT work, I say,
because: I store char* in int, or at least
some warning at compile time should be
given, but there are none. When I modify the
types if those 3 variables in char* it
should work and thus, give me no warnings.
It does work, but it gives warnings
"conversion.c:37: warning: assignment makes
pointer from integer without a cast". I know
that strtok returns char*. should this have
changed? If yes how does C store pointers in
integers?
Give me some explenations, please, I am
dying to know where the problem is.
Alex.

I'm using a Solaris 7 "C" compiler and your code wouldn't compile at all. The strtok function returns a pointer-to-a-character string, so to get your code to compile, I did this:
include <stdio.h>
#include <string.h>int main(int argc, char *argv[])
{
FILE *source;
char *text;
char *iframe1;
char *iframe2;
char line[100];if ((source=fopen("myfile", "r")) == NULL)
{
printf("ERROR: can not open myfile file:\n");
exit(1);
}while (!feof(source))
{
fgets(line, 100, source);
if (strlen(line) == 0)
{
printf("stopping right now!\n");
exit(1);
}
}
iframe1 = strtok(line,"{}");
iframe2 = strtok(NULL,"{}");
text = strtok(NULL,"{}");printf("{%s}\n", iframe1);
printf("{%s}\n", iframe2);
printf("{%s}\n", text);
}If you want to convert the first 2 tokens to integer, I think you need to use atoi after parsing with strtok.

Yes, that's what I thought too,
but then again why does it work? And why is it giving warnings with this kind of implementation and no error at all with what I wrote?

Not being a FreeBSD person, I can't tell you.
Some "C" compilers are more forgiving than others. Perhaps your compiler made assumptions about your declarations, and also automatically did the char-to-integer conversions.
In my Solaris world, your stub program wouldn't even compile.

===========================================================
Some compilers allow you to disable type cast and like errors from preventing the link process from continuing. i.e. if this type of error is disabled (to allow for loose and assumed typecasting) then the compiler will link with the assumption that you have specific intentions and know what the type is and therefore will not programmatically cause an error. USE AT YOUR OWN RISK!!!...John W. Borelli
IT Specialist
Hawkeye Security
borelli35

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |