arrayscuint64

How exactly find out length of ADT vector in C which is type of uint64_t?


I am trying to find out how to find the length of an ADT array type of uint64_t in C.

The code that provides the function for find length of array:

void func(uint64_t *arr , const char *restrict sep){
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("Size of  vektor: %d", size);
}

I am not able to change any variables because it is prohibited by its creator of whole code I can only implement some functions.

code in vector.h file

/* Exported types --------------------------------------------------------------------------------*/
/*! Data type that is stored in the vector. */
typedef uint64_t Vector_DataType_t;

/*! Structure that describes the vector. */
typedef struct {
  /*! Internal pointer to allocated memory. */
  Vector_DataType_t *items;

  /*! Number of currently allocated cells. */
  size_t size;

  /*! Pointer to the next empty cell. */
  Vector_DataType_t *next;

  /*! Number of cells allocated during expanding. */
  size_t alloc_step;
} Vector_t;

main.c file in which I am suppose to implament functionality (I can't post here whole bcse there is more than 200 lines)


int main(void)
{
  bool run = true;
  Vector_DataType_t n;
  printf("Vector test program\n"
         "Type the default size of array and the size of incrementation.\n"
         "Size:\n");
  Vector_t *vector;
  {
    size_t size;

    if (io_utils_get("%zu", &size) == true) { // Change from "false" to true
          return 0;
    }

    printf("Incrementation:\n");

    size_t step;
    if (io_utils_get("%zu", &step) == false) {
      return 0;
    }

    vector = Vector_Create(size, step);
  }

  if (vector == NULL) {
    printf("Memory for vector was not allocated successfully.\n");
    return 0;
  }

Solution

  • You cannot find the size of an array passed as a pointer into a function.

    The compiler has no way of knowing how big it is, either

    I have ignored the fact that your code very messsed up , I assume you are trying to do this

    Note NOTE ==== this code does not work ===================

     void func(uint64_t *arr , const char *restrict sep){
        int size = sizeof(arr) / sizeof(arr[0]);
        printf("Size of  vektor: %d", size);
    }