用C ++学习指针 [英] Learning pointers in C++

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

问题描述






我正在尝试学习如何使用指针作为一个小例子。


我做了:


void main(int argc,char * argv [])

{

char algo [] =" Algo mas";

char * p = algo;

char otra [256] =" " ;;

otra = * p;


printf("%s",otra);

}


我试图给algo分配一个值,然后尝试将algo分配给

指针p,然后尝试将p指向的值赋给otra。 br />

程序编译好,但是当我尝试运行它时,挂机:(


我做错了什么?


提前致谢


J

解决方案

哈维尔写道:




我正在尝试学习如何使用指针作为一个小例子。

我做了:

void main(int argc,char * argv [])
{
char algo [] =" Algo mas" ;;
char * p = algo;
char otra [256] ="" ;;
otra = * p;

printf("%s",otra);
}

我试图给算法分配一个值,然后尝试将算法分配给
po inter p然后尝试将p指向的值赋给otra。

程序编译ok


哪个编译器?

我我只是问,因为如果你的编译器确实编译了上面的

程序没有错误,那就有一个严重的错误。

***你不能指定一个数组***

otra是一个数组,因此分配

otra = * p;

是非法的!

但是当我尝试运行它时,挂断:(




你做了什么:

首先,pgm创建一个数组,用字符初始化它

'''''''''''''''''''''''''''''''''''''' '''''\\'0''


char algo [] =" Algo mas";


算法

+ --- + --- + --- + --- + --- + --- + --- + --- + --- +

| A | l | g | o | | m | a | s | \ 0 |

+ --- + --- + --- + --- + --- + --- + --- + --- + --- +


然后创建一个指针,指向算法的开头。

这样可行,因为在该上下文中,数组的名称为alon(''algo) '')

去掉第一个数组元素的地址。


char * p = algo;


算法

+ --- + --- + --- + --- + --- + --- + --- + --- + --- +

| A | l | g | o | | m | a | s | \ 0 |

+ --- + --- + --- + --- + --- + --- + --- + --- + --- + < br $>
^

|

+ ------------ +

|

p |

+ ------- + |

| o ---------- +

+ ------- +


接下来创建另一个数组,这次是长度为256个字符

并用字符''''''\\'0''初始化。注意:由于这仅仅是
2个字符,这也意味着剩余的254个字符不会被触及
。它们包含垃圾。


char otra [256] =" " ;;


算法

+ --- + --- + --- + --- + --- + --- + - - + --- + --- +

| A | l | g | o | | m | a | s | \ 0 |

+ --- + --- + --- + --- + --- + --- + --- + --- + --- + < br $>
^

|

+ ------------ +

|

p |

+ ------- + |

| o ---------- +

+ ------- +

otra

+ --- + --- + --- + - ...... --- + --- + --- +

| | \0 | XX | x xx | XX | xx |

+ --- + --- + --- + - ...... --- + --- + --- +


然后是非法声明:


otra = * p;


我不知道你的编译器做了什么,我只想说

你写道:


otra [0] = * p;


然后以下会发生了:

左侧的任务:很简单,只需查看''otra''并确定

元素的索引为0.此元素将获得一个新值。

作业的右侧:查找''p''。有一个阵列从

发出它。 * p中的''*'告诉程序遵循该箭头并使用

在该箭头的末尾。所以箭头指向角色A.

因此otra [0]会将其作为新值:


算法

+ --- + --- + --- + --- + --- + --- + --- + --- + --- +

| A | l | g | o | | m | a | s | \ 0 |

+ --- + --- + --- + --- + --- + --- + --- + --- + --- + < br $>
^

|

+ ------------ +

|

p |

+ ------- + |

| o ---------- +

+ ------- +

otra

+ --- + --- + --- + - ...... --- + --- + --- +

| A | \0 | XX | x xx | XX | xx |

+ --- + --- + --- + - ...... --- + --- + --- +


printf("%s",otra);


这输出A,因为otra包含一个有效的C样式字符串,

由字符A和\ 0组成。 printf在

otra [0]开始输出并在otra [1]停止输出,因为有一个''\ 0''字符。


但是:用您编译的程序仔细检查已发布的程序。您发布的

版本是非法的,没有编译器应该编译它而不用

错误消息。


-

Karl Heinz Buchegger
kb******@gascad.at


你需要做char * p =& algo;


greets

Javier写道:< blockquote class =post_quotes>嗨

我正在尝试学习如何使用指针作为一个小例子。

我做了:
void main(int argc,char * argv [])
{
char algo [] =" Algo mas" ;;
char * p = algo;
char otra [ 256] =" " ;;
otra = * p;

printf("%s",otra);
}
我试图分配一个值算法,然后尝试将算法分配给指针p然后尝试将p指向的值分配给otra。

程序编译好,但是当我尝试运行它时,挂断:(

我做错了什么?

提前致谢

J




qolume写道:

你需要做char * p =& algo;



不,他做了没有。不仅没有那个帮助,程序也不会在那之后形成良好的形式。



Hi

I''m trying to learn how to use pointers with a little example.

I did:

void main(int argc, char* argv[])
{
char algo[] = "Algo mas";
char * p = algo;
char otra[256] = " ";
otra = *p;

printf("%s", otra);
}

I tried to assign a value to algo, then tried to assign algo to a
pointer p and then tried to assign the value pointed by p to otra.

Program compiles ok but when I try to run it, hangs up :(

What I''m doing wrong ?

Thanks in advance

J

解决方案

Javier wrote:


Hi

I''m trying to learn how to use pointers with a little example.

I did:

void main(int argc, char* argv[])
{
char algo[] = "Algo mas";
char * p = algo;
char otra[256] = " ";
otra = *p;

printf("%s", otra);
}

I tried to assign a value to algo, then tried to assign algo to a
pointer p and then tried to assign the value pointed by p to otra.

Program compiles ok
Which compiler?
I am just asking, because if your compiler indeed compiled the above
program without an error, it has a serious bug.
*** You can''t assign to an array ***
otra is an array, thus the assignment
otra = *p;
is illegal!
but when I try to run it, hangs up :(



What did you do:
First the pgm creates an array and initializes it with the characters
''A'' ''l'' ''g'' ''o'' '' '' ''m'' ''a'' ''s'' ''\0''

char algo[] = "Algo mas";

algo
+---+---+---+---+---+---+---+---+---+
| A | l | g | o | | m | a | s | \0|
+---+---+---+---+---+---+---+---+---+

Then a pointer is created, which points to the beginning of algo.
This works, because in that context the name of the array alon (''algo'')
depractes to the address of the first array element.

char * p = algo;

algo
+---+---+---+---+---+---+---+---+---+
| A | l | g | o | | m | a | s | \0|
+---+---+---+---+---+---+---+---+---+
^
|
+------------+
|
p |
+-------+ |
| o----------+
+-------+

Next another array is created, this time it has a length of 256 characters
and is initialized with the characters '' '' ''\0''. Note: since this are only
2 characters, this also means that the remaining 254 characters are not
touched. They contain garbage.

char otra[256] = " ";

algo
+---+---+---+---+---+---+---+---+---+
| A | l | g | o | | m | a | s | \0|
+---+---+---+---+---+---+---+---+---+
^
|
+------------+
|
p |
+-------+ |
| o----------+
+-------+
otra
+---+---+---+-- ... ---+---+---+
| | \0| xx| x xx| xx| xx|
+---+---+---+-- ... ---+---+---+

Then comes the illegal statement:

otra = *p;

I don''t know what your compiler did with that, lets just say
you wrote:

otra[0] = *p;

Then the following would happen:
left side of assignment: Thats easy, just look up ''otra'' and identify
the element with index 0. This element will get a new value.
right side of assignment: Look up ''p''. There is an array emitting from
it. The ''*'' in *p tells the program to follow that arrow und use whatever
is at the end of that arrow. So the Arrow is pointing to the character A.
Thus otra[0] will get this as new value:

algo
+---+---+---+---+---+---+---+---+---+
| A | l | g | o | | m | a | s | \0|
+---+---+---+---+---+---+---+---+---+
^
|
+------------+
|
p |
+-------+ |
| o----------+
+-------+
otra
+---+---+---+-- ... ---+---+---+
| A | \0| xx| x xx| xx| xx|
+---+---+---+-- ... ---+---+---+

printf( "%s", otra );

This outputs "A", since otra contains a valid C style string, which
consists of the characters ''A'' and ''\0''. printf starts its output at
otra[0] and stops it at otra[1], since there is a ''\0'' character.

But: double check the posted program with the one you compiled. The
version you posted is illegal and no compiler should compile it without
an error message.

--
Karl Heinz Buchegger
kb******@gascad.at


you will need to do char * p = &algo;

greets
Javier wrote:

Hi

I''m trying to learn how to use pointers with a little example.

I did:

void main(int argc, char* argv[])
{
char algo[] = "Algo mas";
char * p = algo;
char otra[256] = " ";
otra = *p;

printf("%s", otra);
}

I tried to assign a value to algo, then tried to assign algo to a
pointer p and then tried to assign the value pointed by p to otra.

Program compiles ok but when I try to run it, hangs up :(

What I''m doing wrong ?

Thanks in advance

J




qolume wrote:

you will need to do char * p = &algo;


No he does not. Not only won''t that help, the program won''t
be well-formed after that.


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

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