另一个理解指针问题 [英] Another Understanding Pointers Question

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

问题描述

大家好,

我看过Rob Morris的帖子,并且认为我会仔细检查

,我正在以正确的方式使用指针。所以我写了以下

字符串函数进行测试。我知道soem可以使用

标准库来实现,但我只是想测试编写我自己的函数。

他们工作正常,但我想要反馈一些你可以看到的任何问题

与他们等


#include< stdio.h>

#include< stdlib。 h>

#include< string.h>


char * left(char * string,int count)

{

char * p;

int i;

p = malloc((count * sizeof(char)+1));

if(!p){

printf(无法分配内存\ n);

退出(1);

}

for(i = 0; i< = count -1; i ++){

p [i] = string [i];

}

p [i ++] =''\ 0'';


返回(p);


}


char * right(char * string,int count)

{

char * p;

int len,i,j = 0;

p = malloc((count * sizeof(char)+1));

if(!p ){

printf( 无法分配内存\ n;)

退出(1);

}

len = strlen(string);

for(i =(len - count);我< = len; i ++){

p [j] = string [i];

j ++;

}


p [j ++] =''\ 0'';

返回(p);


}


char * chreplace(char * string,int count,char rep)

{

char * p;

p = malloc((sizeof( string)+1));

if(!p){

printf(无法分配内存\ n);

退出(1);

}

count--;

strcpy(p,string);

p [ count] = rep;


return(p);

}


char * section(char * string ,int from,int to)

{

char * p;

int i,j = 0;

p = malloc(((to - from)* sizeof(char)+1));

if(!p){

printf("无法分配记忆\ n;;

退出(1);

}

for(i = from; i< = to ; i ++){

p [j] = string [i];

j ++;

}

p [j ++ ] =''\ 0'';


return(p );

}

int main(无效)

{

char blah [] =" abcdefghijklm" ;

char * test;

char * test2;

char * test3;

char * test4;


test = left(等等,10);

test2 =对(等等,10);

test3 = chreplace(blah) ,2,''Q'');

test4 = section(blah,4,10);

puts(test);

puts(test2);

puts(test3);

puts(test4);

返回0;

}


欢迎评论和改进,如果合适的火焰。

-

------
perl -e''printf"%silto%c%sck%ccodegurus%corg%c"," ma",58," mi,64,

46,10;''

Hi everyone,
I seen the post by Rob Morris, and thought that I would double check
that I was using pointers in the correct way. So I written the following
string functions to test. I know soem can be iumplimented using the
standard libary, but I just wanted to test writing my own functions.
They work ok, but I would like some feed back on any issues you can see
with them etc

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

char *left(char *string, int count)
{
char *p;
int i;
p = malloc((count * sizeof(char)+1));
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
for(i = 0; i <= count -1; i++) {
p[i] = string[i];
}
p[i++] = ''\0'';

return(p);

}

char *right(char *string, int count)
{
char *p;
int len, i, j = 0;
p = malloc((count * sizeof(char)+1));
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
len = strlen(string);
for(i = (len - count); i <= len; i++){
p[j] = string[i];
j++;
}

p[j++] = ''\0'';
return(p);

}

char *chreplace(char *string, int count, char rep)
{
char *p;
p = malloc((sizeof(string)+1));
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
count--;
strcpy(p, string);
p[count] = rep;

return(p);
}

char *section(char *string, int from, int to)
{
char *p;
int i, j = 0;

p = malloc(((to - from) * sizeof(char)+1));
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
for( i = from; i <= to; i++) {
p[j] = string[i];
j++;
}
p[j++] = ''\0'';

return(p);
}
int main(void)
{
char blah[] = "abcdefghijklm";
char *test;
char *test2;
char *test3;
char *test4;

test = left(blah, 10);
test2 = right(blah, 10);
test3 = chreplace(blah, 2, ''Q'');
test4 = section(blah, 4, 10);
puts(test);
puts(test2);
puts(test3);
puts(test4);
return 0;
}

Comments and improvements are welcome, flames to if appropriate.
--
------
Materialised

perl -e ''printf "%silto%c%sck%ccodegurus%corg%c", "ma", 58, "mi", 64,
46, 10;''

推荐答案

