字符数组,字符串,字符指针和NULL的故事

本文介绍了一个将浮点数转换为字符串的C++函数实现。该函数通过将浮点数放大1000倍并取整,然后利用字符数组进行逐位存储,并通过反转操作完成转换过程。同时对比了不同初始化字符数组的方法。
// floatToString_function.cpp : 定义控制台应用程序的入口点。
//
#include "StdAfx.h"
#include<conio.h>
#include<stdio.h>
#include <string.h>

void floattostring(float dec,char *s)
{

	int i=0,j;
	char temp;
	int ipart;
	ipart=int(dec*1000);

	printf("String is: %s",s);

	while(ipart)
	{
		s[i]=ipart%10+'0';
		ipart=ipart/10;
		i++;
	}

	for(j=i-1;j>=i/2;j--)
	{
		temp=s[j];
		s[j]=s[i-j-1];
		s[i-j-1]=temp;	
	}

}

int main()
{
	float ii;
//	char fs[20] = {NULL} ;
	char fs[] = {NULL} ;
//	char *fs = new char;
	//	*fs =NULL;


	printf("please input a number: ");
	scanf("%f",&ii);
	floattostring(ii,fs);

	printf("String is: %s",fs);

//	delete fs;

	getch();
	return 0;
}

char fs[20] = {NULL} 给字符数组里的每一个字符都为NULL;

char fs[20] = "NULL"  把NULL当做一个字符串常量付给fs[20];

char fs[] = "NULL" ; 把NULL字符串赋给fs,fs的长度为5;

char *fs = new char; *fs =NULL; fs为指向char变量的指针, 后给指针为空;

char *fs = new char;  *fs ={NULL}; 赋值错误,不能识别;

char *fs = new char;*fs ='NULL'; 字符型赋值正确;

char *fs = new char;*fs =“NULL”; 不能把字符串常量赋值给字符型变量; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值