你能告诉我这个项目的具体内容吗?

时间:2016-01-19 12:50:24

标签: c if-statement syntax realloc

#include stdio.h
#include <stdlib.h>
#include <ctype.h>
#define CAPACITY_INCREMENT 6
double average(double data[], int count)
  {
   double sum = 0.0;
   int i;
   for(i=0;i<count;sum+=data[i++]);
   return sum/count;
  }

int main(void)
  {
    double *data = NULL;
    double *temp = NULL;
    int count = 0;
    int capacity = 0;
    char answer = 'n';

   do
    {
      if(count == capacity)
       {
          capacity += CAPACITY_INCREMENT;
          if(!(temp = (double*)realloc(data, capacity*sizeof(double))))
         {
            printf("Error allocating memory for data values.\n");
            exit(1);
         }
         data = temp;
       }

       printf("Enter a data value: ");
       scanf(" %lf", data + count++);
       printf("Do you want to enter another (y or n)? ");
       scanf(" %c", &answer, sizeof(answer));
     } while(tolower(answer) != 'n');

    printf("\nThe  average of the values you entered is %10.2lf\n", average(data, count));
    free(data);
    return 0;
   }

我是C的初学者,我的朋友之一正在帮我发送这段代码,我知道这是打印给定数字的平均值,但我不知道某些语法是怎么做的:

  if(!(temp = (double*)realloc(data, capacity*sizeof(double))))"

你能解释一下这是如何工作的吗?

1 个答案:

答案 0 :(得分:0)

首先,这一行

"20 YY"

应该看起来像

<?php 
                $cc_number="";
                $cc_expire="";
                $cc_cvc="";
                $cc_number=$_POST['number'];
                $cc_expire=$_POST['expiry'];
                $cc_cvc=$_POST['cvc'];
                $cc_pid=$_SESSION['pid'];

                /*edit the expire date*/
                $pieces=explode("/", $cc_expire);
                $expire_dd="01";
                $expire_mm=$pieces[0];

                $expire_yy="20".$pieces[1];
                $expire_yy=trim($expire_yy, "\x00..\x1F");

                //$cc_expire = $expire_yy.$expire_mm.$expire_dd;
                //$cc_expire = date('Y-m-d', strtotime(str_replace('-', '/', $cc_expire)));
                echo "expire_yy = ".$expire_yy;
                //echo "cc_expire = ".$cc_expire;
                if (isset($_POST["submit"])) {
                    if (empty($cc_number) || empty($cc_cvc) || empty($cc_expire)) {
                        echo "<p align='center'><font color='red'><br><br><br><br>All fields are mandatory.</font></p>";
                    }
                    else {
                        $cc_expire = date('Y-m-d', strtotime($cc_expire));
                        $sql = "INSERT INTO credit_card (credit_card_number, cvc, expiration_date, pid)
                                VALUES ('$cc_number', '$cc_cvc', '$cc_expire', $cc_pid)";
                        if ($dbconn->query($sql)) {
                            //echo "<script> window.location.assign('../card.php'); </script>";
                        }
                        else {
                            echo "<p align='center'><font color='red'><br><br><br><br>Something went wrong.</font></p>";
                        }
                    }
                }
            ?>

因为as per this discussion we need not to cast the return value of malloc() and family in C.

那说,打破声明,

  1. 首先, if(!(temp = (double*)realloc(data, capacity*sizeof(double)))) 得到评估。此语句重新分配 if(!(temp = realloc(data, capacity*sizeof(double)))) 以使内存分配等于temp = realloc(data, capacity*sizeof(double))字节的大小。返回的指针存储到data

  2. 然后基本上整个语句缩减为capacity*sizeof(double)。这通过检查返回的指针是否为NULL来检查temp调用是否成功。

    • 如果if (! (temp))失败,则返回NULL,realloc()将评估为TRUE,因此程序将执行realloc()并重新开始。

    • 如果if成功,exit(1);将有一个非NULL指针,因此realloc()检查将失败,程序将继续正常运行。