具有有效索引的数组未在c中返回正确的状态代码

时间:2014-11-30 02:08:45

标签: c arrays struct indexing

我这里有两个函数,对于intarr_set(),如果索引有效,我会将ia [index]的值设置为val并返回INTARR_OK这是一个状态代码,而另一个函数intarr_get()会如果索引也有效,则将* i设置为ia [index]。但是当我用我生成的随机数组测试我的函数时,[11 49 36 3 69 21 72 73 94 69 2 22 2 96 64 93],我收到一条消息,说我的intarr_get()函数没有'即使我有一个有效的索引,t返回INTARR_OK。有谁知道我哪里出错了?

这是我的数组结构:

typedef struct {
  int* data;
  unsigned int len;
} intarr_t;

这是我对intarr_set的函数:

intarr_result_t intarr_set( intarr_t* ia, 
                unsigned int index, 
                int val )

{
    if (ia == NULL)
    {
        return INTARR_BADARRAY;
    }
    else
    {
        if ( ia->data[index] != 0 )
        {
            ia->data[index] = val;
            return INTARR_OK;
        }
        else
        {
            return INTARR_BADINDEX;
        }
    }
    return 0;
}

这是我对intarr_get的函数:

intarr_result_t intarr_get( const intarr_t* ia, 
                unsigned int index, 
                int* i )
{
    if (ia == NULL)
    {
        return INTARR_BADARRAY;
    }
    else
    {
        if (ia->data[index] != 0)
        {
            if (i != NULL)
            {
                *i = ia->data[index];
            }
            return INTARR_OK;
        }
        else
        {
            return INTARR_BADINDEX;
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

intarr_setintarr_get函数的问题与您测试无效索引的方式有关。处理此问题的正确方法是将ai->data的当前分配大小存储为ai->len。保持当前分配的大小可以轻松测试index ai->len以确定有效性。它还提供了一种当前的方法,可以在以后的realloc ai->data内保持大小。所做的更改是:

intarr_result_t intarr_set( intarr_t* ia, 
                unsigned int index, 
                int val )
{
    ...
        // if ( ia->data[index] != 0 )
        if ( index < ia->len )
}

intarr_result_t intarr_get( const intarr_t* ia, 
                unsigned int index, 
                int* i )
{
    ...
        // if (ia->data[index] != 0)
        if ( index < ia->len )
}

进行更改后,使用您提供的index数组编号设置了一个简短测试,以测试INTARR_OKINTARR_BADINDEX是否正常运行。完整的测试代码以及​​结果如下。如果您还有其他问题,请发表评论:

#include <stdio.h>
#include <stdlib.h>

#define INTARR_OK 1
#define INTARR_BADINDEX -1
#define INTARR_BADARRAY -2

typedef int intarr_result_t;

typedef struct {
int* data;
unsigned int len;
} intarr_t;

intarr_result_t intarr_set( intarr_t* ia, 
                unsigned int index, 
                int val )
{
    if (ia == NULL)
    {
        return INTARR_BADARRAY;
    }
    else
    {
        // if ( ia->data[index] != 0 )
        if ( index < ia->len )
        {
            ia->data[index] = val;
            return INTARR_OK;
        }
        else
        {
            return INTARR_BADINDEX;
        }
    }
    return 0;
}

intarr_result_t intarr_get( const intarr_t* ia, 
                unsigned int index, 
                int* i )
{
    if (ia == NULL)
    {
        return INTARR_BADARRAY;
    }
    else
    {
        // if (ia->data[index] != 0)
        if ( index < ia->len )
        {
            if (i != NULL)
            {
                *i = ia->data[index];
            }
            return INTARR_OK;
        }
        else
        {
            return INTARR_BADINDEX;
        }
    }
    return 0;
}

int main (void)
{
    int rtmp[] = { 11, 49, 36, 3, 69, 21, 72, 73, 94, 69, 2, 22, 2, 96, 64, 93 };
    unsigned int rsz = sizeof (rtmp)/sizeof (*rtmp);    /* set size of index array      */
    unsigned int i = 0;
    int x = 0;
    int result = 0;

    intarr_t myarr = { NULL, 0 };                   /* initialize struct to NULL, 0     */

    myarr.data = calloc (rsz, sizeof (myarr.data)); /* allocate intarr_t, set to zero   */
    myarr.len = rsz;                                /* save for later realloc & testing */

    /* test intarr_set and intarr_get */
    printf ("\nSetting and retrieving array values, valid index (0 < index < %d)\n\n", rsz);
    for (i = 0; i < rsz; i++)
    {
        result = intarr_set ( &myarr, rtmp [i], i + 1 );
        printf ("  set myarr.data[%2u] = %d  (return: %s)\n", rtmp[i], i+1,
                (result > 0) ? "INTARR_OK" : "INTARR_BADINDEX");
        intarr_get ( &myarr, rtmp [i], &x );
        printf ("  got myarr.data[%2u] = %d  (return: %s)\n", rtmp[i], x,
                (result > 0) ? "INTARR_OK" : "INTARR_BADINDEX");
    }

    printf ("\nResulting myarr.data array\n\n");
    for (i = 0; i < myarr.len; i++)
        if (myarr.data[i])
            printf ("  myarr.data[%2u] = %d\n", i, myarr.data[i]);
        else
            printf ("  myarr.data[%2u] = 0\n", i);
    printf ("\n");

    if (myarr.data)                                 /* free allocated data */
        free (myarr.data);

    return 0;
}

<强>输出:

Setting and retrieving array values, valid index (0 < index < 16)

  set myarr.data[11] = 1  (return: INTARR_OK)
  got myarr.data[11] = 1  (return: INTARR_OK)
  set myarr.data[49] = 2  (return: INTARR_BADINDEX)
  got myarr.data[49] = 1  (return: INTARR_BADINDEX)
  set myarr.data[36] = 3  (return: INTARR_BADINDEX)
  got myarr.data[36] = 1  (return: INTARR_BADINDEX)
  set myarr.data[ 3] = 4  (return: INTARR_OK)
  got myarr.data[ 3] = 4  (return: INTARR_OK)
  set myarr.data[69] = 5  (return: INTARR_BADINDEX)
  got myarr.data[69] = 4  (return: INTARR_BADINDEX)
  set myarr.data[21] = 6  (return: INTARR_BADINDEX)
  got myarr.data[21] = 4  (return: INTARR_BADINDEX)
  set myarr.data[72] = 7  (return: INTARR_BADINDEX)
  got myarr.data[72] = 4  (return: INTARR_BADINDEX)
  set myarr.data[73] = 8  (return: INTARR_BADINDEX)
  got myarr.data[73] = 4  (return: INTARR_BADINDEX)
  set myarr.data[94] = 9  (return: INTARR_BADINDEX)
  got myarr.data[94] = 4  (return: INTARR_BADINDEX)
  set myarr.data[69] = 10  (return: INTARR_BADINDEX)
  got myarr.data[69] = 4  (return: INTARR_BADINDEX)
  set myarr.data[ 2] = 11  (return: INTARR_OK)
  got myarr.data[ 2] = 11  (return: INTARR_OK)
  set myarr.data[22] = 12  (return: INTARR_BADINDEX)
  got myarr.data[22] = 11  (return: INTARR_BADINDEX)
  set myarr.data[ 2] = 13  (return: INTARR_OK)
  got myarr.data[ 2] = 13  (return: INTARR_OK)
  set myarr.data[96] = 14  (return: INTARR_BADINDEX)
  got myarr.data[96] = 13  (return: INTARR_BADINDEX)
  set myarr.data[64] = 15  (return: INTARR_BADINDEX)
  got myarr.data[64] = 13  (return: INTARR_BADINDEX)
  set myarr.data[93] = 16  (return: INTARR_BADINDEX)
  got myarr.data[93] = 13  (return: INTARR_BADINDEX)

Resulting myarr.data array

  myarr.data[ 0] = 0
  myarr.data[ 1] = 0
  myarr.data[ 2] = 13
  myarr.data[ 3] = 4
  myarr.data[ 4] = 0
  myarr.data[ 5] = 0
  myarr.data[ 6] = 0
  myarr.data[ 7] = 0
  myarr.data[ 8] = 0
  myarr.data[ 9] = 0
  myarr.data[10] = 0
  myarr.data[11] = 1
  myarr.data[12] = 0
  myarr.data[13] = 0
  myarr.data[14] = 0
  myarr.data[15] = 0