LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2024|回复: 17

如何用char型指针指向一个整型数据?

[复制链接]
发表于 2006-12-19 08:33:05 | 显示全部楼层 |阅读模式
有一个int数据,如int i=5; 如何定义一个char型指针 char *p; 让p指向i呢?
在图形界面编程MiniGUI中有个函数SetWindowText()可在界面上显示一串字符,其参数是指向这串字符的指针,我现在想显示一个数字,怎么办啊?
发表于 2006-12-19 09:17:09 | 显示全部楼层
把数字转换为字符串,然后显示之
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-12-19 09:28:45 | 显示全部楼层
能详细点吗? 把语句写出来啊
回复 支持 反对

使用道具 举报

发表于 2006-12-19 09:50:52 | 显示全部楼层
atoi
<stdlib.h>        
  cplusplus.com  
int  atoi ( const char * string );

Convert string to integer.
  Parses string interpreting its content as a number and returns an int value.

Parameters.

string
    String representing an integer number. The number is considered until a non-numeric character is found (digits, and signs are considered valid numeric characters for this parameter as specified in format). The format used is:
    [whitespaces][+|-][nnnnn]
    (where whitespaces are any tab or space character and nnnnn may be any number of digits)

Return Value.
  The converted integer value of the input string.
  On overflow the result is undefined.
  If an error occurs 0 is returned.

Portability.
  Defined in ANSI-C.

Example.

/* atoi example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char szInput [256];
  printf ("Enter a number: ");
  gets ( szInput );
  i = atoi (szInput);
  printf ("Value entered is %d, and its double %d",i,i*2);
  return 0;
}

Output:
Enter a number: 73
Value entered is 73, and its double 146

See also.
  atof, atol, ecvt, fcvt, gcvt, strtod
回复 支持 反对

使用道具 举报

发表于 2006-12-19 09:52:02 | 显示全部楼层
itoa
<stdlib.h>        
  cplusplus.com  
char *  itoa ( int value, char * buffer, int radix );

Convert integer to string.
  Converts an integer value to a null-terminated string using the specified radix and stores the result in the given buffer.
  If radix is 10 and value is negative the string is preceded by the minus sign (-). With any other radix, value is always considered unsigned.
  buffer should be large enough to contain any possible value: (sizeof(int)*8+1) for radix=2, i.e. 17 bytes in 16-bits platforms and 33 in 32-bits platforms.

Parameters.

value
    Value to be represented as a string.
buffer
    Buffer where to store the resulting string.
radix
    Numeral radix in which value has to be represented, between 2 and 36.

Return Value.
  A pointer to the string.

Portability.
  Not defined in ANSI-C. Supported by some compilers.

Example.

/* itoa example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char buffer [33];
  printf ("Enter a number: ");
  scanf ("%d",&i);
  itoa (i,buffer,10);
  printf ("decimal: %s\n",buffer);
  itoa (i,buffer,16);
  printf ("hexadecimal: %s\n",buffer);
  itoa (i,buffer,2);
  printf ("binary: %s\n",buffer);
  return 0;
}

Output:
Enter a number: 1750
decimal: 1750
hexadecimal: 6d6
binary: 11011010110

See also.
  atof, atol, ecvt, fcvt, gcvt, strtod
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-12-19 10:30:17 | 显示全部楼层
谢谢楼上的兄弟,我马上试试
回复 支持 反对

使用道具 举报

发表于 2006-12-19 10:38:36 | 显示全部楼层
别上当,没有itoa的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-12-19 10:57:31 | 显示全部楼层
在Linux下怎么没有itoa函数啊?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-12-19 11:01:28 | 显示全部楼层
在Linux下怎么没有itoa函数啊?
回复 支持 反对

使用道具 举报

发表于 2006-12-19 12:24:53 | 显示全部楼层
LZ的这个问题有点奇怪阿。你所说的那个API所接受的是字串参数,即使你让chap *p指向一个整形数,那也不能解决问题,程序只会把你那个整形数转化为对应编码的ASCII来处理。

你的问题应该是怎样将一个整型数转化为一个字串,然后将这个字串作为那个API的参数使用。这可以用sprintf函数来处理。譬如:
  1. #include <stdio.h>
  2. int main(void)
  3. {
  4.     int i=6000;
  5.     char str[100];
  6.     sprintf(str,"%d",i);
  7.     printf("%s\n",str);
  8.     return 0;
  9. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表