C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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 April 7th, 2003, 09:14 PM
Xzyx987X's Avatar
Xzyx987X Xzyx987X is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 Xzyx987X User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Xzyx987X
Converting int to char *

Could someone please explain why the statement:

int intVar = 255;
printf("\nintVar = ", (char *) intVar);

produces the output:

intVar =

I can guess as to why this is a problem since the data types are totally incompatible, but the compiler doesn't produce any errors (I'm using Visual C++.) I'm probobly missing something obvious since I just started programming with C++, so could someone tell me how to accomplish what I'm trying to do properly?

Reply With Quote
  #2  
Old April 7th, 2003, 09:26 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,587 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 21 h 21 m 5 sec
Reputation Power: 997
Change your printf statement to:

printf("\nintVar = %d", intVar);

The %d tells the printf() function to print the variable as an integer. There's no need to type-cast an intvar to a char pointer.

Reply With Quote
  #3  
Old April 7th, 2003, 10:05 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,861 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 23 m 55 sec
Reputation Power: 462
FYI & for your future debugging:

You normally want the number of format variables (my term, I forget what's official; eg %d) to equal the number of arguments that follow the format string. Failure to do so will result two different kinds of problems:

1. if you have more arguments than format variables, then the last arguments will simply be ignored. Your problem is an example of this.

2. If you have more format variables than arguments -- eg,
printf("x=%d, y=%d, z=%d\n",x,y);
-- then it will try to fill that third format variable with whatever "follows" y in memory, AKA "garbage".

Back to the example:
Code:
x = 1;
y = 2;
z = 3;

printf("x=%d, y=%d, z=%d\n",x,y);

will probably produce something like:
x=1, x=2, z=-43256893

It's always a surprise to get a result like that. So when you get really wild formatted output, particularly in the last fields, the number of arguments and of format variables should be the first thing you check.

Lesson gained from personal experience.

Reply With Quote
  #4  
Old April 8th, 2003, 02:18 PM
Xzyx987X's Avatar
Xzyx987X Xzyx987X is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 Xzyx987X User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Xzyx987X
Thanks for the help. I think I'm going to read through the source of stdio.h to learn a bit more about it before I start to get too comfortable with it's functions.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Converting int to char *


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT