使用哈希表访问C中的PHP变量 - 指针间接级别的更改

问题描述:

I am referring Sara Golemon "Extending and Embedding PHP". I am confused regarding the following snippet

zval **fooval;    
if (zend_hash_find(EG(active_symbol_table), "foo", sizeof("foo"), (void**)&fooval) == SUCCESS) 

Why is &fooval, which would evaluate to a zval***, cast to a void** ?

我指的是Sara Golemon“扩展和嵌入PHP”。 我对以下代码段感到困惑 p>

  zval ** fooval;  
if(zend_hash_find(EG(active_symbol_table),“foo”,sizeof(“foo”),(void **)& fooval)== SUCCESS)
  code>  pre> 
 
 

为什么& fooval code>,它会评估为 zval *** code>,强制转换为 void ** code>? p> \ n div>

The function is assigning a pointer to the value of a pointer to a pointer. It doesn't care what type of pointer it is assigning, just that it is getting a pointer. void more-or-less, then, means "generic".

I will admit that this is a gross over-simplification, but would be the same as having this in PHP:

function foo(stdClass $bar){
   // do something
}

class Bat extends stdClass{

}
$bat = new Bat();
foo((stdClass)$bat);

The confusion with changing Pointer Indirection Level can be explained using the following snippet.

In this case ipNew is single level pointer, but using (single or chain of )indirection operator you can use it to retrieve values until the value on right of '*' is an address

#include<iostream>
using namespace std;
int main()
{

int i = 1;
int* ip = &i;
int** ipp = &ip;

cout<<"i = "<<i<<"
ip =  "<<ip<<"
ipp = "<<ipp<<"
*ip = "<<(*ip)<<"
**ipp = "<<(**ipp);

int * ipNew = (int *)&ip;
cout<<"

i="<<i<<"
ip="<<ip<<"
ipp="<<ipp<<"
"<<"*ip="<<(*ip)<<"
**ipp="<<(**ipp)<<endl<<"
ipNew="<<ipNew<<"
*ipNew="<<(int *)*ipNew<<"
**ipNew="<<*((int*)*ipNew)<<endl;

return 0;
}