首页 > 解决方案 > 如何确定数组在c中的维数

问题描述

所以我想创建一些以数组作为输入的函数,但我不知道数组会有多少维。

c中有没有办法确定数组有多少维?(理想情况下作为一个函数)

标签: arraysc

解决方案


So this is actually a fairly difficult problem in C. Usually this is solved using one of three ways:

  1. Have a special terminating value, like '\0' for strings
  2. Pass the dimensions of the array as a parameter to whatever function you're using
  3. Keep the pointer of the array and its dimension in a struct together

If I remember correctly, there's a way to figure out the size of an allocated array using *nix systems, but I definitely would not recommend doing this. Just keep track of your allocated memory.


推荐阅读