指针概念 [英] pointer concepts

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

问题描述




我是新手。我需要清除我的指针概念。


当我使用char * str时,下面的代码给出了分段错误;

int main()

{

char * str;

printf("输入字符串以反转\ n);

scanf(" ;%s",& str);

strlen(str);

}


如果我使用char str [ 20]它运行正常。

int main()

{

char str [20];

printf (输入一个字符串以反转\ n);

scanf("%s",& str);

strlen(str);

}


你能解释一下原因吗?


谢谢

Hi,

I''m a newbie. I need to clear my pointer concepts.

the code below gives segmentation fault when i use char* str;
int main()
{
char *str;
printf("Enter a string to reverse\n");
scanf("%s",&str);
strlen(str);
}

if I use char str[20] it runs fine.
int main()
{
char str[20];
printf("Enter a string to reverse\n");
scanf("%s",&str);
strlen(str);
}

Can you please explain why?

Thanks

推荐答案

新手写道:


我是新手。我需要清除我的指针概念。

下面的代码在我使用char * str时给出了分段错误;
int main()
使用:int main(void){
char * str;
printf("输入一个字符串以反转\ n);
scanf("%s",& str);
这就是问题所在。 str指向内存中的某个地方

但你不知道那个区域是否属于你的b $ b属于你。您需要分配内存(动态)。

如果您阅读的字符多于您分配的
字符,则可能会再次遇到问题。的strlen(STR);
返回一些东西。 }

如果我使用char str [20]它运行正常。
嗯,是的,因为你的str [20]会自动分配给你b $ b,你要告诉它你想要多少内存

。要动态分配内存,您需要使用指针并为其分配内存(malloc& co)。 int main()
int main(void){
char str [20];
printf("输入字符串以反转\ n);
scanf(" ;%s",& str);
strlen(str);
返回一些东西}

你能解释一下原因吗?
Hi,

I''m a newbie. I need to clear my pointer concepts.

the code below gives segmentation fault when i use char* str;
int main() use: int main(void) {
char *str;
printf("Enter a string to reverse\n");
scanf("%s",&str); Here''s the problem. str points to somewhere in memory
but you have no idea where and whether or not that area
belongs to you. You need to allocate memory (dynamically).
You can run into the problem again if you read more
characters than you allocated. strlen(str); return something. }

if I use char str[20] it runs fine. Well, yes, because you str[20] is automatically
allocated and you tell it exactly how much memory
you want. To dynamically allocate memory you need
to use a pointer and allocate memory for it (malloc & co). int main() int main(void) {
char str[20];
printf("Enter a string to reverse\n");
scanf("%s",&str);
strlen(str); return something }

Can you please explain why?



就是这样。

-

Ioan - Ciprian Tandau

tandau _at_ freeshell _dot_ org(希望现在还不算太晚)

(......它仍然是作品......)


That''s it.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it''s not too late)
(... and that it still works...)


Nelu写道:
新手写道:
int main()使用:int main (void)
int main() use: int main(void)
{
char * str;
printf("输入字符串以反转\ n);
scanf("%s" ,& str);
{
char *str;
printf("Enter a string to reverse\n");
scanf("%s",&str);


这就是问题所在。 str指向记忆中的某个地方
但你不知道那个区域是否属于你。你需要动态分配内存。
如果你读的字符多于你分配的字符,你可能会再次遇到问题。


Here''s the problem. str points to somewhere in memory
but you have no idea where and whether or not that area
belongs to you. You need to allocate memory (dynamically).
You can run into the problem again if you read more
characters than you allocated.




另一个问题,就像没有为字符串分配任何空间一样大,

scanf("%s",...)需要`char *`,你给它`char **' 。你用
(OP)使用`& str`产生`str`变量的地址,/ not /它指向的

字符串(即使存在一个) 。


你似乎没有足够高的编译器警告设置,或者你没有b $ b忽略你得到的那个。我的gcc抱怨...


如果您只是快速测试符合sizeof(char *)的字符串

,那么也可以屏蔽该错误在你的系统上(当你试图使用现在破坏的指针时,它会破坏

)。



Another problem, as big as not allocating any space for the string, is
that scanf("%s",...) requires `char*`, and you give it `char**`. You
(OP) used `&str` which yields the address of `str` variable, /not/ the
string it points to (even if one exists).

You don''t seem to have high enough compiler warning setting, or you
ignored the one you got. My gcc complained...

The badness can be masked also if you just quickly test with the string
that will fit into the sizeof(char*) on your system (it would break
later, when you try to use now broken pointer).

strlen(str );
strlen(str);


返回一些东西。


return something.

}
如果我使用char str [20]它运行正常。
}

if I use char str[20] it runs fine.


好吧,是的,因为str [20]会被自动分配,你可以告诉它你想要多少内存。要动态分配内存,你需要使用指针并为它分配内存(malloc& co)。


Well, yes, because you str[20] is automatically
allocated and you tell it exactly how much memory
you want. To dynamically allocate memory you need
to use a pointer and allocate memory for it (malloc & co).

int main()
int main()


int main(void )


int main(void)

{
char str [20];
printf("输入字符串以反转\ n);
scanf("%s" ,& str);
strlen(str);
{
char str[20];
printf("Enter a string to reverse\n");
scanf("%s",&str);
strlen(str);


返回一些东西


return something

}

你能解释一下原因吗?
}

Can you please explain why?




在这里,你有完全相同的问题。使用`str`而不是

`& str`用于scanf()。不要被其他类型弄糊涂。再次访问关于指针和C字符串的

教科书部分。

就是这样。



Here, you have exactly the same problem as above. Use `str` and not
`&str` for scanf(). Don''t get confused by other types. Revisit your
textbook sections on pointers and C strings.
That''s it.




的确如此。


-

BR,弗拉基米尔


好的,现在让我们看看四个维度在黑板上。

- Joy博士



Indeed.

--
BR, Vladimir

"OK, now let''s look at four dimensions on the blackboard."
-- Dr. Joy


elu写道:
你如果你读的字符多于你分配的字符,可能会再次遇到问题。
You can run into the problem again if you read more
characters than you allocated.




我同意你的意见。

但即使您输入超过10个字符,下面的代码也能正常工作?

为什么?


#include< cstdio>

#include< cstring>

#include< iostream>

使用命名空间std;

int main()

{

char * str;

str = new char [10];

printf("输入字符串以反转\ n");

scanf("%s",str);

cout<<的strlen(STR)<<结束;

系统(暂停);

返回0;

}



I agree with you on that.
But the codes below works even if you input more than 10 characters?
Why?

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char *str;
str = new char[10];
printf("Enter a string to reverse\n");
scanf("%s",str);
cout << strlen(str)<< endl;
system("pause");
return 0;
}


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

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