我理解指针吗? [英] Do I understand pointers?

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

问题描述

我正在教自己C乐趣。我写了下面列出的litle程序

来转换rot13文本。它一次读取一个字符并通过指针转换




常量字符*字母表示字母。我从字母地址中减去从strchr返回的指针

,以获得字母表中的位置

,然后rot13它。我的问题是,这是安全的吗?

合法吗? (它可以在我的Windows机器BTW上工作。)我用Google搜索程序来执行此操作,他们大多只是从输入中减去一个,我收集了b $ b是仅限ASCII - 我的程序是否更便携?


你会推荐另类方法吗?


非常感谢,< br $>
Rob Morris


#include< stdio.h>

#include< string.h>


int main(无效)

{

int in,out;

char * loc;

char * letters =" AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTt UuVvWwXxYyZz" ;;


while((in = getchar())!= EOF){

loc = strchr(letters,in);

if(loc!= NULL)

out = *(((loc-letters + 26)%52)+ letters);

其他

out = in;

putchar(out);

}

返回0;

}

Hi, I''m teaching myself C for fun. I wrote the litle program listed
below to convert rot13 text. It reads one char at a time and converts
it via pointers.

The constant char* letters holds the alphabet. I subract the pointer
returned from strchr from the address of letters to get the location
within the alphabet, then rot13 it. My question is, is this safe and
legal? (It works on my windows machine BTW.) I googled for programs to
do this and they mostly just subtracted ''a'' from the input, which I
gather is ASCII only - so is my program any more portable?

Would you recommed another method for things like this?

Many thanks,
Rob Morris

#include <stdio.h>
#include <string.h>

int main (void)
{
int in, out;
char *loc;
char *letters="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTt UuVvWwXxYyZz";

while ((in = getchar()) != EOF) {
loc = strchr(letters, in);
if (loc != NULL)
out = *(((loc-letters+26)%52)+letters);
else
out = in;
putchar(out);
}
return 0;
}

推荐答案

Rob Morris< ro *********** @ durham.ac.uk>写道:
Rob Morris <ro***********@durham.ac.uk> writes:
我在教自己C语言很有趣。我写了下面列出的litle程序来转换rot13文本。它一次读取一个字符并通过指针转换它。

常量char *字母保存字母表。


我对你的程序的唯一(次要)批评是你没有

告诉编译器这些指针指向不可修改的东西,

即我会推荐


const char * loc;

const char * letters =" AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxY yZz" ;;


由于后一个指针本身永远不会改变它的值,你也可以

const-qualify指针本身:


const char * const letters =" AaBb ..." ;;

我从字母地址
中减去strchr返回的指针,得到字母表中的位置,然后rot13它。我的问题是,这是安全合法的吗? (它适用于我的Windows机器BTW。)


是的,这是安全有效的。如果他们将

指向同一个对象,你可以减去两个指针。在这种情况下,当你指向

时,两者都指向相同的字符串文字

