如何计算阵列中的非零元素? [英] How to calculate the non-zero elements in the array?

查看:128
本文介绍了如何计算阵列中的非零元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算阵列中的非零元素 - 没有警告?
我有简单的code:

How to calculate the non-zero elements in the array - without warning? I have simple code:

int i;
char *symbols[1000];
for(i = 0; i < 10; i++){
   symbols[i] = "Hi :-)";
}

i++;
symbols[i] = NULL;

int mumberofelements = 0;

for(i=0; i < 1000; i++){
   if(symbols[i] != NULL){ // WARNING comparison between pointer and integer [enabled by default]
   numberofelemets++;
   }
}

但我警告:

指针和整数之间的比较[默认启用]

comparison between pointer and integer [enabled by default]

我怎样才能解决这个问题?

How can I fix it?

推荐答案

您不需要明确对证 NULL

for(i=0; i < 1000; i++)
{
   if(!symbols[i])    // If pointer is NULL then increase the elements
      numberofelemets++;
}

这篇关于如何计算阵列中的非零元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