Computing.Net > Forums > Programming > this should NOT work, but ...

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

this should NOT work, but ...

Reply to Message Icon

Name: Godgory
Date: February 13, 2006 at 13:00:33 Pacific
OS: FreeBSD 6.0
CPU/Ram: Barton 2600+/512DDR
Product: me
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: February 13, 2006 at 14:16:31 Pacific
Reply:

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.


0

Response Number 2
Name: Godgory
Date: February 14, 2006 at 09:04:17 Pacific
Reply:

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?


0

Response Number 3
Name: nails
Date: February 14, 2006 at 09:44:00 Pacific
Reply:

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.


0

Response Number 4
Name: borelli35
Date: February 19, 2006 at 10:51:25 Pacific
Reply:

===========================================================
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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: this should NOT work, but ...

Simple FOR Loop not working www.computing.net/answers/programming/simple-for-loop-not-working/20278.html

right click and copy paste not work www.computing.net/answers/programming/right-click-and-copy-paste-not-work/17468.html

win32 problem creating windows www.computing.net/answers/programming/win32-problem-creating-windows/10006.html