2004年5月10日星期一15:11: 42 +0100,物化< Ma ********** @ privacy.net>

写道:
On Mon, 10 May 2004 15:11:42 +0100, Materialised <Ma**********@privacy.net>
wrote:
大家好,字符串函数来测试。我知道soem可以使用
标准库来实现,但我只是想测试自己编写的函数。
它们工作正常,但是我想要一些反馈你可以看到的任何问题
和他们一起等等。


好​​的,我会指出一些我看到的东西。我确定还有其他很多其他的东西会被b $ b抓住。

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

char * left(char * string,int count)


因为你不是修改字符串,将其定义为指向const的指针:


char * left(const char * string,int count)

(或者,Dan Saks样式)

char * left(char const * string,int count)


{
char * p;
int i;
p = malloc((count * sizeof(char)+1));


我不知道是否有团体共识或者是否值得

麻烦说''sizeof(char) ''当值总是1.个人而言,为了简单起见,我将b $ b留下来:

p = malloc(count + 1);


除了一件事:你不知道你是否需要所有这些空间。

如果有人指定计数为10000怎么办?你不是更好吗

搞清楚,首先,你需要/真正/需要多少空间,只需要分配那个?b / b $ b / b >
if(!p){
printf(无法分配内存\ n);
退出(1);
}

for(i = 0; i< = count -1; i ++){
p [i] = string [i];


另一个问题,如果数量太大,你会继续走到最后这个源数组的
(未定义的行为)。 />
}
p [i ++] =''\ 0'';

返回(p);

}

char * right(char * string,int count)


所有与上面相同的问题(好吧,现在如果数量太大你将会是

试图读出内存/之前/字符串而不是之后......)

{
char * p;
int len,i,j = 0;
p = malloc((count * sizeof(char)+1));
if(!p){
printf(无法分配内存\ n);
exit(1);
}
len = strlen(string);
for(i =(len - count); i< = len; i ++){
p [j] = string [i];
j ++;
}

p [j ++] =''\ 0'';
返回( p);


char * chreplace(char * string,int count,char rep)
blah blah const blah blah ... {
char * p;
p = malloc((sizeof(string) +1));
if(!p){
printf(无法分配内存\ n);
退出(1);
}
count - ;
strcpy(p,string);
p [count] = rep;
没有理智的检查(再次)。
return(p);
那里不需要parens。 return不是函数。

}

char * section(char * string,int from,int to)
const,

从/到的理智检查

{
char * p;
int i,j = 0;

p = malloc(( - from)* sizeof(char)+1));
if(!p){
printf(无法分配内存\ n);
exit(1);
}
for(i = from; i< = to; i ++){
p [j] = string [i];
j ++;
}
p [j ++] =''\ 0'';

返回(p);
}
int main(无效)
{
char blah [] =" abcdefghijklm";
char * test;
char * test2;
char * test3;
char * test4;
test = left(blah,10);
test2 = right(blah,10);
test3 = chreplace(blah,2,''Q'');
test4 = section( blah,4,10);
put(test);
puts(test2);
puts(test3);
puts(test4);
返回0;
}

欢迎提出意见和改进,f如果合适的话,可以说谎。
Hi everyone,
I seen the post by Rob Morris, and thought that I would double check
that I was using pointers in the correct way. So I written the following
string functions to test. I know soem can be iumplimented using the
standard libary, but I just wanted to test writing my own functions.
They work ok, but I would like some feed back on any issues you can see
with them etc
Okay, I''ll point out some things I see. I''m sure there''s much else others
will catch.

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

char *left(char *string, int count)
Since you''re not modifying string, define it as pointer-to-const:

char *left(const char *string, int count)
(or, Dan Saks style)
char *left(char const *string, int count)

{
char *p;
int i;
p = malloc((count * sizeof(char)+1));
I don''t know if there''s a group consensus or not on whether it is worth the
trouble to say ''sizeof(char)'' when the value is always 1. Personally, I''d
leave it out for the sake of simplicity:
p = malloc(count + 1);

Except for one thing: you don''t know if you''ll need all of that space.
What if someone specifies count as 10000? Wouldn''t you be better of
figuring out, first, how much space you''re /really/ going to need and just
allocating that?
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
for(i = 0; i <= count -1; i++) {
p[i] = string[i];
Another problem if count is too big is you''ll keep going right off the end
of the source array here (undefined behavior).
}
p[i++] = ''\0'';

return(p);

}

