简体   繁体   中英

iterating in a array in C

Hey I have something of this type

eph_t *a;

The type is eph_t as you can see. Its an array in C but I do not know the size of the array nor do I know what is the end element of the array. Is there a way, I can go through the whole array because I want to assign the values of every element in an array to something.

What can be my options to consider? If you could not understand something in the question just comment so that I can inform you.

If you do not know the size of the array then it is unsafe to iterate over it. Any time that you attempt to read elements beyond the last one you will get undefined behaviour. There is nothing safe you can do unless you know the size of the array.

As others have said, it is unsafe to iterate over an array when you do not know the end of the array. This is often worked around in the following way.

  1. If you have access to the array declaration ( int a[10]; ) for example. you can use the sizeof operator to determine the array's size. Please note this WILL NOT WORK when passing a pointer to an array to a function.
  2. Functions that use an array will often take a direct size or some way to infer the size as extra parameters to the function ( memset is a good example)
  3. The array may have a special terminator element (usually a NULL or 0 element at the end) when means you may not iterate beyond (C strings are good examples)

So if you are designing a function that takes an array as a parameter, please use the above patterns. If you are using a function that does not use one of the above pattern, report the problem to the library designer as a bug.

A pointer in C is just an address. When used as an array, you have to figure out (by some other means) the length of the array.

A lot of libraries dealing with arrays have functions accepting both the pointer to the array and its size. For example qsort(3) wants a second nmemb argument giving the number of elements of the array base (first argument to qsort ) to be sorted.

Alternatively, instead of passing just a pointer, you could use flexible array members (in C99) and pass (and return, when relevant) a pointer to a structure like

  struct eph_tuple_st {
     unsigned len;
     eph_t* ptrtab[];
  };

with the convention that the flexible array ptrtab field has len elements.

At last, as others suggested, you could use a sentinel value (ie a null word) to end the array. Generally I don't recommend that (risk of buffer overflow, time complexity to compute the actual size).

FWIW, recent C++ have std::dynarray (C++2014) and std::vector , and Ocaml has the Array module. You could switch to some more friendly programming language.

You can reserve the first element of array to store the size

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

typedef struct {
    int x, y;
    double z;
} eph_t;

static void temp(eph_t *a)
{
    size_t n;

    memcpy(&n, a - 1, sizeof(size_t)); /* get size (stored in a - 1) */
    printf("Count = %zu\n", n);
}

int main(void)
{
    const size_t n = 5;
    eph_t a[n + 1]; /* allocate space for 1 more element */

    memcpy(&a[0], &n, sizeof(size_t)); /* now the first element contains n */
    temp(a + 1); /* skip first element */
    return 0;
}

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