0

This is part of an IOCTL switch case. I am getting the following error?

error: storage size of 'data' isn't known error: 'mesg' undeclared (first use in this function)

case PASS_STRUCT:

        struct mesg{
            int pIDs[SIZE];
            int niceVal;
        };

        struct mesg data;

        ret = copy_from_user(&data, arg, sizeof(data));
        if(ret < 0){
            printk("PASS_STRUCT\n");
            return -1;  
        }

        printk("Message PASS_STRUCT : %d\n",data.niceVal);
        break;

SIZE value is set before creating mesg.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
user340
  • 375
  • 12
  • 28

1 Answers1

2
struct mesg{
    int pIDs[SIZE];
    int niceVal;
};

SIZE value is set before creating mesg.

In C SIZE needs to be a true compile-time constant to use it like that. Something like:

#define SIZE 10

You also should move the structure declaration outside the switch.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • the SIZE of the pIDs array changes upon each all to this driver, it is not defined as #define SIZE 10. What can be done? – user340 Aug 26 '12 at 15:40
  • @kani Yeah. It will bring other changes though. – cnicutar Aug 26 '12 at 15:43
  • Hmm yeah :-( can you have a look at this. http://stackoverflow.com/questions/12130689/passing-struct-to-device-driver-through-ioctl. I define SIZE before calling PASS_STRUCT can't any thing done in that code? – user340 Aug 26 '12 at 15:49
  • 1
    @kani Read more about `C` and Linux internals: it will be easier than just dragging the code one question to the other – cnicutar Aug 26 '12 at 15:51
  • Will #define SIZE 10 be able to be changed at runtime? – user340 Aug 26 '12 at 16:02
  • @kani No, once you define it it becomes "text". – cnicutar Aug 26 '12 at 16:03
  • with a the same code above, now am getting this error: expected expression before 'struct'. What do you think of this? – user340 Aug 26 '12 at 16:11
  • 1
    @kani, If you want the array size to be dynamic, then you really want an `int numIDs` element and change `pIDs` to a pointer (`int * pIDs`). Then the driver will have to allocate a memory space the size of `numIDs*sizeof(int)`, then do a `copy_from_user()` to that memory space using `pIDs`. – Joshua Aug 29 '12 at 16:33