0
static int myarray[2]={-1,234};
module_param_array(myarray,int,&arrayargc,0);
MODULE_PARM_DESC(myarray,"Integer Array");

static int __init module_init_2(void)
{
 int i;
  for(i=0;i< (sizeof myarray/sizeof(int));i++);
{

printk(KERN_INFO "myarray[%d] is %d",i,myarray[i]);

}

I am writing a simple module to take some command line input.During compilation it is giving a warning

warning: array subscript is above array bounds [-Warray-bounds]
printk(KERN_INFO "myarray[%d] is %d",i,myarray[i]);

Why is it giving warning as loop seems to be run till i=2, I saw some questions regarding this but that didn't help me so much

Saud Farooqui
  • 27
  • 1
  • 10

2 Answers2

3

Your very beginning printf specifies three %s for three strings, yet you provided just one string for that printf, and so the crash.

Note from Weather Vane comment:

Remember that the C compiler will concatenate string literals which are separated only by whitespace.

That means even though you wrote three separate "Option #1", "Option #2", etc. in three lines they still counted as just one string (after concatenation. Fix that by adding a comma at the end of each line to prevent concatenation (and so you'd have three separate strings).

artm
  • 17,291
  • 6
  • 38
  • 54
0

You can try this. I assumed you want to output both values that are successfully read.

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

int
main(int argc, char const *argv[]) {
    int period, time;

    const char micro_sec = 'u';
    const char mili_sec = 'm';
    const char sec = 's';

    printf("\nSelect unit of Time period: \n");

    printf("\nOption 1: %c for micro seconds\n"
             "Option 2: %c for mili seconds\n"
             "Option 3: %c for seconds\n", 
              micro_sec, mili_sec, sec);

    printf("\nEnter unit of Time Period: ");

    period = getchar();

    if (period == micro_sec || period == mili_sec || period == sec) {
        printf("Enter Time Period: ");

        if (scanf("%d", &time) != 1) {
            printf("Error reading time!\n");
            exit(EXIT_FAILURE);
        }

        printf("\nUnit of time: %c\n", period);
        printf("Time Period: %d\n", time);

    } else {
        printf("\nIncorrect unit of time entered.\n");
    }

    return 0;
}
RoadRunner
  • 25,803
  • 6
  • 42
  • 75