我是c的新手。我有一个问题,我无法获得指向内存中相同空间的两个指针。这是一些代码。
struct Gate* tempGate;
tempGate = (struct Gate*)malloc(sizeof(struct Gate));
struct IO* IOList;
IOList = (struct IO*)malloc(sizeof(struct IO)*20);
tempGate->inputs = (struct IO*)malloc(sizeof(struct IO*)*2);
tempGate->outputs = (struct IO*)malloc(sizeof(struct IO*));
主要是我
tempGate->inputs[j] = IOList[i];
稍后在嵌套for循环中我们有了这个
public void send () {
final String name,phone,fb;
EditText one,two,three;
one=(EditText) findViewById(R.id.editText);
two=(EditText) findViewById(R.id.editText2);
three=(EditText) findViewById(R.id.editText3);
name=one.getText().toString();
phone=two.getText().toString();
fb=three.getText().toString();
Thread T=new Thread(new Runnable() {
@Override
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://test.com/test.php");
try {
List <NameValuePair> nameValuePairs= new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name",name));
nameValuePairs.add(new BasicNameValuePair("number",phone));
nameValuePairs.add(new BasicNameValuePair("fbname",fb));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response= httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
});
T.start();
}
现在当我改变IOList [i]的值时,不应该tempGate-&gt;输入[j]也会改变吗?如果不是为什么?我怎么能这样呢?帮助我Codiwan你是我唯一的希望。
答案 0 :(得分:3)
您应该将inputs
和outputs
指针数组指向IO
,而不是IO
数组。然后,您可以使此数组的元素指向IOList
的元素。
struct Gate{
enum GateType type;
struct Gate* next;
int numInputs;
struct IO** outputs;
struct IO** inputs;
};
tempGates->inputs = malloc(sizeof(struct IO*)*2);
tempGates->outputs = malloc(sizeof(Struct IO*));
然后你的循环应该是:
tempGate->inputs[j] = &(IOList[i]);
然后,当您更改IOList[i].val
时,这将更改tempGate->inputs[j]->val
。