c - 我的数组函数未返回变量

标签 c arrays function

该程序的目的是采用十六进制数字(例如“ 10”或“ 01”)进行转换
二进制等于00010000和000000001。

这是一个非常复杂的程序的伪代码尝试:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <stdbool.h>
int main(int argc, char ** argv) {

  int * a1;
  int * a2;
  array[0] = 10;
  array[1] = 01;
  a1 = HexToBin(array[0]);
  a2 = HexToBin(array[1]);

  printf("here is binary of the hex number '10' %d", a1); //THIS SHOULD PRINT 00010000
  printf("here is binary of the hex number '01' %d", a2); //THIS SHOULD PRINT 00000001

}

int * HexToBin(char * array) {

  int i;
  for (i = 0; i < 2; i++) {
    switch (array[i]) {

    case '0':
      return 0000;
      break;
    case '1':
      return 0001;
      break;
    default:
      break;
    }
  }
}


我怎样才能做到这一点?

最佳答案

您的printf语句打印的是a1a2的地址,而不是该地址的内容。您需要使用*取消引用或使用下标符号a1[0]

另外,您的HexToBin方法应返回'int而不是int *。

关于c - 我的数组函数未返回变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58894988/

相关文章:

c++ - C++ 中的类 C 程序?

javascript - JSON stringify 忽略数组中的某些值

python - 在另一个列表中编码列表元素存在的最有效方法

c - 使用指针传递结构数组并返回结构数组 - 函数

c++ - 内联或不内联

c - fflush(stdin) 在 C 编程中做什么?

c++ - 使用 GCC 强制执行显式(直接)#include 语句

c - 在 C 中读取目录时出现问题

python - Numpy 数组按值传递

Azure Function 返回 500 内部服务器错误