简体   繁体   中英

Eclipse Crashing When Returning Null C++

I am having issues with my code. I want to return null when the array size is 0. However, when testing in Eclipse, it crashes. Any tips?

Thanks!

#include <iostream>
using namespace std;

double* maximum(double* a, int size)
{
   if (size == 0)
   {
      return NULL;
   }
   double* m = a;
   double* p = a;
   for (int i = 0; i < size; i++)
   {
      if (*p > *m)
      {
         m = p;
      }
      p++;
   }
   return m;
}

int main()
{
   double data[] = {};
 double* max = maximum(data, 0);
   cout << *max << endl;
   return 0;
}

取消引用 NULL 指针是非法的,您可以使用cout << *max << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM