指针作为函数的参数 [英] A pointer as a porameter of the function

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

问题描述

你好,


经过一段时间的思考后,我找到了一些适合我最好的解决方案。如果我错了,请更正。

函数有两个参数。一个字符串数组,更好地说是一个指向

it,char * p_buf和int size。

int read_name(char * p_buf,int size){

char * p_item_name_1;

char * p_item_name;


printf("输入说明:");

/ *我们希望没有比大小更多的字符! * /

if(fgets(p_item_name_1,size,stdin)!= NULL){

/ *如果输入包含新行* /

if((p_item_name = strchr(p_item_name_1,''\ n''))!= NULL){

* p_item_name =''\'''; / *摆脱新行,指针p_item_name

指向它* /

}

else {

while(getchar()!=''\ n''){

;

}

}

strcpy(p_buf,p_item_name_1); / *现在将其复制到缓冲区

由来电者提供* /

返回EXIT_SUCCESS;

}

否则{

返回EXIT_FAILURE;

}

}

主要的
可能是这样的这个:


char * p_new_buf;

char * buf;

int size = 10;

/ *这里我们可以用malloc缓冲区* /

p_buf = malloc(sizeof(char)* size);


svata

Hello there,

after some time of pondering I come to some solution which would suit
me best. Please correct, if I am wrong.
Function has two parameters. A string array, better said a pointer to
it, char *p_buf and int size.
int read_name( char *p_buf, int size) {
char *p_item_name_1;
char *p_item_name;

printf("Enter the description: ");
/* we want no more chracters than size!!! */
if (fgets(p_item_name_1, size, stdin) != NULL){
/* if the input contains a new line */
if (( p_item_name = strchr(p_item_name_1, ''\n'')) != NULL ){
*p_item_name = ''\0''; /* get rid of new line, pointer p_item_name
points to it*/
}
else {
while(getchar() != ''\n''){
;
}
}
strcpy(p_buf, p_item_name_1); /* and now copy it to the buffer
provided by caller */
return EXIT_SUCCESS;
}
else {
return EXIT_FAILURE;
}
}

in the main might be something like this:

char *p_new_buf;
char *buf;
int size = 10;
/* and here we can malloc buffer */
p_buf = malloc(sizeof(char) * size);

svata

推荐答案



svata写道:

svata wrote:

你好,<经过一段时间的思考后我得到了一些解决方案,这个方案最适合我。如果我错了,请更正。

函数有两个参数。一个字符串数组,更好地说是一个指向

it,char * p_buf和int size。


int read_name(char * p_buf,int size){

char * p_item_name_1;

char * p_item_name;
Hello there,

after some time of pondering I come to some solution which would suit
me best. Please correct, if I am wrong.
Function has two parameters. A string array, better said a pointer to
it, char *p_buf and int size.
int read_name( char *p_buf, int size) {
char *p_item_name_1;
char *p_item_name;



两个差异化的变量名称......

Two poorly differentiated variable names...


printf("输入说明:" );

/ *我们不想要比大小更多的字符! * /
printf("Enter the description: ");
/* we want no more chracters than size!!! */



其实你可能不想超过(大小-1),因为你必须_always_

允许尾随'' \\ 0''。

Actually you probably want no more than (size -1), as you must _always_
allow for the trailing ''\0''.


if(fgets(p_item_name_1,size,stdin)!= NULL){
if (fgets(p_item_name_1, size, stdin) != NULL){



p_item_name_1尚未初始化。你现在把数据读入内存中的一些任意位置。

p_item_name_1 hasn''t been initialised. You now read data into some
arbitrary place in memory.


/ *如果输入包含一个新行* /

if((p_item_name = strchr(p_item_name_1,''\ n''))!= NULL){
/* if the input contains a new line */
if (( p_item_name = strchr(p_item_name_1, ''\n'')) != NULL ){



为什么要使用该名称作为地址换行?它没有帮助

可读性。

Why use that name for the address of the newline? It doesn''t help
readability.


* p_item_name =''\'''; / *摆脱新行,指针p_item_name

指向它* /

}

else {

while(getchar()!=''\ n''){

;

}
*p_item_name = ''\0''; /* get rid of new line, pointer p_item_name
points to it*/
}
else {
while(getchar() != ''\n''){
;
}



你仍然没有处理EOF ...

You still don''t deal with EOF...


}

strcpy(p_buf,p_item_name_1); / *现在将其复制到缓冲区

由来电者提供* /
}
strcpy(p_buf, p_item_name_1); /* and now copy it to the buffer
provided by caller */



为什么你没有读到那里开始与?

Why didn''t you just read into there to start with?


返回EXIT_SUCCESS;

}

else {

返回EXIT_FAILURE ;

}

}

主要的
可能是这样的:


char * p_new_buf;

char * buf;

int size = 10;

/ *这里我们可以使用malloc buffer * /

p_buf = malloc(sizeof(char)* size);
return EXIT_SUCCESS;
}
else {
return EXIT_FAILURE;
}
}

in the main might be something like this:

char *p_new_buf;
char *buf;
int size = 10;
/* and here we can malloc buffer */
p_buf = malloc(sizeof(char) * size);



为什么要声明两个char *项目,p_new_buf和buf,但是然后将

分配给p_buf?


(通常最好是实际编写和编译你的代码,然后切割

并粘贴到你发布到新闻组的消息中,然后这样的

废话可以避免)。


malloc()并不总是成功。你应养成

的习惯,检查它是否确实......

Why do you declare two char * items, p_new_buf and buf, but then assign
to p_buf?

(It''s usually best to actually write and compile your code, then cut
and paste into the message you post to the newsgroup, then this sort of
nonsense can be avoided).

malloc() doesn''t always succeed. You should develop the habit of
checking whether it did...


svata写道:
svata wrote:

你好,


经过一段时间的思考后,我找到了一些适合我最好的解决方案。如果我错了,请更正。

函数有两个参数。一个字符串数组,更好地说是一个指向

it,char * p_buf和int size。
Hello there,

after some time of pondering I come to some solution which would suit
me best. Please correct, if I am wrong.
Function has two parameters. A string array, better said a pointer to
it, char *p_buf and int size.



如果花费一些时间来学习基础知识,那么

试图编码。

It would be better if took some time to learn the basics before
attempting to code.


int read_name(char * p_buf,int size){
int read_name( char *p_buf, int size) {



size可以是const int。

size could be made const int.


char * p_item_name_1;

char * p_item_name;


printf("输入描述:");
char *p_item_name_1;
char *p_item_name;

printf("Enter the description: ");



请注意,除非输出以换行符终止_或_

调用fflush(),否则输出可能不会出现如果预料到的话。

Be aware that unless the output is terminated with a newline _or_ a
called to fflush() is made, the output may not appear when expected.


/ *我们希望没有比大小更多的字符! * /

if(fgets(p_item_name_1,size,stdin)!= NULL){
/* we want no more chracters than size!!! */
if (fgets(p_item_name_1, size, stdin) != NULL){



你在哪里为这个输入分配了空间? p_item_name_1是声明的
,但是没有初始化为指向一块内存用于保存

输入,niether是否设置为NULL。上面的语句将确保fgets()尝试写入p_item_name_1恰好是指向的b / b
,这很可能导致未定义的行为。

Where have you allocated space for this input? p_item_name_1 is
declared, but is not initialised to point a block of memory for holding
the input, niether is it set to NULL. The above statement will ensure
that fgets() tries to write to wherever p_item_name_1 happens to be
pointing, most likely resulting in undefined behaviour.


/ *如果输入包含新行* /

if((p_item_name = strchr(p_item_name_1,''\ n''))!= NULL) {
/* if the input contains a new line */
if (( p_item_name = strchr(p_item_name_1, ''\n'')) != NULL ){



你已经将p_item_name_1与上面的fgets()一起使用了,但是在这里你使用另一个

指针,再次未初始化。更多未定义的行为。

You''ve used p_item_name_1 with fgets() above but here you use another
pointer, again uninitialised. More undefined behaviour.


* p_item_name =''\'''; / *摆脱新行,指针p_item_name

指向它* /

}

else {

while(getchar()!=''\ n''){

;

}

}

strcpy(p_buf,p_item_name_1); / *现在将其复制到缓冲区

由调用者提供* /
*p_item_name = ''\0''; /* get rid of new line, pointer p_item_name
points to it*/
}
else {
while(getchar() != ''\n''){
;
}
}
strcpy(p_buf, p_item_name_1); /* and now copy it to the buffer
provided by caller */



检查调用者是否确实传递了非空指针,

最大指针检查你可以在标准C内做。

Check wether the caller has indeed passed a non-null pointer, the
maximum pointer checking you can do within standard C.


返回EXIT_SUCCESS;

}

else {

返回EXIT_FAILURE;

}

}


in主要可能是这样的:


char * p_new_buf;

char * buf;

int size = 10;

/ *这里我们可以用malloc缓冲区* /

p_buf = malloc(sizeof(char)* size);
return EXIT_SUCCESS;
}
else {
return EXIT_FAILURE;
}
}

in the main might be something like this:

char *p_new_buf;
char *buf;
int size = 10;
/* and here we can malloc buffer */
p_buf = malloc(sizeof(char) * size);



首先sizeof(char)总是一个,所以你不需要包含它。

其次总是检查malloc()的返回值以及

失败。如果你习惯于检查所有

函数的返回值是否失败,以后会更容易。

Firstly sizeof(char) is always one, so you need not include it.
Secondly always check the return value of malloc() and the like for
failure. If you get into the habit of checking the return value of all
functions for failure, it will be a lot more easier later on.

所以我重写了它:


/ *函数检查malloc * /

int malloc_check(char * in){

if(in == NULL){

printf(无法在行%d上分配内存,__LINE__);

返回1;

}

else {

返回0;

}

}


void read_name(char * p_buf,const int size){

char * p_nl;

int ch;

int malloc_check(char * in);


printf("输入描述:");

fflush(stdout);

/ *如果有一些输入* /

if(fgets(p_buf,(size -1),stdin)!= NULL){

/ * malloc p_nl的内存(一个字符)* /

p_nl = malloc(1); / *我们需要它来摆脱新行* /

ch = malloc_check(p_nl); / *检查malloc状态* /

if(ch!= 1){/ *即malloc返回成功* /

if((p_nl = strchr(p_buf,'') \ n''))!= NULL){

* p_nl =''\'''; / *摆脱新行* /

}

}

}

}


/ *我在这里遇到的唯一一个问题,至少我知道的是,我的b $ b无法弄清楚如何正确处理EOF中的问题。这个上下文

函数。

* /


然后在主要的()


char * p_buf;

char * p_input;

void read_name(char *,const int);

int malloc_check(char * in);

int ch;


p_buf = malloc(10); / * malloc内存为p_buf * /

ch = malloc_check(p_buf); / *检查是否成功* ​​/

如果(ch!= 1){/ *如果是,请执行... * /

read_name(p_buf,10);

p_input = malloc(10); / * malloc内存为p_input * /

如果(ch!= 1){/ *如果成功的情况...... * /

strcpy(p_input,p_buf); / *将p_buf的内容复制到p_item * /

免费(p_buf); / *免费内存* /

}

printf("%s",p_input);

}


功能正常,我没有编译错误,但输入项目名称后

是插入新行然后如果我按回车键,则提示输入

说明出现。所以我肯定有回车/新线

处理不好。


其他的是,我只想存储字符串的长度我希望(并且剩下超过长度的剩余部分),但是如果我输入更长的时间,则不会存储任何内容。我怀疑EOF没有被正确处理




希望,这段代码能得到更好的响应:)


svata

So I rewrote it:

/* function to check malloc */
int malloc_check(char *in){
if (in == NULL ) {
printf("Failed to allocate memory on line %d", __LINE__);
return 1;
}
else {
return 0;
}
}

void read_name( char *p_buf, const int size) {
char *p_nl;
int ch;
int malloc_check(char *in);

printf("Enter the description: ");
fflush(stdout);
/* if there is some input */
if (fgets(p_buf, (size -1), stdin) != NULL){
/* malloc memory for p_nl ( one char ) */
p_nl = malloc(1); /* we need it to get rid of new line */
ch = malloc_check(p_nl); /* check malloc status */
if ( ch != 1 ){/* ie malloc returned success */
if (( p_nl = strchr(p_buf, ''\n'')) != NULL ){
*p_nl = ''\0''; /* get rid of new line */
}
}
}
}

/* the only problem I have here, of at least I''m aware of, is that I
can''t figure out how to correctly handle EOF in the context of thsi
function.
*/

and then in the main()

char *p_buf;
char *p_input;
void read_name(char *, const int);
int malloc_check(char *in);
int ch;

p_buf = malloc(10); /* malloc memory for p_buf */
ch = malloc_check(p_buf); /* check if it succeded */
if ( ch != 1 ) {/* if yes, do... */
read_name(p_buf, 10);
p_input = malloc(10);/* malloc memory for p_input */
if ( ch != 1 ) {/* if the case of success... */
strcpy(p_input, p_buf);/* copy content of p_buf to p_item */
free(p_buf);/* free memory*/
}
printf("%s", p_input);
}

Function works, I have no compile errors, but after entering item name
is "a new line inserted" and then if I press enter, then promp "Enter
the description appears". So I''m sure there is carriage return/new line
handled in bad way.

Other thing is, that I would like to store only the lenght of string I
wish( and drop the rest what exceeds the lenght ), but if I enter
longer one, nothing is stored at all. I suspect EOF not being handled
properly.

Hope, this code gets better response from you :)

svata


这篇关于指针作为函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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