tags:

views:

109

answers:

4

Hello,

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. So my array of pointers will contain all the devices entered. However, I am getting a UB on this line:

**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++);
    }
}
+3  A: 

**devices is a char, device_buff is an array of char. The two types are incompatible.

anon
+3  A: 

You are dereferencing a null pointer. Nothing good can come out of that

char** devices = NULL;

initializes the pointer to 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. 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.

Maciej Hehl
"rather impossible to use them, if one doesn't understands exactly what he/she is doing" - a bit like integers then? And no, they are not "hard".
anon
@Neil Just like integers, if we talk about professional programming, but you must admit, there is a difference between integers and pointers for this mass of programmer wannabes we see all the time here. In their case programs using only integers have quite high chance to kinda work. Pointers require much more monkeys and much more time. They are harder in this sense.
Maciej Hehl
+3  A: 

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.

Possible solutions include using multiple character arrays (e.g. char device_buff[3][DEVICE_SIZE]) or one long array, and advancing a pointer each time you call fgets().

Oli Charlesworth
+1  A: 

You must use dynamic or predefined allocation for your Buffer-ARRAY. The Endmarker in the example is an empty String not a NULL-Pointer.

#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++);
  }
}