char *right(char *string, int count)
All the same issues as above (well, now if count is too big you''ll be
trying to read out of memory /before/ string instead of after it...)
{
char *p;
int len, i, j = 0;
p = malloc((count * sizeof(char)+1));
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
len = strlen(string);
for(i = (len - count); i <= len; i++){
p[j] = string[i];
j++;
}

p[j++] = ''\0'';
return(p);

}

char *chreplace(char *string, int count, char rep) blah blah const blah blah...{
char *p;
p = malloc((sizeof(string)+1));
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
count--;
strcpy(p, string);
p[count] = rep; No sanity checking on count (again).
return(p); don''t need parens there. return is not a function.
}

char *section(char *string, int from, int to) const,
sanity checking of from/to
{
char *p;
int i, j = 0;

p = malloc(((to - from) * sizeof(char)+1));
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
for( i = from; i <= to; i++) {
p[j] = string[i];
j++;
}
p[j++] = ''\0'';

return(p);
}
int main(void)
{
char blah[] = "abcdefghijklm";
char *test;
char *test2;
char *test3;
char *test4;

test = left(blah, 10);
test2 = right(blah, 10);
test3 = chreplace(blah, 2, ''Q'');
test4 = section(blah, 4, 10);
puts(test);
puts(test2);
puts(test3);
puts(test4);
return 0;
}

Comments and improvements are welcome, flames to if appropriate.




这应该会让你有点工作...

-leor

-

Leor Zolman --- BD软件--- www.bdsoft .com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器at:
www.bdsoft.com/tools/stlfilt.html


2004年5月10日星期一,Materialized写道:
On Mon, 10 May 2004, Materialised wrote:
大家好,
我看到了Rob Morris发帖,并认为我会仔细检查我是否以正确的方式使用指针。所以我写了以下
字符串函数来测试。我知道soem可以使用
标准库来实现,但我只是想测试自己编写的函数。
它们工作正常,但是我想要一些反馈你可以看到的任何问题
与他们等

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

