学习指针 [英] Learning pointers

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

问题描述

有人可以查看我的代码并告诉我你是否看到任何

错误?


========= ===============

#include< stdio.h>

#include< ctype.h> ;

#include< conio.h> //用于使getch()工作

#include< string.h>


int main(无效)

{


//声明变量

// -----------------


字符文本[70];

字符* txtptr;


//提示用户输入文本行

// ----------------------------


printf(" \\ \\ n输入一行文字(最多69个字符):\ n");

fgets(text,sizeof text,stdin);


/ /确保字符串终止符不超过数组中的最后一个

//元素。

// ------------- ----------------------------------------------


txtptr = text;

*(txtptr + strlen(txtptr) - 1)=''\''';


//以大写字符转换和输出文本。

// --------------------------- -------------------------


printf(" \\\
大写的文字行是:\\\
"); <无线电通信/>

txtptr = text;

while(* txtptr!=''\''')

putchar(toupper(* txtptr ++) ));


//以大写字符转换和输出文本。

// -------------- --------------------------------------


printf(" \ n \ n以小写字母行显示:\ n");


txtptr = text;

while(* txtptr!=''\''')

putchar(tolower(* txtptr ++));

putchar(''\ n'');


printf(" \ n");


getch(); //暂停输出


返回0;


} //结束主

Can someone take a look at my code and let me know if you see any
mistakes?

========================

#include <stdio.h>
#include <ctype.h>
#include <conio.h> // Used to make the getch() work
#include <string.h>

int main(void)
{

// Declare variables
// -----------------

char text[70];
char *txtptr;

// Prompt user for line of text
// ----------------------------

printf ("\nEnter a line of text (up to 69 characters):\n");
fgets(text,sizeof text,stdin);

// Ensure that the string terminator is no more than the last
// element in the array.
// -----------------------------------------------------------

txtptr = text;
*(txtptr + strlen(txtptr) - 1) = ''\0'';

// Converts and outputs the text in uppercase characters.
// ----------------------------------------------------

printf ("\nThe line of text in uppercase is:\n");

txtptr = text;
while (*txtptr != ''\0'')
putchar( toupper(*txtptr++) );

// Converts and outputs the text in uppercase characters.
// ----------------------------------------------------

printf ("\n\nThe line of text in lowercase is:\n");

txtptr = text;
while ( *txtptr != ''\0'')
putchar( tolower(*txtptr++));
putchar(''\n'');

printf("\n");

getch(); // Pauses output

return 0;

} // end main

推荐答案

Ma*********@home.com 写道:
有人可以查看我的代码并告诉我你是否看到任何错误?
printf(&\\;输入一行文字(最多69个字符):\ n");
fgets(text,sizeof text,stdin);
//确保字符串终止符不超过数组中的最后一个
//元素。
// ---------------- -------------------------------------------
txtptr = text;
*(txtptr + strlen(txtptr) - 1)=''\''';
Can someone take a look at my code and let me know if you see any
mistakes? printf ("\nEnter a line of text (up to 69 characters):\n");
fgets(text,sizeof text,stdin); // Ensure that the string terminator is no more than the last
// element in the array.
// -----------------------------------------------------------

txtptr = text;
*(txtptr + strlen(txtptr) - 1) = ''\0'';




这不会工作。如果''text''在

结尾处没有包含''\ 0'',那么你就不能使用strlen(),因为它只是计算数字

的字符直到它命中''\0'',所以如果没有一个strlen()不会

能够找出字符串结束的位置。我明白你这是用b * b写这个方式来使用指针但这里很简单


*(txtptr + sizeof text - 1)=''\ 0 '';


就是你所需要的。

问候,Jens

-

_ _____ _____

| || _ _ || _ _ | Je *********** @ physik.fu-berlin.de

_ | | | | | |

| | _ | | | | | | http://www.physik.fu-berlin.de/~toerring

\ ___ / ens | _ | homs | _ | oerring



This won''t work. If ''text'' does not alrealy contain a ''\0'' at the
end you can''t use strlen() because all it does is count the number
of chars until it hits ''\0'', so if there isn''t one strlen() won''t
be able to figure out where the string ends. I understand that you
wrote it this way to use pointers but here a simple

*(txtptr + sizeof text - 1) = ''\0'';

is what you need.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring


>> txtptr = text;
>> txtptr = text;
*(txtptr + strlen(txtptr) - 1)=''\ 0'';
*(txtptr + strlen(txtptr) - 1) = ''\0'';


结尾没有包含''\ 0'',你就不能使用strlen(),因为它所做的就是计算字符的数量,直到它为止点击''\0'',所以如果没有一个strlen()不能确定字符串结束的位置。我明白你用这种方式编写它来使用指针但这里很简单

*(txtptr + sizeof text - 1)=''\''';

是你需要的。
问候,Jens



This won''t work. If ''text'' does not alrealy contain a ''\0'' at the
end you can''t use strlen() because all it does is count the number
of chars until it hits ''\0'', so if there isn''t one strlen() won''t
be able to figure out where the string ends. I understand that you
wrote it this way to use pointers but here a simple

*(txtptr + sizeof text - 1) = ''\0'';

is what you need.
Regards, Jens




=================== ======


嘿Jens,


当我添加该代码时,我收到以下警告:


本地变量''txtptr''未经初始化使用


我应该如何初始化变量?设置它等于零?


这是我的更新代码:


#include< stdio.h>

#include< ctype.h>

#include< conio.h> //用于使getch()工作

#include< string.h>


int main(无效)

{


//声明变量

// -----------------


字符文本[70];

字符* txtptr;


//提示用户输入文本行

// ----------------------------


printf(" \\ \\ n输入一行文字(最多69个字符):\ n");

fgets(text,sizeof text,stdin);


/ /确保字符串终止符不超过数组中的最后一个

//元素。

// ------------- ----------------------------------------------


*(txtptr + sizeof text - 1)=''\''';


//以大写字符转换和输出文本。

// ----------------------------------------- -----------

printf(\ n大写文字行:\ nn);

txtptr = text;

while(* txtptr!=''\''')

putchar(toupper(* txtptr ++));


//以大写字符转换和输出文本。

// ---------------------------- ------------------------


printf(" \ n \ n)小写是:\ n");


txtptr = text;

while(* txtptr!=''\ 0'')

putchar(tolower(* txtptr ++));

putchar(''\ n'');


printf(" \\ \\ n");


getch(); //暂停输出直到用户按下键盘上的按钮

键盘。


返回0;


} //结束主



=========================

Hey Jens,

When I add that code I get the following warning:

local variable ''txtptr'' used without having been initialized

How should I initialize the variable? Just set it equal to zero?

Here is my updated code:

#include <stdio.h>
#include <ctype.h>
#include <conio.h> // Used to make the getch() work
#include <string.h>

int main(void)
{

// Declare variables
// -----------------

char text[70];
char *txtptr;

// Prompt user for line of text
// ----------------------------

printf ("\nEnter a line of text (up to 69 characters):\n");
fgets(text,sizeof text,stdin);

// Ensure that the string terminator is no more than the last
// element in the array.
// -----------------------------------------------------------

*(txtptr + sizeof text - 1) = ''\0'';

// Converts and outputs the text in uppercase characters.
// ----------------------------------------------------

printf ("\nThe line of text in uppercase is:\n");

txtptr = text;
while (*txtptr != ''\0'')
putchar( toupper(*txtptr++) );

// Converts and outputs the text in uppercase characters.
// ----------------------------------------------------

printf ("\n\nThe line of text in lowercase is:\n");

txtptr = text;
while ( *txtptr != ''\0'')
putchar( tolower(*txtptr++));
putchar(''\n'');

printf("\n");

getch(); // Pauses output until the user presses a button on
the keyboard.

return 0;

} // end main


Ma *** ******@home.com 写道:
Ma*********@home.com wrote:
#include< stdio.h>
#include< ctype.h>
#include< conio.h> //用于使getch()工作
#include< string.h>

int main(void)


//声明变量
// -----------------


注意这种评论。他们没有在代码中添加任何内容

,除非他们重申了明显的代码。他们可以把读者混淆成

想到一些非显而易见的事情(毕竟,为什么呢?b
评论?)当没有;因为它们往往会被忽略,所以它们很容易与代码不同步。

最好不要评论_what_已完成,但是在_why_它已经完成了b $ b。每个人都可以知道你在这里声明了变量,但它有时候很有用,可以知道某个特定变量的用途是什么。

用于。

char text [70];
char * txtptr;
printf(&\\;输入一行文字(最多69个字符):\ n");
fgets(text,sizeof text,stdin);


请注意,这并不妨碍用户输入超过69个
字符;其余的将保留在输入流中,等待你的
下一个输入函数。

//确保字符串终止符不超过最后一个
/数组中的/元素。


此评论甚至没有意义。您在下面的任何内容中都没有使用数组中的

最后一个元素。你在

_string_的最后一个元素附近做了一些事情,这是另一回事。并且字符串的最后一个字符

是_always_的null终止符 - 这就是使它成为字符串的原因,而不是

未终止的字符数组。

txtptr = text;
*(txtptr + strlen(txtptr) - 1)=''\''';


你在这里以迂回的方式做的是覆盖字符串终结符之前的最后一个字符

。无论最后一个角色是否有
,你都可以这样做;而且,无论你实际上是什么
字符都是这样做的。

我怀疑你真正想做的就是删除换行符

fgets()倾向于留在字符串中的字符。如果是这样,请注意

此换行符不一定存在:仅当您的用户输入

时才会出现。例如,如果用户输入超过70个字符的
字符串,则可能会丢失 - 前七十个将被放入文本中,

和其余的,包括换行符将保留在输入流中。

您也可以从文件中输入管道输入,并且您读取的

行恰好是文件中的最后一行 - 但不会在换行符中结束
。在这种情况下,结果

字符串中也不会有换行符。

所以你想做的是:

- 检查文本中是否有换行符;

- 如果是,则覆盖它,但如果没有,则不覆盖任何内容。


一种方法是
if((txtptr = strchr(text,''\ n''))* txtptr =''\ 0'';

//转换并以大写字符输出文本。


这不会转换任何内容。它只打印大小相当于

输入。

printf(" \\\
大写的文字行是:\ n");

txtptr = text;
while(* txtptr!=''\\ \\ 0'')
putchar(toupper(* txtptr ++));


首先,toupper()接受一个int,要求其输入在

_unsigned_ char或EOF的范围。如果您的字符恰好已签名,并且

您输入的值为负值,则此调用可能会产生在

一些...创造性的价值。

其次,这是一个for循环的理想场所。


for (txtptr =文本; !* txtptr = \0’ ; txtptr ++)

putchar(toupper((unsigned char)* txtptr);


Ditto用于小写循环。

putchar( ''\ n'');

printf(" \ n");
#include <stdio.h>
#include <ctype.h>
#include <conio.h> // Used to make the getch() work
#include <string.h>

int main(void)
{

// Declare variables
// -----------------
Be careful with this kind of comment. They add nothing to the code
except that they restate the obvious; they can confuse the reader into
thinking something non-obvious is going on (after all, why else
comment?) when there isn''t; and because they tend to get ignored, they
can easily get out of sync with the code.
It''s much better not to comment on _what_ is done, but on _why_ it is
done. Everybody can figure out that you declare variables here, but it''s
sometimes useful to know what a particular variable is intended to be
used for.
char text[70];
char *txtptr; printf ("\nEnter a line of text (up to 69 characters):\n");
fgets(text,sizeof text,stdin);
Note that this doesn''t prevent the user from entering more than 69
characters; the rest will remain in the input stream, waiting for your
next input function.
// Ensure that the string terminator is no more than the last
// element in the array.
This comment does not even make sense. You do nothing below with the
last element in the array. You do something near the last element of the
_string_, which is a different thing. And the last character of a string
is _always_ the null terminator - that''s what makes it a string, not an
unterminated char array.
txtptr = text;
*(txtptr + strlen(txtptr) - 1) = ''\0'';
What you do here, in a roundabout way, is overwrite the last character
before the string terminator. You do this regardless of whether there is
a last character at all; moreover, you do this regardless of what that
character actually is.
What I suspect you really wanted to do is to remove the newline
character which fgets() tends to leave in the string. If so, note that
this newline is not necessarily there: it is only there if your user
entered it. It could be missing if, e.g., the user enters a string of
more than seventy characters - the first seventy will be put into text,
and the rest, including the newline, will remain in the input stream.
It''s also possible that you get your input piped from a file, and the
line you read happens to be the last line in the file - but does not end
in a newline. In that case, there won''t be a newline in the resulting
string, either.
So what you want to do is:
- check that there is a newline in text;
- if so, overwrite it, but if not, overwrite nothing.

One way to do this is

if ((txtptr=strchr(text, ''\n'')) *txtptr=''\0'';
// Converts and outputs the text in uppercase characters.
This converts nothing. It merely prints the upper-case equivalent of the
input.
printf ("\nThe line of text in uppercase is:\n");

txtptr = text;
while (*txtptr != ''\0'')
putchar( toupper(*txtptr++) );
First of all, toupper() takes an int, requires that its input is in the
range of _unsigned_ char, or EOF. If your char happens to be signed, and
you enter a character with a negative value, this call can result in
some... creative values.
Second, this is the perfect place for a for-loop.

for (txtptr=text; *txtptr!=''\0''; txtptr++)
putchar(toupper((unsigned char)*txtptr);

Ditto for the lowercase loop.
putchar(''\n'');

printf("\n");




为什么这两个不同的电话?做同样的事情...


Richard



Why these two different calls? They do exactly the same thing...

Richard


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

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