c-具有随机打印的void指针数组

时间:2018-10-29 11:18:47

标签: c arrays pointers printing

我尝试了许多不同的方法,并且看到了许多不同的文章来做到这一点,但是我无法使其正常工作。我尝试了malloc方法。我正在尝试将各种数据类型分配到空指针数组中,并打印一个随机选择的值。

#include <stdio.h>
#define LENGTH 4
void main()
{
    int a = 3;
    float b = 2.5;
    char c = 'H';
    int d = 421;
    char e = 'a';
    void *array;
    array[LENGTH];
    array[0] = &a;
    array[1] = &b;
    array[2] = &c;
    array[3] = &d;
    array[4] = &e;
    printf("%p \n", array[rand() % LENGTH]);
}

3 个答案:

答案 0 :(得分:1)

为了打印任何类型的通用数据,您将需要将类型信息与数据一起存储。这通常在C语言中通过创建一个枚举来完成。例如:

typedef enum
{
  TYPE_INT,
  TYPE_FLOAT,
  TYPE_CHAR,
  TYPE_STR,
} type_t;

然后您可以将其与数据一起存储:

typedef struct
{
  type_t type;
  void*  data;
} generic_t;

因此该数组将必须是一个结构数组:

generic_t array [LENGTH] = { ... };

完整示例:

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

typedef enum
{
  TYPE_INT,
  TYPE_FLOAT,
  TYPE_CHAR,
  TYPE_STR,
} type_t;

typedef struct
{
  type_t type;
  void*  data;
} generic_t;


void print_int   (int   x) { printf("%d", x); }
void print_float (float x) { printf("%f", x); }
void print_char  (char  x) { printf("%c", x); }
void print_str   (const char* x) { printf("%s", x); }

void print (const generic_t* gen)
{
  switch(gen->type)
  {
    case TYPE_INT:   print_int(*(int*)gen->data);       break;
    case TYPE_FLOAT: print_float(*(float*)gen->data);   break;
    case TYPE_CHAR:  print_char(*(char*)gen->data);     break;
    case TYPE_STR:   print_str((const char*)gen->data); break;
  }
}

#define LENGTH 5

int main (void)
{
  srand(time(NULL));

  int a = 3;
  float b = 2.5;
  char c = 'H';
  int d = 421;
  char e[] = "hello";

  generic_t array [LENGTH] =
  {
    { TYPE_INT,   &a },
    { TYPE_FLOAT, &b },
    { TYPE_CHAR,  &c },
    { TYPE_INT,   &d },
    { TYPE_STR,   &e },
  };

  for(size_t i=0; i<10; i++) // print 10 random indices of the array
  {
    print( &array[rand()%LENGTH] );
    printf("\n");
  }
}

使用C11 _Generic等可以使它更漂亮,更安全,但是上面是通用C编程的“老派”,向后兼容方式。

答案 1 :(得分:0)

假设您要随机打印变量的地址:

reponseValide1.findIndex(obj => obj.title == reponseValide2[0].title)

要在这些地址上打印值,必须对程序进行一些更改。
-为数据类型声明#include <stdio.h> #define LENGTH 5 //There are five variables void main() { int a = 3; float b = 2.5; char c = 'H'; int d = 421; char e = 'a'; void *array[LENGTH] = {&a, &b, &c, &d, &e}; printf("%p \n", array[rand() % LENGTH]); }
-在Enum内使用匿名union来保存数据及其类型
-根据适当的类型进行打印的打印功能

程序将如下所示:

structure

答案 2 :(得分:0)

在调用printf之前,您需要区分类型。这是一种方法。这种方法使您可以正常使用printf:public class Solution { public static void climbStairsByComputingAllNextSteps(int n) { processNextClimbingByComputingAllNextSteps(0, n); } private static int processNextClimbingByComputingAllNextSteps(int i, int n) { if (i > n) { return 0; } if (i == n) { return 1; } return processNextClimbingByComputingAllNextSteps(i + 1, n) + processNextClimbingByComputingAllNextSteps(i + 2, n); } public static int climbStairsDynamically(int n) { if (n == 1) { return 1; } int[] dp = new int[n + 1]; dp[1] = 1; dp[2] = 2; for (int i = 3; i <= n; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n]; } public static void main(String[] args) { long startTime, diffTime; int steps; try { steps= Integer.parseInt(args[0]); } catch (Exception e) { steps=10; } System.out.println("Time spent for " + steps + " step(s):"); System.out.print("\t - climbStairsByComputingAllNextSteps(): "); startTime = System.nanoTime(); climbStairsByComputingAllNextSteps(steps); diffTime = System.nanoTime() - startTime; System.out.println(diffTime + " nanoseconds"); System.out.print("\t - climbStairsDynamically(): "); startTime = System.nanoTime(); climbStairsDynamically(steps); diffTime = System.nanoTime() - startTime; System.out.println(diffTime + " nanoseconds"); } }

请记住要调整MAX_FORMATTED_LEN。

printf("Random entry: %s\n", FMT_TYPE(array[rand() % LENGTH]))