LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2006, 02:18 AM   #1
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Rep: Reputation: 15
convert char * to int?


I am working on a program and I read in from a file a position which is a number. I am using strtok() to get my data from the file. I want to do this:

tourndata[x].position = tokenPtr;

here is how they are declared:

char *tokenPtr;

struct tourndata{
char name[25];
char handle[25];
int position;
};


I get a compiler error that says "invalid conversion from char * to int".

how can I convert the tokenPtr to an int? Or if you have any other ideas feel free to comment.

Thanks.
 
Old 05-01-2006, 02:22 AM   #2
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Rep: Reputation: 16
atoi() will do the job i think.
 
Old 05-01-2006, 03:14 AM   #3
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
or cast it (int)

tourndata[x].position = (int) tokenPtr;

Last edited by kalleanka; 05-01-2006 at 03:21 AM.
 
Old 05-01-2006, 03:21 AM   #4
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
Why do you want do cast it anyway? Maybe it would be easier to make position into a char *. If not, then kalleanka has given you the answer.

Quote:
atoi() will do the job i think.
Nope, wrong context.
 
Old 05-01-2006, 03:24 AM   #5
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
IOerror got a point!

Its a bad thing to cast since it can giv faulty code. Better to fix the code so the types fits.
 
Old 05-01-2006, 03:32 AM   #6
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Original Poster
Rep: Reputation: 15
When I read in the position from the text file I use tokenPtr to point to it. Then I want to use it as an int in my program and then output it back to the text file as an int when I am done. Don't you think atoi would work good for this purpose?
 
Old 05-01-2006, 04:38 AM   #7
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 34
I'm still not clear on exactly what you're trying to do. Is the number a string of digits in the file, or is it just the file position, a pointer to some offset into the file?

In the former case, I misunderstood you at first and scoban was correct, although I would suggest strtol instead of atoi, since atoi does not catch errors. In the latter case, then do as kalleanka and I suggested, either change position to a char *, or cast it, if you must.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
char to int: a twist Guru3 Programming 5 12-05-2004 08:49 AM
Converting int value to char liguorir Programming 8 05-23-2004 07:21 PM
int(char(254)) becomes kalleanka Programming 2 02-17-2004 03:35 PM
int, long in char * marek Programming 10 09-01-2003 10:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration