【问题标题】:Declare an Array without Size in C programming在 C 编程中声明一个没有大小的数组
【发布时间】:2018-09-23 17:26:54
【问题描述】:

假设输入以无符号幅度和二进制补码形式给出,我正在编写一个程序,它将给定的位串(最多 32 位)转换为十进制。我一次从用户那里读取一个字符并尝试将其存储到一个数组中,但是该数组没有所需的大小。有没有办法让数组在不知道数组大小的情况下通过循环?我还试图找出一种不使用 pow 和乘法函数的方法。我在下面发布我的代码,如果您有任何想法,请

#include "stdio.h"
#include "math.h"

#define MAX_BITS 32
#define ENTER '\n'
#define NUMBER_TWO 2

int main()
{
        int unsignedMag;
        int twosComp;
        int negation[n];
        int bitStore[n];
        char enter;

        //Input from the User
        printf("Enter up to 32 bits (hit 'enter' to terminate early): ");

        //Reads the first bit as a character
        char bit = getchar();
        while (getchar != enter) {
                bit = bit - '0';
                scanf("%c", &bitStore[bit]);
                getchar();
        }

        //Terminates if user hits enter
        if (bit == enter) {
                return 0;
        }

        //Continue through code
        else {
                //Loop to calculate unsigned magnitude
                for (int i = 0; i < bitStore[i]; i++) {
                        unsignedMag = unsignedMag + (bitStore[i] * pow(NUMBER_TWO, i));
                }

                //Loop to calculate complete negation
                for (int j = 0; j < bitStore; j++) {
                        negation[j] = ~bitStore[j]
                }
                negation = negation + 1;
                for (int l = 0; l < negation; l++) {
                        twosComp = twosComp + (negation[l] * pow(NUMBER_TWO, l));
                }


        }
        return 0;

}

【问题讨论】:

  • 您可以通过size_t size = sizeof(arr)/sizeof(arr[0]) 方法找到大小 - 但仅限于定义数组的位置,而不是传递的位置。

标签: c arrays twos-complement


【解决方案1】:

“有没有办法让数组在不知道数组大小的情况下通过循环?”

没有。数组大小在声明数组时是固定的,并且大小是已知的:例如@Observer

size_t size = sizeof bitStore/sizeof bitStore[0];

相反,由于代码具有“给定位字符串(最多 32 位)”,因此将数组定义为大小 32(或者 33 是​​一个 字符串 是需要的)。
跟踪分配了多少数组。

//int bitStore[n];
int bitStore[MAX_BITS];
int count = 0;

// char bit = getchar();
int bit = getchar(); // Use `int` to account for potentially 257 different values

//while (getchar != enter) {
while (count < MAX_BITS && (bit == '0' || bit == '1')) {
    bit = bit - '0';

    // Do not read again, instead save result. 
    //scanf("%c", &bitStore[bit]);  
    bitStore[count++] = bit;

    // getchar();
    bit = getchar();
}

不使用 pow 和乘法函数。

只需通过移位加或乘以 2。目前尚不清楚为什么 OP 的目标是不使用“乘法”。我看不出有什么理由禁止*。当底层乘法开销很大时,一个好的编译器会生成高效的代码,因为 *2 很容易优化。

    // int unsignedMag;
    unsigned unsignedMag = 0; // initialize

    // for (int i = 0; i < bitStore[i]; i++) {
    for (int i = 0; i < count; i++) {
      // preferred code, yet OP wants to avoid * for unclear reasons 
      // unsignedMag = unsignedMag*2 + bitStore[i];
      unsignedMag = unsignedMag + unsignedMag + bitStore[i];
    }

pow() 最好避免在这里,原因有很多。最重要的是,对整数问题使用 double 数学会遇到宽整数的精度问题。


将给定的位串(最多 32 位)转换为十进制

请注意,此任务不需要bitStore[] 数组。读取数据时只需形成unsignedMag

【讨论】:

  • 那么你会用什么来代替 pow(),我有点不清楚我应该在那里做什么,因为我试图避免乘法和 pow() 函数。
  • @mickeyvolmouse unsignedMag = unsignedMag + unsignedMag + bitStore[i]; 已发布,避免使用 pow() 和乘法。
  • 我的意思是为了两个人的恭维。我知道如何摆脱乘法,但我必须做 2^l 才能得到二进制补码的十进制值,如上面的代码所示
  • @mickeyvolmouse “必须做 2^l 才能获得二进制补码的十进制值”是一种方法——但这不是唯一的方法。代码可以用(1LL &lt;&lt; l)替换pow(NUMBER_TWO, l)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
  • 2018-09-14
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多