指针方法?? [英] Pointers in methods??

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

问题描述

是否只允许指针指向某个方法中的某个东西?


#include< stdio.h>


struct data {


int x;


int y;


};


struct data test,* pp;


pp =& test; //为什么这是非法的?

main(){

pp =& test;

}

为什么上面的代码是非法的?

解决方案

JS写道:

是否只允许使指针指向某个方法中的某个东西??
< stdio.h>

结构数据{

int x;

int y;

};

结构数据测试,* pp;

pp =& test; //为什么这是非法的?


您不能将运行时代码(赋值)放在功能块之外。

不要将赋值(运行时)与初始化混淆(编译 - 时间),

例如


struct data * pp =& test;


main(){

pp =& test;


这个,我相信,有效。

}



在文章< ; d1 ********** @ news.net.uni-c.dk> ;, JS< dsa。@ asdf.com>写道:

:是否只允许指针指向方法中的某些内容?


C没有方法;从你的代码我怀疑你的意思

" functions" ;.


:struct data {int x; int y; };

:struct data test,* pp;

:pp =& test; //为什么这是非法的?


因为你把它放在另一条线上,所以pp

的设置不是初始化器而是声明。


尝试


struct data {int x; int y; };

结构数据测试,* pp =&测试;


-

如果你喜欢VT-52,那就太开心了's。




JS写道:

是否只允许制作指针指向某个方法中的某些东西??


结构数据{

int x;

int y;

};

结构数据测试,* pp;

pp =& test; //为什么这是非法的?




因为它是一个可执行语句,并且一个可执行的

语句必须驻留在一个函数内(不是方法。

这个赋值是非法的,因为退出()

调用或者'for''循环在这一点上是非法的。


但是,您仍然可以通过在'pp''声明中使用

初始化程序来实现目标:


struct data test,* pp =& test;


或者更清楚


struct data test;

struct data * pp =& test;


尽管存在'='',但这不是*赋值

语句;它是一个带初始化器的声明。保持在

介意C倾向于为多个

目的重复使用相同的字符。根据上下文,`*''可以是乘法

运算符或指针解引用运算符或'/ *''

或'* /''注释的一部分标记或'* =''乘法和赋值

运算符的一部分。就这样,`=''可以是赋值运算符,或者部分

的初始化语法,或者`==''或`!=''或
$的一部分b $ b` * =''或者......尽管它们都使用`=''并且

两侧都有相同的符号集,你的代码和我的代码是/>
是不同的:一个是可执行语句,另一个是

是带有初始化程序的变量声明。


-
Er*********@sun.com


Is it only allowed to make a pointer point to something in a method??

#include <stdio.h>

struct data {

int x;

int y;

};

struct data test, *pp;

pp = &test; //WHY IS THIS ILLEGAL??
main(){
pp = &test;
}

Why is the above code illegal??

解决方案

JS wrote:

Is it only allowed to make a pointer point to something in a method??

#include <stdio.h>

struct data {

int x;

int y;

};

struct data test, *pp;

pp = &test; //WHY IS THIS ILLEGAL??
You cannot put run-time code (assignment) outside of a function block.
Do not confuse assignment (run-time) with initialization (compile-time),
e.g.

struct data *pp = &test;


main(){
pp = &test;
This, I trust, works.

}



In article <d1**********@news.net.uni-c.dk>, JS <dsa.@asdf.com> wrote:
:Is it only allowed to make a pointer point to something in a method??

C doesn''t have "methods"; from your code I suspect you mean
"functions".

:struct data { int x; int y; };
:struct data test, *pp;
:pp = &test; //WHY IS THIS ILLEGAL??

Because you have it on a different line, the setting of pp
is not an initializer but rather a statement.

Try

struct data { int x; int y; };
struct data test, *pp = &test;

--
Feep if you love VT-52''s.




JS wrote:

Is it only allowed to make a pointer point to something in a method??

#include <stdio.h>

struct data {

int x;

int y;

};

struct data test, *pp;

pp = &test; //WHY IS THIS ILLEGAL??



Because it is an executable statement, and an executable
statement must reside inside a function (not "method").
This assignment is illegal for the same reason an exit()
call or a `for'' loop would be illegal at this point.

However, you can still achieve your goal by using an
initializer in the declaration of `pp'':

struct data test, *pp = &test;

or perhaps more clearly

struct data test;
struct data *pp = &test;

Despite the presence of the `='' this is *not* an assignment
statement; it is a declaration with an initializer. Keep in
mind that C tends to re-use the same character for multiple
purposes. Depending on context, `*'' can be the multiplication
operator or the pointer dereference operator or part of a `/*''
or `*/'' comment marker or part of the `*='' multiply-and-assign
operator. Just so, `='' can be the assignment operator, or part
of the syntax of an initialization, or part of `=='' or `!='' or
`*='' or ... Despite the fact that they both use `='' and have
the same set of symbols on both sides of it, your code and mine
are different: one is an executable statement, and the other
is a variable declaration with an initializer.

--
Er*********@sun.com


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

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