char vs int [英] char vs int

查看:70
本文介绍了char vs int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>

#include< string.h>

#include< stdlib.h>

int main(void){

char * p;

p = malloc(4);

strcpy(p," tja") ;

printf("%s \ n",p);

免费(p);

返回0;

}


为什么是pa char而不是int? MPJ

解决方案




Merrill& Michele写道:

#include< stdio.h>
#include< string.h>
#include< stdlib.h>
int main( void){
char * p;
p = malloc(4);


检查malloc的返回值。永远。

strcpy(p," tja");


为了安全起见,你可以做例如

* p =''\ 0'';

strncat(p," tja",4-1);

printf("%s \ n",p);
免费(p);
返回0;
}

为什么是pa char而不是int? MPJ




p是一个char *。绝对不是char或int。

请在发送之前按照简单的重新读取消息的规则

,因为我们不得不猜测是什么

你可能意味着除了平均很高的价值之外,很多你的消息都是b $ b渗透的。


我的猜测是:

"字符常量是int类型和许多函数

返回int /当我们处理时有int参数

单个字符 - 为什么我们在char数组或char *变量指向的内存中存储字符串

?"


是你的问题是什么?

为此,我有一个答案。

-Michael

-

电子邮件:我的是一个gmx dot de地址。




" Merrill&米歇尔" <是******** @ comcast.net>在消息中写道

news:yr ******************** @ comcast.com ...

# include< stdio.h>
#include< string.h>
#include< stdlib.h>
int main(void){
char * p;
p = malloc(4);
strcpy(p," tja");
printf("%s \ n",p);
免费(p) ;
返回0;
}

为什么是pa char而不是int? MPJ




因为你正在使用一个需要

a char *的函数复制一个char数组?


Merrill& Michele写道:

#include< stdio.h>
#include< string.h>
#include< stdlib.h>
int main( void){
char * p;
p = malloc(4);
strcpy(p," tja");
printf("%s \ n" ,p);
免费(p);
返回0;
}

为什么pa char而不是int? MPJ



因为你,程序员,把它变成了一个char指针。

这似乎很好,因为你正在处理一个C字符串,

和字符串通常最好被操作为序列

的字符,它也是%s的字符串。在你的printf声明中

预期..


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *p;
p=malloc(4);
strcpy(p, "tja");
printf("%s\n", p);
free(p);
return 0;
}

Why is p a char and not an int? MPJ

解决方案



Merrill & Michele wrote:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *p;
p=malloc(4);
Check the return value of malloc. Always.
strcpy(p, "tja");
To be on the safe side, you could for example do
*p = ''\0'';
strncat(p,"tja",4-1);
printf("%s\n", p);
free(p);
return 0;
}

Why is p a char and not an int? MPJ



p is a char *. It is definitely not a char or int.
Please follow the simple rule of rereading your message
before you send it as we otherwise have to guess what
you could have meant apart from the on average high
level of weirdness by which many of your messages are
permeated.

My guess is:
"character constants are of type int and many functions
return int/have int parameters when we are dealing with
single characters -- why is it then that we store strings
in char arrays or memory pointed to by char * variables?"

Is that your question?
For that I have an answer.
-Michael
--
E-Mail: Mine is a gmx dot de address.



"Merrill & Michele" <be********@comcast.net> wrote in message
news:yr********************@comcast.com...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *p;
p=malloc(4);
strcpy(p, "tja");
printf("%s\n", p);
free(p);
return 0;
}

Why is p a char and not an int? MPJ



Becuase you are copying a char-array into it using a function which requires
a char*?


Merrill & Michele wrote:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *p;
p=malloc(4);
strcpy(p, "tja");
printf("%s\n", p);
free(p);
return 0;
}

Why is p a char and not an int? MPJ


Cause you, the programmer, made it a char pointer.
Which seems good, as you''re coping a C string to it,
and strings are usually best manipulated as sequences
of chars, it is also what "%s" in your printf statement
would expect..


这篇关于char vs int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