(对其初始元素为`letter'','loc'为某处'为'it)。减去它们。

我用Google搜索程序来执行此操作,它们大多只是从输入中减去''a''
,我收集的只是ASCII


这不仅仅是ASCII,但它假设这些字母是连续的,并且是b $ b和订购的。对于ASCII和可能的其他编码就是这种情况,

但是还有其他编码,这是不正确的。无论如何,

C标准并不保证

字母的连续性和顺序,所以在便携式编程中不应该依赖它。

- 我的程序是否更便携?


是的。

你会为这样的事情推荐另一种方法吗?


不,这个没问题。

#include< stdio.h>
#include< string.h>

int main(无效)
{int / out;
char * loc;
char * letters =" AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTt UuVvWwXxYyZz" ;;

while((in = getchar())!= EOF){
loc = strchr(letters,in);
if(loc!= NULL)
out = *( ((loc-letters + 26)%52)+ letters);

out = in;
putchar(out);
}
返回0;
}
Hi, I''m teaching myself C for fun. I wrote the litle program listed
below to convert rot13 text. It reads one char at a time and converts
it via pointers.

The constant char* letters holds the alphabet.
The only (minor) criticism I have about your program is that you did not
tell the compiler that these pointers point to something unmodifiable,
i.e. I would recommend

const char *loc;
const char *letters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxY yZz";

Since the latter pointer itself never changes its value, you could also
const-qualify the pointer itself:

const char *const letters = "AaBb...";
I subract the pointer returned from strchr from the address of letters
to get the location within the alphabet, then rot13 it. My question
is, is this safe and legal? (It works on my windows machine BTW.)
Yes, that''s safe and valid. You can subtract two pointers if they point
to the same object. In this case, both point to the same string literal
(`letter'' to its initial element, `loc'' somewhere "into" it) when you
subtract them.
I googled for programs to do this and they mostly just subtracted ''a''
from the input, which I gather is ASCII only
That''s not ASCII only, but it assumes that the letters are consecutive
and ordered. This is the case for ASCII and possibly other encodings,
but there are still other encodings for which it isn''t true. Anyway,
the C standard doesn''t guarantee consecutiveness and order of the
letters, so one shouldn''t rely on it in portable programming.
- so is my program any more portable?
Yes.
Would you recommed another method for things like this?
No, this one is fine.
#include <stdio.h>
#include <string.h>

int main (void)
{
int in, out;
char *loc;
char *letters="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTt UuVvWwXxYyZz";

while ((in = getchar()) != EOF) {
loc = strchr(letters, in);
if (loc != NULL)
out = *(((loc-letters+26)%52)+letters);
else
out = in;
putchar(out);
}
return 0;
}




Martin

-

, - 。 Martin Dickopp,德国德累斯顿,=, - _-。 =。

/, - ) http://www.zero -based.org/ ((_ /)oo(\_))

\` - ''` - ''(。)` - ''

` - 。 Debian,GNU操作系统的一种变体。 \ _ /



Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-'' `-''(. .)`-''
`-. Debian, a variant of the GNU operating system. \_/


Rob Morris写道:
Rob Morris wrote:

我在教自己C语言很有趣。我写了下面列出的litle程序来转换rot13文本。它一次读取一个字符并通过指针转换它。

常量char *字母保存字母表。我从字母的地址中减去从strchr返回的指针
来获取字母表中的位置,然后rot13它。我的问题是,这是否安全且合法? (它适用于我的Windows机器BTW。)我搜索程序来执行此操作,它们大多只是从输入中减去一个,我收集的只是ASCII - 所以我的程序也是如此还有便携式吗?

你会为这样的事情推荐另一种方法吗?

非常感谢,罗伯莫里斯

#include< ; stdio.h>
#include< string.h>

int main(无效)
{int / out; out;
char * loc;
char * letters =" AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTt UuVvWwXxYyZz" ;;

while((in = getchar())!= EOF){
loc = strchr(letters,in) ;
if(loc!= NULL)
out = *(((loc-letters + 26)%52)+ letters);

out = in;
putchar(out);
}
返回0;
}

Hi, I''m teaching myself C for fun. I wrote the litle program listed
below to convert rot13 text. It reads one char at a time and converts
it via pointers.

The constant char* letters holds the alphabet. I subract the pointer
returned from strchr from the address of letters to get the location
within the alphabet, then rot13 it. My question is, is this safe and
legal? (It works on my windows machine BTW.) I googled for programs to
do this and they mostly just subtracted ''a'' from the input, which I
gather is ASCII only - so is my program any more portable?

Would you recommed another method for things like this?

Many thanks,
Rob Morris

#include <stdio.h>
#include <string.h>

int main (void)
{
int in, out;
char *loc;
char *letters="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTt UuVvWwXxYyZz";

while ((in = getchar()) != EOF) {
loc = strchr(letters, in);
if (loc != NULL)
out = *(((loc-letters+26)%52)+letters);
else
out = in;
putchar(out);
}
return 0;
}




Rob,


我建立了你的程序并提供了以下输入

-


AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrS sTtUuVvWwXxYy Zz


并收到以下输出 -


NnOoPpQqRrSsTtUuVvWwXxYyZzAaBbCcDdEeFfGgHhIiJjKkll Mm


我会的说你实现了目标。

你所做的一切都是,恕我直言,相当

合法便携。


我还要说你的理解

远远超过了指针,而且有一天你会成为一个C程序员的b $ b $

你有什么编程背景

来自?


BTW,我使用gcc在Sun Solaris 8上构建它。

Stephen



Rob,

I built your program and provided
the following input -

AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYy Zz

and received the following output -

NnOoPpQqRrSsTtUuVvWwXxYyZzAaBbCcDdEeFfGgHhIiJjKkLl Mm

I''d say you achieved your objective.
Everything you''ve done is, IMHO, quite
legal and portable.

I''d also say that your understanding
far exceeds pointers, and you''ll be
one hell-of-a C programmer someday.
What programming background are you
comming from?

BTW, I built it on Sun Solaris 8 using gcc.
Stephen


Stephen L.写道:
Stephen L. wrote:
Rob Morris写道:
Rob Morris wrote:
我是'我教自己C的乐趣。我写了下面列出的litle程序来转换rot13文本。它一次读取一个字符并通过指针转换它。

常量char *字母保存字母表。我从字母的地址中减去从strchr返回的指针
来获取字母表中的位置,然后rot13它。我的问题是,这是否安全且合法? (它适用于我的Windows机器BTW。)我搜索程序来执行此操作,它们大多只是从输入中减去一个,我收集的只是ASCII - 所以我的程序也是如此还有便携式吗?

你会为这样的事情推荐另一种方法吗?

非常感谢,罗伯莫里斯

#include< ; stdio.h>
#include< string.h>

int main(无效)
{int / out; out;
char * loc;
char * letters =" AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTt UuVvWwXxYyZz" ;;

while((in = getchar())!= EOF){
loc = strchr(letters,in) ;
if(loc!= NULL)
out = *(((loc-letters + 26)%52)+ letters);

out = in;
putchar(out);
}
返回0;
}
Hi, I''m teaching myself C for fun. I wrote the litle program listed
below to convert rot13 text. It reads one char at a time and converts
it via pointers.

The constant char* letters holds the alphabet. I subract the pointer
returned from strchr from the address of letters to get the location
within the alphabet, then rot13 it. My question is, is this safe and
legal? (It works on my windows machine BTW.) I googled for programs to
do this and they mostly just subtracted ''a'' from the input, which I
gather is ASCII only - so is my program any more portable?

Would you recommed another method for things like this?

Many thanks,
Rob Morris

#include <stdio.h>
#include <string.h>

int main (void)
{
int in, out;
char *loc;
char *letters="AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTt UuVvWwXxYyZz";

while ((in = getchar()) != EOF) {
loc = strchr(letters, in);
if (loc != NULL)
out = *(((loc-letters+26)%52)+letters);
else
out = in;
putchar(out);
}
return 0;
}



Rob,

我建立了你的程序并提供以下输入 -

AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYy Zz

并收到以下输出 -

NnOoPpQqRrSsTtUuVvWwXxYyZzAaBbCcDdEeFfGgHhIiJjKkll Mm

我会说你实现了目标。
你所做的一切都是,恕我直言,相当合法和便携。

我也会说你的理解远远超过了指针,你有一天会成为一名C程序员。
你是从什么编程背景来的?

顺便说一句,我使用gcc在Sun Solaris 8上构建它。

Stephen


Rob,

I built your program and provided
the following input -

AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYy Zz

and received the following output -

NnOoPpQqRrSsTtUuVvWwXxYyZzAaBbCcDdEeFfGgHhIiJjKkLl Mm

I''d say you achieved your objective.
Everything you''ve done is, IMHO, quite
legal and portable.

I''d also say that your understanding
far exceeds pointers, and you''ll be
one hell-of-a C programmer someday.
What programming background are you
comming from?

BTW, I built it on Sun Solaris 8 using gcc.
Stephen




哎呀,谢谢。到目前为止,我的编程背景是机械工程学位所需的最低金额。百线节目。


感谢您的批评。我希望有一天能够达到这样的地步:

指针不再吓到我了。


OTOH我很想转向黑暗的一面并学习windows

API,因此可移植性将成为我的一个外国概念。哦,好吧:)



Gee, thanks. My programming background so far has been the barest
amount needed for a mechanical engineering degree. Hundred-line programs.

Thanks for the critique. I hope to someday reach the point where
pointers no longer scare me.

OTOH I''m quite tempted to turn to the dark side and learn the windows
API, whereupon portability will become a foreign concept to me. Oh well :)


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

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