char * left(char * string,int count)
{
char * p;
int i;


检查输入是个好习惯。如果string == NULL怎么办?如果

count> strlen的(字符串)?当你将字符串

复制到p时,你可以实际检查这个。

p = malloc((count * sizeof(char)+1));


sizeof(char)的值始终为1.这可以简化为:


p = malloc(count + 1);

if(!p){
printf(无法分配内存\ n);
退出(1);
}


我不太可能使用退出失败的函数。你已经从调用函数中取走了所有控制权。

。如果我创建了一个

大量数据然后调用你的函数怎么办?我会非常恼火地失去所有数据的b $ b。如果您返回NULL而不是我可以选择继续,保存并退出或退出。

for(i = 0; i< = count -1; i ++){


我只是想看看:


for(i = 0; i< count; i ++){


看起来更自然。另外,如果计数> strlen的(字符串)?

p [i]是安全的但是字符串[i]?

p [i] = string [i];
}
p [ i ++] =''\''';


为什么增加我?这可能很容易:


p [i] =''\ 0'';

return(p);
}


就指针使用而言,这很好。关于代码

的一些评论会很好。当我在malloc中看到计数+ 1时,我想知道为什么

+1。我以为它是为空的角色而没有评论我

无法确定。当有人使用你的功能我必须阅读并且

了解代码以确保我正确使用它。有一个

评论在顶部解释用法会很好。

char * right(char * string,int count)
{
char * p;
int len,i,j = 0;


与之前的功能相同。检查你的输入。

p = malloc((count * sizeof(char)+1));


与之前的功能相同。

if(!p){
printf(无法分配内存\ n);
exit(1);
}


与上一个函数相同。

len = strlen(string);
for( i =(len - count); i< = len; i ++){
p [j] = string [i];
j ++;
}


好​​。可能更好用:


for(j = 0; i =(len - count); j< count; i ++,j ++)

p [j ] = string [i];


首先,我在这里初始化j。这样我肯定我初始化它并且函数顶部之间没有代码

,这里改变了它。


其次,使用j索引退出循环对我来说似乎是正确的。这只是个人偏好。


第三,使用逗号运算符并使代码更短。我喜欢

尽可能多地放在一个屏幕上而不会失去可读性。


最后,我们确信p [j]是安全的。串[i]怎么样?你可能

也有一个数组超出范围。

p [j ++] =''\''';


再次,为什么增加j?

返回(p);
}


全部 - 总之,很好地利用了指针。

char * chreplace(char * string,int count,char rep)
{
char * p;


检查输入。

p = malloc((sizeof(string)+1));


第一个严重错误。 sizeof(字符串)将是sizeof(char *)。你

不想要指针的大小。您希望指向的字符串大小为

。这应该是:


p = malloc(strlen(string)+1);

if(!p){
printf(" Can not)分配内存\ n");
退出(1);
}


与以前的功能相同。

count- - ;


为什么递减计数并不是很明显。我可以阅读你的

代码,并确定count参数是1索引的,你是

递减它以使其0索引但是我应该阅读你的代码

来计算出来。

strcpy(p,string);
p [count] = rep;


你实际上可以组合两行并且有:


p [ - count] = rep;

返回(p);
}


我喜欢这样一个事实,即使用户传递一个文字字符串这个

函数仍然有用。

char * section(char * string,int from,int to)
{
char * p;
int i,j = 0;


检查输入。

p = malloc(((to - from)* sizeof(char)+1));


sizeof(char)是多余的。如果来自< 0?它可能发生。检查

输入。

if(!p){
printf(无法分配内存\ n);
退出(1 ); $
}


与之前的函数相同。

for(i = from; i< = to; i ++){
p [j] = string [i];
j ++;
}


for(j = 0,i = from; i< = to; i ++,j ++)

p [j] = string [i];

p [j ++] =''\''';


为什么增加j?

返回(p);
}


还不错但仍然有一个严重的错误。

int main(无效)
{char / blah [] =" abcdefghijklm" ;;
char * test;
char * test2;
char * test3;
char * test4;

test = left(blah,10);
test2 = right(blah,10);
test3 = chreplace(blah,2,''Q'');
test4 = section(blah,4,10);
puts(test);
puts(test2) ;
put(test3);
puts(test4);


在功能中分配大量内存但绝对不能调用

free()。这将是一次内存泄漏。


免费(test4);

免费(test3);

免费(test2);

免费(测试);

返回0;
}
欢迎评论和改进,如果合适的火焰。
-
------
具体化

perl -e''printf"%silto%c%sck%ccodegurus%corg%c"," ma", 58,mi,64,
46,10;''
Hi everyone,
I seen the post by Rob Morris, and thought that I would double check
that I was using pointers in the correct way. So I written the following
string functions to test. I know soem can be iumplimented using the
standard libary, but I just wanted to test writing my own functions.
They work ok, but I would like some feed back on any issues you can see
with them etc

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

char *left(char *string, int count)
{
char *p;
int i;
It is a good habit to check your inputs. What if string == NULL? what if
count > strlen(string)? You can actually check this as you copy the string
over to p.
p = malloc((count * sizeof(char)+1));
The value of sizeof(char) is always 1. This can be simplified to:

p = malloc(count+1);
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
I would be less likely to use a function that exits on failure. You have
taken away all control from the calling function. What if I had created a
lot of data then called your function? I''d be very annoyed to lose all
that data. If you returned NULL instead then I have the option to
continue, save and quit or just quit.
for(i = 0; i <= count -1; i++) {
I just like to see:

for(i = 0; i < count; i++) {

it seems more natural. Additionally, what if count > strlen(string)? The
p[i] is safe but is the string[i]?
p[i] = string[i];
}
p[i++] = ''\0'';
Why increment i? This could have just as easily been:

p[i] = ''\0'';
return(p);
}
As far as pointer usage goes, this to good. Some comments on the code
would be good. When I saw the count+1 in the malloc I was wondering why
the +1. I assumed it was for the null character but without comments I
couldn''t be sure. As someone using your function I''d have to read and
understand the code to be sure I was using it correctly. Having a
commented at the top explaining usage would be good.
char *right(char *string, int count)
{
char *p;
int len, i, j = 0;
Same as previous function. Check your inputs.
p = malloc((count * sizeof(char)+1));
Same as previous function.
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
Same as previous function.
len = strlen(string);
for(i = (len - count); i <= len; i++){
p[j] = string[i];
j++;
}
Good. Might be better using:

for(j = 0; i = (len - count); j < count; i++, j++)
p[j] = string[i];

First, I initialize j here. This way I''m sure I initialized it and no code
between the top of the function and here changed it.

Second, using the j index to exit the loop just seems right to me. This is
just a personal preference though.

Third, use the comma operator and make the code a little shorter. I like
to put as much on one screen as I can without losing readability.

Finally, we are sure that p[j] is safe. What about string[i]? You might
have an array out of bounds there as well.
p[j++] = ''\0'';
Again, why increment j?
return(p);
}
All-in-all, a good use of pointers.
char *chreplace(char *string, int count, char rep)
{
char *p;
Check the inputs.
p = malloc((sizeof(string)+1));
First serious mistake. The sizeof(string) will be the sizeof(char*). You
don''t want the size of a pointer. You want the size of the string being
pointed to. This should be:

p = malloc(strlen(string)+1);
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
Same thing as previous functions.
count--;
Not immediately obvious why you are decrementing count. I can read your
code and figure out that the count parameter is 1-indexed and you are
decrementing it to make it 0-indexed but I should have to read your code
to figure that out.
strcpy(p, string);
p[count] = rep;
You could actually combine two lines and have:

p[--count] = rep;
return(p);
}
I like the fact that even of the user passing a literal string this
function still works.
char *section(char *string, int from, int to)
{
char *p;
int i, j = 0;
Check the inputs.
p = malloc(((to - from) * sizeof(char)+1));
The sizeof(char) is redundant. What if to-from < 0? It could happen. Check
the inputs.
if(!p){
printf("Cannot allocate memory\n");
exit(1);
}
Same as previous functions.
for( i = from; i <= to; i++) {
p[j] = string[i];
j++;
}
for(j = 0, i = from; i <= to; i++, j++)
p[j] = string[i];
p[j++] = ''\0'';
Why increment j?
return(p);
}
Not bad but still had one serious mistake.
int main(void)
{
char blah[] = "abcdefghijklm";
char *test;
char *test2;
char *test3;
char *test4;

test = left(blah, 10);
test2 = right(blah, 10);
test3 = chreplace(blah, 2, ''Q'');
test4 = section(blah, 4, 10);
puts(test);
puts(test2);
puts(test3);
puts(test4);
Lots of allocating of memory in the functions but absolutely not call to
free(). That would be a memory leak.

free(test4);
free(test3);
free(test2);
free(test);
return 0;
}
Comments and improvements are welcome, flames to if appropriate.
--
------
Materialised

perl -e ''printf "%silto%c%sck%ccodegurus%corg%c", "ma", 58, "mi", 64,
46, 10;''




-

发送电子邮件至:darrell at cs dot toronto dot edu

不要发电子邮件给 vi ************ @ whitehouse.gov


Materialized写道:
Materialised wrote:
大家好,
我看过Rob Morris的帖子,并且认为我会仔细检查我是否以正确的方式使用指针。所以我写了以下
字符串函数来测试。我知道soem可以使用
标准库来实现,但我只是想测试自己编写的函数。
它们工作正常,但是我想要一些反馈你可以看到的任何问题
与他们一起等等


关于你使用int的一个小注释。

据我认为你的字符串函数

永远不会使用负值。

所以你可能要考虑使用size_t。

char * left(char * string,int count)
Hi everyone,
I seen the post by Rob Morris, and thought that I would double check
that I was using pointers in the correct way. So I written the following
string functions to test. I know soem can be iumplimented using the
standard libary, but I just wanted to test writing my own functions.
They work ok, but I would like some feed back on any issues you can see
with them etc
Just a minor note on your use of int.
As far as I think your string functions
will never work with negative values.
So you may want to consider using size_t instead.
char *left(char *string, int count)



char * left(const char * string,size_t count)





祝你好运,
Robert Bachmann

-
Ro * ************@rbdev.net |)|)
http://rb.rbdev.net | \。|)。


char *left(const char *string, size_t count)

etc.

Best regards,
Robert Bachmann
--
Ro*************@rbdev.net |) |)
http://rb.rbdev.net |\.|).


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

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