c语言中,怎么定义一个随意大小的字符串呢? c语言中怎么实现任意长度字符串输入
c\u8bed\u8a00\u4e2d,\u600e\u4e48\u6837\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32\u53d8\u91cf1\u3001\u5b9a\u4e49\u53d8\u91cf\u5b57\u7b26\u4e32\u3002
char str[200];
\u5b9a\u4e49\u4e00\u4e2a\u957f\u5ea6\u6700\u5927\u4e3a200\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3002
2\u3001\u5b9a\u4e49\u5e38\u91cf\u5b57\u7b26\u4e32\u3002
\u7531\u4e8e\u5e38\u91cf\u5b57\u7b26\u4e32\u5b58\u50a8\u5728\u5e38\u91cf\u7a7a\u95f4\u4e2d\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u6307\u9488\uff0c\u6307\u5411\u8be5\u5730\u5740\u5373\u53ef\u3002\u5982char *s = "test string"\u3002
\u62d3\u5c55\u8d44\u6599c\u8bed\u8a00:
C\u8bed\u8a00\u662f\u4e00\u95e8\u901a\u7528\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e94\u7528\u5e7f\u6cdb\u3002C\u8bed\u8a00\u7684\u8bbe\u8ba1\u76ee\u6807\u662f\u63d0\u4f9b\u4e00\u79cd\u80fd\u4ee5\u7b80\u6613\u7684\u65b9\u5f0f\u7f16\u8bd1\u3001\u5904\u7406\u4f4e\u7ea7\u5b58\u50a8\u5668\u3001\u4ea7\u751f\u5c11\u91cf\u7684\u673a\u5668\u7801\u4ee5\u53ca\u4e0d\u9700\u8981\u4efb\u4f55\u8fd0\u884c\u73af\u5883\u652f\u6301\u4fbf\u80fd\u8fd0\u884c\u7684\u7f16\u7a0b\u8bed\u8a00\u3002
\u5c3d\u7ba1C\u8bed\u8a00\u63d0\u4f9b\u4e86\u8bb8\u591a\u4f4e\u7ea7\u5904\u7406\u7684\u529f\u80fd\uff0c\u4f46\u4ecd\u7136\u4fdd\u6301\u7740\u826f\u597d\u8de8\u5e73\u53f0\u7684\u7279\u6027\uff0c\u4ee5\u4e00\u4e2a\u6807\u51c6\u89c4\u683c\u5199\u51fa\u7684C\u8bed\u8a00\u7a0b\u5e8f\u53ef\u5728\u8bb8\u591a\u7535\u8111\u5e73\u53f0\u4e0a\u8fdb\u884c\u7f16\u8bd1\uff0c\u751a\u81f3\u5305\u542b\u4e00\u4e9b\u5d4c\u5165\u5f0f\u5904\u7406\u5668\uff08\u5355\u7247\u673a\u6216\u79f0MCU\uff09\u4ee5\u53ca\u8d85\u7ea7\u7535\u8111\u7b49\u4f5c\u4e1a\u5e73\u53f0\u3002
\u4e8c\u5341\u4e16\u7eaa\u516b\u5341\u5e74\u4ee3\uff0c\u4e3a\u4e86\u907f\u514d\u5404\u5f00\u53d1\u5382\u5546\u7528\u7684C\u8bed\u8a00\u8bed\u6cd5\u4ea7\u751f\u5dee\u5f02\uff0c\u7531\u7f8e\u56fd\u56fd\u5bb6\u6807\u51c6\u5c40\u4e3aC\u8bed\u8a00\u5236\u5b9a\u4e86\u4e00\u5957\u5b8c\u6574\u7684\u7f8e\u56fd\u56fd\u5bb6\u6807\u51c6\u8bed\u6cd5\uff0c\u79f0\u4e3aANSI C\uff0c\u4f5c\u4e3aC\u8bed\u8a00\u6700\u521d\u7684\u6807\u51c6\u3002
\u76ee\u524d2011\u5e7412\u67088\u65e5\uff0c\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7\uff08ISO\uff09\u548c\u56fd\u9645\u7535\u5de5\u59d4\u5458\u4f1a\uff08IEC\uff09\u53d1\u5e03\u7684C11\u6807\u51c6\u662fC\u8bed\u8a00\u7684\u7b2c\u4e09\u4e2a\u5b98\u65b9\u6807\u51c6\uff0c\u4e5f\u662fC\u8bed\u8a00\u7684\u6700\u65b0\u6807\u51c6\uff0c\u8be5\u6807\u51c6\u66f4\u597d\u7684\u652f\u6301\u4e86\u6c49\u5b57\u51fd\u6570\u540d\u548c\u6c49\u5b57\u6807\u8bc6\u7b26\uff0c\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u5b9e\u73b0\u4e86\u6c49\u5b57\u7f16\u7a0b\u3002
\u5176\u5b9e\u6838\u5fc3\u5c31\u662f\u5b9e\u73b0\u52a8\u6001\u6570\u7ec4\uff0c\u65e0\u8bba\u5b57\u7b26\u4e32\u591a\u957f\uff0c\u90fd\u80fd\u591f\u50a8\u5b58\u3002
\u7528getchar\u4e00\u4e2a\u4e00\u4e2a\u5f97\u5230\u5b57\u7b26\uff0c\u76f4\u5230\u6587\u4ef6\u7ed3\u675f\u3002
\u6838\u5fc3\u4ee3\u7801\u5b9e\u73b0\uff1a
int n=0,k=100; //\u521d\u59cb\u5316\uff0c\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a0\uff0c\u5b57\u7b26\u4e32\u7a7a\u95f4\u957f\u5ea6\u4e3a100 char * s2 = NULL,s1 = (char *)malloc(100); //\u521d\u59cb\u5316\u5b57\u7b26\u4e32\u7a7a\u95f4 if (s1 == NULL) return; //\u5185\u5b58\u5206\u914d\u5931\u8d25\uff0c\u8fd4\u56de while((c=getchar())!=EOF){ //\u5224\u65ad\u662f\u5426\u5230\u6587\u4ef6\u7ed3\u675f\uff0c\u4e00\u4e2a\u4e2a\u8bfb\u53d6\u5b57\u7b26 if(k<=n){ //\u5f53\u524d\u5b57\u7b26\u4e32\u957f\u5ea6\u5927\u4e8e\u7b49\u4e8e\u5b57\u7b26\u4e32\u7a7a\u95f4\u957f\u5ea6\u65f6 k*=2; //\u957f\u5ea6\u589e\u957f2\u500d s2 = (char *)realloc(s1,k); //\u91cd\u65b0\u5206\u914d\u5185\u5b58 if(s2 == NULL){ //\u5185\u5b58\u5206\u914d\u5931\u8d25 free(s1); //\u91ca\u653e\u5df2\u5206\u914d\u5185\u5b58 return; }else{ //\u5185\u5b58\u5206\u914d\u6210\u529f s1=s2; } } s1[n++]=c; //\u5b57\u7b26\u4e32\u8d4b\u503c\u5b57\u7b26 } s1[n]=0; //\u5b57\u7b26\u4e32\u672b\u5c3e\u7f6e\u4e3a\0
你要的随意大小字符串,刚出炉,还是热的。
#include <stdio.h>
#include<malloc.h>
#include<string.h>
#define maxsize 10
int main()
{
char *str,*strSave,cSave;
int i,n=2,strSize;
strSize=maxsize;
str=(char *)malloc(sizeof(char)*strSize);
printf("输入任意长度字符串:");
for(i=0;i<strSize;i++)
{
scanf("%c",&cSave);
if(i==strSize-1)//当输入到预设内存地址最后一位
{
if(cSave=='
')
{
cSave=0;
str[i]=cSave;
}
else
{
str[i]=0;
strSize=maxsize*n++;
strSave=str;
str=(char *)malloc(sizeof(char)*strSize);//当内存不够,增加1倍
strcpy(str,strSave);//新内存空间继承数据
str[i]=cSave;//保存新输入值
free(strSave);//释放旧内存空间
strSave=NULL;//防野指针
}
}
else
{
if(cSave=='
')
{
cSave=0;
str[i]=cSave;
break;
}
else
{
str[i]=cSave;
}
}
}
printf("打印输入的字符串为:%s",str);
return 0;
}
“随意大小”肯定是做不到的,即使软件平台不作限制,硬件资源也是有限的。正确的理解应当是使用者的主观感觉上没有上限限制。其于这种理解,下面提供的代码能做到这一点:
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
char *mygets(char *p){
int N = sizeof(int)==4 ? 262144 : 32767;
char *q;
if((q=(char *)malloc(N))==NULL){//申请一个足够大的临时空间
printf("Application memory failure...
");
return NULL;
exit(0);
}
gets(q);
if((p=(char *)malloc(strlen(q)+1))==NULL){//按实际需要申请空间
printf("Failed to create a string...
");
return NULL;
exit(0);
}
strcpy(p,q);
free(q);//释放临时空间
return p;
}
int main(void){
char *str=NULL;
printf("Input a string...
");
if(!(str=mygets(str))) return 0;
printf("%s
",str);
free(str);
return 0;
}
c语言不能实现,字符串本质上还是字符数组以‘\0’字符结尾,在分配空间时必须指定数组长度,
例如:char str[]="abcdef";
C是不可能实现的
如果你要用C实现,至少要提供一个长度参数
绛旓細浣犺鐨闅忔剰澶у皬瀛楃涓诧紝鍒氬嚭鐐夛紝杩樻槸鐑殑銆俰nclude <stdio.h>#include<malloc.h>#include<string.h>#define maxsize 10int main(){ char *str,*strSave,cSave; int i,n=2,strSize; strSize=maxsize; str=(char *)malloc(sizeof(char)*strSize); printf("杈撳叆浠绘剰闀垮害瀛楃...
绛旓細鏈夊嚑绉嶆柟娉曪細1锛夊彲浠ュ畾涔変竴涓冻澶熷ぇ鐨勬暟缁勶紝淇濊瘉澶熺敤灏卞ソ銆2锛変娇鐢ㄩ摼琛ㄧ粨鏋勶紝鐢ㄤ竴涓敵璇蜂竴涓厓绱犵殑绌洪棿 3锛夌敤malloc鍏堢敵璇蜂竴瀹氱殑绌洪棿浣滀负鏁扮粍鐨勭┖闂达紝绛夊埌涓嶅鐢ㄧ殑鏃跺欙紝鍐嶉噸鏂癿alloc涓涓洿澶х殑绌洪棿锛屽皢鏃х殑閭d釜绌洪棿閲岀殑鏁版嵁鎷疯礉杩囨潵锛屼綔涓烘暟缁勭殑鏂扮殑绌洪棿锛岀劧鍚巉ree鎺夋棫鐨勯偅涓┖闂村嵆鍙
绛旓細鍙兘鏍规嵁涓嶅悓绫诲瀷鏉ラ檺瀹氳寖鍥达紝涓旇寖鍥撮兘鏄2鐨凬娆″箓銆傝嫢鏍规嵁绋嬪簭闄愬畾锛屽垯鍙互鍦ㄧ▼搴忎腑鍔犲叆杩欐牱鐨勫垽鏂拰璧嬪硷細if(num>10000) num=10000; // num 涓轰綘瑕侀檺瀹氱殑鏁存暟銆
绛旓細瀹氫箟鍔ㄦ佹暟缁勶紝渚嬪锛歩nclude <stdio.h> include <stdlib.h> int len;char *str;printf("杈撳叆鏁扮粍鐨勯暱搴︼細");scanf("%d",&len);str=(char*)malloc(len*sizeof(char));涓簊tr鍒嗛厤鍒嗛厤len涓瓧绗澶у皬鐨勭┖闂淬
绛旓細free int main(){ int i, n;int* p; // 澹版槑涓烘寚閽 scanf( "%d", &n );p = ( int* )malloc( sizeof(int) * n ); // 浠ュ瓧鑺備负鍗曚綅鍒嗛厤 for ( i = 0; i < n; ++i ) { p[i] = i+1;printf( "%d ", p[i] );} free( p ); // 鐢ㄥ畬璁板緱閲婃斁 return 0;} ...
绛旓細鎴戠畝鍗曞啓浜涓涓紝涓嶇煡閬撲綘鏄笉鏄杩欐牱涓涓晥鏋滐細include<stdio.h> include<windows.h> include<conio.h> include<stdlib.h> void gotoxy(int x,int y){ HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);COORD coordScreen={x,y};SetConsoleCursorPosition(hConsole,coordScreen);} void main...
绛旓細C娌℃湁鎻愪緵鐜版垚鐨勫姙娉曘備笉杩囨垜鍦ㄥ疄闄呭伐浣滀腑鍋氳繃锛岀敤璧锋潵鎸虹埥鐨勩傚0鏄涓涓鎵闇绫诲瀷鐨勬寚閽坧锛岀敤malloc鍑芥暟鐢宠涓涓緝灏忕殑绌洪棿(渚嬪10涓厓绱)骞跺皢棣栧湴鍧璧嬬粰p灏辨瀯鎴愪簡浠涓洪鍦板潃鐨勪竴缁存暟缁勶紱鐒跺悗鍚戞暟缁勮緭鍏ユ暟鎹紱杈撳叆婊″悗杩橀渶杈撳叆鏃讹紝鐢ㄤ笂杩板悓鏍峰姙娉曞彟鐢宠涓涓互鎸囬拡q涓洪鍦板潃鐨勮緝澶х殑鏁扮粍(渚嬪20涓...
绛旓細鍦C璇█涓紝鏁扮粍鐨澶у皬閫氬父鏄湪缂栬瘧鏃剁‘瀹氱殑锛屾棤娉曞湪杩愯鍔ㄦ佹敼鍙樸備絾鏄紝浣犲彲浠ヤ娇鐢ㄦ寚閽堝拰鍔ㄦ佸唴瀛樺垎閰嶆潵瀹炵幇绫讳技鐨勬晥鏋溿傚叿浣撴楠ゅ涓嬶細1. 棣栧厛锛屽0鏄涓涓鎸囬拡鍙橀噺鍜屼竴涓暣鍨嬪彉閲忥紝鐢ㄤ簬瀛樺偍杈撳叆鐨勬暟缁勫ぇ灏忋俰nt *arr;int size;2. 鎺ヤ笅鏉ワ紝閫氳繃鐢ㄦ埛杈撳叆鑾峰彇鏁扮粍鐨勫ぇ灏忋俻rintf("璇疯緭鍏ユ暟缁勭殑澶у皬锛");...
绛旓細鍔ㄦ佹暟缁勫綋鐒惰鑷繁鍦ㄥ爢涓敵璇风┖闂村晩锛屽叿浣撲唬鐮佹槸杩欐牱鐨 int **p;p=(int **)malloc(sizeof(int)*n);for(i=0;i<n;i++)p[i]=(int *)malloc(sizeof(int)*n);杩欐牱灏卞垱寤轰簡涓涓鍔ㄦ佺殑浜岀淮鏁扮粍p[n][n]銆
绛旓細鑻ntx=2,y=3;,鍒檟&y鐨勭粨鏋滄槸___2___銆&绗﹀彿鏄滄寜浣嶄笌鈥濊繍绠楃銆傛槸灏嗙鍙蜂袱杈圭殑鏁村瀷鏁帮紝鍏堣浆鎹负 浜岃繘鍒舵暟锛岀劧鍚庢寜姣忎竴浣嶄笌杩愮畻銆傛渶鍚庡皢寰楀埌鐨勨滀簩杩涘埗鏁扳濆啀杞崲涓哄崄杩涘埗锛屽氨鏄粨鏋滀簡銆2 == 10 3 == 11 2 & 3 = 10 == 2 缁撴灉鏄2 ...