结构数组不保存

时间:2015-11-19 06:12:59

标签: c arrays struct

我正在计算c中的结构,我不确定为什么这不会返回值。我知道当你传递一个函数一个数组并添加值时,这些值就在函数之后的数组中。结构也是如此吗?下面是我的代码的简化版本(我的结构有更多的内部变量,但它们也没有返回)。

typedef struct {
  double points;
  FILE *file;
} Polygon;

void readobject(FILE *g, Polygon poly) {
  fscanf(g, "%lf", &poly.points); //lets say this is reads in 6.0
  printf("%lf\n", poly.points); //this will print 6.0
}


int main (int argc, char **argv){
 Polygon polygon[argc]; 
 int cc = 0;
 polygon[cc].file = fopen(argv[cc], "r");
 readobject(polygon[cc].file, polygon[cc]);
 printf("%lf\n", polygon[cc].points); //This prints out 0.0
}

为什么这样做?我怎样才能让它返回6.0?

2 个答案:

答案 0 :(得分:1)

您将按值传递对象readobjectreadobject中的修改是函数的本地修改。您需要将指针传递给对象,以便从main显示更改。

void readobject(FILE *g, Polygon* poly) {
                             // ^^
  fscanf(g, "%lf", &(poly->points)); //lets say this is reads in 6.0
  printf("%lf\n", poly->points); //this will print 6.0
}

然后使用:

进行呼叫
readobject(polygon[cc].file, &polygon[cc]);
                          // ^^

答案 1 :(得分:0)

虽然数据在结构中彼此相邻存储,结构与数组一样,但结构与数组不同,因为您可以在其中包含不同类型的值(成员)。如果您希望修改您的值,则可以传递其指针或引用。

作为指针:

class EmailCustom extends CEmailValidator
{
    public $pattern = '/^[ ]*[a-zA-Z0-9!#$%&\'*+\\/=?^_`’{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`’{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?[ ]*$/';

    protected function validateAttribute($object, $attribute)
    {
        $pattern = '/^[ ]*[a-zA-Z0-9!#$%&\'*+\\/=?^_`’{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`’{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?[ ]*$/';
        if ($object->visitor_card_status == 2) {
            if (!preg_match($pattern, $object->$attribute)) {
                if ($object->$attribute !== $object->first_name . '.' . $object->last_name) {
                    $this->addError($object, $attribute, 'Email is incorrect format');
                }
            }
        } else {
            if (!preg_match($pattern, $object->$attribute)) {
                $this->addError($object, $attribute, 'Email is incorrect format');
            }
        }
    }

    public function clientValidateAttribute($object, $attribute)
    {
        if ($this->validateIDN)
        {
            Yii::app()->getClientScript()->registerCoreScript('punycode');
            // punycode.js works only with the domains - so we have to extract it before punycoding
            $validateIDN='
                            var info = value.match(/^(.[^@]+)@(.+)$/);
                            if (info)
                                value = info[1] + "@" + punycode.toASCII(info[2]);
                            ';
        } else {
            $validateIDN = '';
        }

        $message = $this->message!==null ? $this->message : Yii::t('yii','{attribute} is not in a recognised format. <span style="text-transform:capitalize;">Please </span>revise.');
        $message = strtr($message, array(
            '{attribute}'=>$object->getAttributeLabel($attribute),
        ));
}

参考:

void readobject(FILE *g, Polygon *poly) {
  //since its a pointer, you access the object's members by the '->' operator
  fscanf(g, "%lf", &poly->points); 
  printf("%lf\n", poly->points); //this will print 6.0
}
//call
readobject(polygon[cc].file, &polygon[cc]);