初级者求助
用malloc函数申请一段内存空间,并将字符串“Hello”保存到该空间并输出到屏幕,然后释放该内存空间 本帖最后由 jhq999 于 2022-11-23 10:41 编辑char* s=“Hello”;
int len=0;
while(s);
char * s1=(char*)molloc(len*sizeof(char));
/*len-=1;
while(len>=0)
{
s1=s;
len-=1;
}*/
while(len>0)
{
len-=1;
s1=s;
}
printf("%s",s1);
free(s1);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char * s = (char *) malloc(sizeof(char) * 6); //含 '\0'
strcpy(s, "Hello\0");
printf("%s", s);
free(s);
return 0;
} #include <stdio.h>
#include <stdlib.h>
int main(void)
{
char * p ;
p = (char *) malloc(16) ;
strcpy(p , "Hello") ;
printf("%s\n" , p) ;
free(p) ;
} #include <stdio.h>
using namespace std;
int main()
{
char* str = NULL;
str = (char*)malloc(10 * sizeof(char));
sprintf(str, "%s", "Hello");
printf("%s\n", str);
free(str);
return 0;
}
页:
[1]