简体   繁体   English

将char缓冲区分配给指针数组

[英]assigning a char buffer to an array of pointers

gcc 4.4.4 c89 gcc 4.4.4 c89

warning assignment makes integer from pointer without a cast
**devices = device_buff;
warning: value computed is not used
*devices++;

I get the above warnings with the code below. 我使用下面的代码得到了上述警告。 What I am trying to do is get an input from the user. 我想要做的是从用户那里获得一个输入。 And assign that char array to an array of pointers. 并将该char数组分配给指针数组。 So my array of pointers will contain all the devices entered. 所以我的指针数组将包含输入的所有设备。 However, I am getting a UB on this line: 但是,我在这条线上获得了一个UB:

**devices = device_buff;

Many thanks for any advice, 非常感谢任何建议,

static void device_input()
{
#define DEVICE_SIZE 80
    char device_buff[DEVICE_SIZE] = {0};
    char **devices = NULL;
    size_t i = 0;

    for(i = 0; i < 3; i++) {
        printf("Enter device name: ");
        fgets(device_buff, (size_t)DEVICE_SIZE, stdin);

        **devices = device_buff;
        *devices++;
    }

    /* NULL terminate last element */
    *devices = NULL;

    printf("Display devices\n");
    while(*devices != NULL) {
        printf("Device [ %s ]\n", *devices++);
    }
}

**devices is a char, device_buff is an array of char. **devices是char, device_buff是char数组。 The two types are incompatible. 这两种类型是不兼容的。

You are dereferencing a null pointer. 您正在取消引用空指针。 Nothing good can come out of that 没有任何好处可以从中产生

char** devices = NULL;

initializes the pointer to NULL . 将指针初始化为NULL It's never set to anything else and then dereferenced (twice). 它永远不会被设置为任何其他东西,然后解除引用(两次)。

Pointers are considered hard and it's rather impossible to use them, if one doesn't understand exactly, what he/she is doing. 指针被认为很难,如果不完全理解,他/她正在做什么,使用它们是相当不可能的。 I think there are two options in your scenario. 我认为您的方案中有两个选项。 You can store the names in one char array, one adjacent to another and keep an array of pointers pointing to the beginnings of those names or you can use an array of char arrays (two dimensional array) to store the names "separately" each one in another array. 您可以将名称存储在一个char数组中,一个与另一个相邻,并保持一个指向这些名称开头的指针数组,或者您可以使用char数组(二维数组)数组来“分别”存储每个名称在另一个数组中。 I think the second way is much simpler and you should start from getting it working. 我认为第二种方式更简单,你应该从让它运作开始。

You can define the array like this 您可以像这样定义数组

#define NUM_OF_NAMES 3

char devices[NUM_OF_NAMES][DEVICE_SIZE] = {0};

now devices[0] , devices[1] and devices[2] are all char arrays of type char[DEVICE_SIZE] .You can use each of them, like the buffer previously. 现在devices[0] devices[1]devices[2]是所有char类型的数组char[DEVICE_SIZE]您可以使用它们中的每一样,缓冲器先前。

Even if you fix the compiler errors (as described by others), what you are trying to do won't work. 即使您修复了编译器错误(如其他人所述),您尝试执行的操作也无法正常工作。 You are calling fgets() on the same device_array each time, so each time it's called, it will overwrite what was stored there previously. 您每次都在同一个device_array上调用fgets() ,因此每次调用它时,它都会覆盖之前存储的内容。

Possible solutions include using multiple character arrays (eg char device_buff[3][DEVICE_SIZE] ) or one long array, and advancing a pointer each time you call fgets() . 可能的解决方案包括使用多个字符数组(例如char device_buff[3][DEVICE_SIZE] )或一个长数组,并在每次调用fgets()时前进指针。

You must use dynamic or predefined allocation for your Buffer-ARRAY. 您必须为Buffer-ARRAY使用动态或预定义分配。 The Endmarker in the example is an empty String not a NULL-Pointer. 示例中的Endmarker是空字符串而不是NULL指针。

#define DEVICE_SIZE 80
typedef char DBuff[DEVICE_SIZE];

static void device_input()
{
  #define MAXB 3
  DBuff device_buff[MAXB+1];
  DBuff *devices=device_buff;
  size_t i = 0;

  for(i = 0; i < MAXB; i++,devices++) {
      printf("Enter device name: ");
      fgets(*devices, (size_t)DEVICE_SIZE, stdin);
  }
  **devices=0;
  devices=device_buff;
  printf("Display devices\n");
  while( **devices ) {
    printf("Device [ %s ]\n", *devices++);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM