0
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/slab.h>
#include<linux/gfp.h>
//#include<linux/types.h>

//#include<linux/kernel.h>
//#include<linux/module.h>

char *my_buff = (char*)kmalloc(100,GFP_KERNEL);
char *buff = "Linux Device Drivers programming!";

This is part of the program. I'm getting an error as "Initialiser element is not a constant". Cant figure out what might be the cause of the error.

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Angus
  • 12,133
  • 29
  • 96
  • 151
  • [don't cast the return value of `malloc()`!](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858) –  Nov 10 '13 at 09:12
  • possible duplicate of [Error "initializer element is not constant" when trying to initialize variable with const](http://stackoverflow.com/questions/3025050/error-initializer-element-is-not-constant-when-trying-to-initialize-variable-w); However this: http://stackoverflow.com/q/13620654/694576 suits even better. – alk Nov 10 '13 at 09:56

2 Answers2

2

Your variable my_buff is outside a function scope and you are initializing it from a non-constant result, i.e., the return value of kmalloc. You can't do that – you need to initialise my_buff in some function that gets called before it is used, or make the initializer a compile-time constant (which probably isn't possible in case of a pointer, although you might be able to change it to char my_buff[100]).

Arkku
  • 41,011
  • 10
  • 62
  • 84
2

You're initializing a static member, means the compiler needs to know what is the value is at compile time. You're trying to assign a value that isn't knowable to the compiler at compile time but knows at run time which is causing the error. Try assigning it in a function as below,

char *my_buff;
main()
{
    my_buff = kmalloc(100,GFP_KERNEL);
    ...
}
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46