字符*(指针)功能 [英] Char * (pointer) function
问题描述
我需要在一个函数一个char *传递,并将它设置为一个CString值。我可以正确设置它作为函数的字符串,但它似乎并没有在那个叫摆在首位的字符*函数的函数正确地打印出来。
INT l2_read(字符*块,INT长度)
{
块=的malloc(sizeof的(char)的*长度); INT I;
对于(i = 0; I<长度;我++){
焦炭℃;
如果(读(和C)℃的)返回(-1); //这个获取单个字符
块[I] = C;
} 的printf(%S,块); //这个精细打印
返回1;
}
//主
字符*串;
int值= l2_read(字符串,16);
的printf(%S,块); //打印错误
在C,一切都是按值传递。一般的规则要记住的是,你不能改变传递给函数的参数值。如果你想传递的东西需要改变,你需要一个指针传递给它。
所以,在你的功能,你想改变块
。 块
是的char *
。为了能够修改的char *值
,你需要传递一个指向,即的char **
。
INT l2_read(焦** chunkp,INT长度)
{
INT I;
* chunkp =的malloc(长* sizeof的** chunkp);
如果(* chunkp == NULL){
返回-2;
}
对于(i = 0; I<长度;我++){
焦炭℃;
如果(读(和C)℃的)返回-1;
(* chunkp)[i] = C;
}
的printf(%S,* chunkp);
返回1;
}
,然后在的main()
:
的char *字符串;
int值= l2_read(安培;串,16);
如果(价值== 1){
的printf(%S,字符串); / *纠正错字* /
免费(字符串); / *调用者调用free()* /
}否则如果(价值== -2){
/ * malloc的失败,处理错误* /
}其他{
/ *读取失败* /
免费(字符串);
}
传递按值用C之所以与strtol()
,关于strtod()
等。 ,需要的char ** endptr
参数,而不是的char * endptr
—他们希望能够将的char *
值,以第一个无效字符的地址,他们可以影响一个的char *
在来电者是唯一的出路获得一个指向它,即获赠的char *
。同样,在你的函数,你希望能够改变的char *
值,这意味着你需要一个指针的char *
。
希望有所帮助。
I need to pass in a char * in a function and have it set to a cstring value. I can properly set it as a string in the function, but it doesn't seem to print out correctly in the function that called the char * function in the first place.
int l2_read(char *chunk,int length)
{
chunk = malloc( sizeof(char) * length);
int i;
for(i = 0; i < length; i++){
char c;
if(read(&c) < 0) return (-1); // this gets a single character
chunk[i] = c;
}
printf("%s",chunk); // this prints fine
return 1;
}
// main
char *string;
int value = l2_read(string,16);
printf("%s",chunk); // prints wrong
In C, everything is passed by value. A general rule to remember is, you can't change the value of a parameter passed to a function. If you want to pass something that needs to change, you need to pass a pointer to it.
So, in your function, you want to change chunk
. chunk
is char *
. To be able to change the value of the char *
, you need to pass a pointer to that, i.e., char **
.
int l2_read(char **chunkp, int length)
{
int i;
*chunkp = malloc(length * sizeof **chunkp);
if (*chunkp == NULL) {
return -2;
}
for(i = 0; i < length; i++) {
char c;
if (read(&c) < 0) return -1;
(*chunkp)[i] = c;
}
printf("%s", *chunkp);
return 1;
}
and then in main()
:
char *string;
int value = l2_read(&string, 16);
if (value == 1) {
printf("%s", string); /* corrected typo */
free(string); /* caller has to call free() */
} else if (value == -2) {
/* malloc failed, handle error */
} else {
/* read failed */
free(string);
}
Pass-by-value in C is the reason why strtol()
, strtod()
, etc., need char **endptr
parameter instead of char *endptr
—they want to be able to set the char *
value to the address of the first invalid char, and the only way they can affect a char *
in the caller is to receive a pointer to it, i.e., receive a char *
. Similarly, in your function, you want to be able to change a char *
value, which means you need a pointer to a char *
.
Hope that helps.
这篇关于字符*(指针)功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!