搜索数组成员之间差异的函数 [英] Function that searches for difference between members of an array

查看:64
本文介绍了搜索数组成员之间差异的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个函数,如果发现数组成员之间存在差异,该函数将返回 true.

I need to write a function that will return true if it has found a difference between members of an array.

我的代码是:

int func1(int  *str)
{
    int i;
    for(i=0;i<*(str+i);i++) {
        if(*(str+i) == *(str+i+1))
        {
            return 1;
        }
    }
    return 0;
}

我必须用指针来实现它.

I have to implement it with pointers.

上面的代码不起作用(逻辑上).

The code above does not work(logically).

有人可以帮忙吗?

更新:

我已将代码更改为以下内容:

I have changed my code to the following:

int func1(int  *str)
{
    int i,temp=0;
    for(i=0;i<10-1;i++) {
        if(*(str+i) == *(str+i+1))
        {
            temp++;
            if( temp == 10 )
            {
                return 1;
            }
        }
    }
    return 0;
}

新代码有什么问题?

推荐答案

你的函数只接受一个数组指针,这对于比较来说似乎太少了.

Your function only takes in a single array pointer, that seems like one too few for a comparison.

您必须添加一个参数来指定数组的长度,或者实现某种策略",例如使用特定值终止数组.

You must add an argument that specifies the lengths of the arrays, or implement some kind of "policy" that e.g. terminates the arrays using a specific value.

您还应该考虑使用标准的memcmp()函数.

You should also look into using the standard memcmp() function.

这篇关于搜索数组成员之间差异的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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