I m a learner in C . while doing the following code Error Msg is array subscript is not an Integer. why that eroor came?
printf("Enter Student name and Mark");
2 Answers
array_s[char_s] —> you are trying to index the array array_s, but you pass it char_s which is an array of chars, NOT an integer.
If you want a string from the input, you should just pass a char array, either char_s or array_s:
This will fill the char array array_s with the characters from the input.
Log in to reply to the answers Post
you may no longer use a non-imperative datatype as a subscript; you need to use the int datatype. For e9dd4e461268c8034f5c8564e155c67a6ample: // calculate sum for (int x = 0; x
The deviation function throws me the following error: "Array subscript is not an integer" . If you can help me locate the cause of error I’ll appreciate it.
4 Answers 4
p is a pointer to a float. That’s why you’re getting the error.
You can only use ints as array indices in C.
Array subscripts must be an integer, an int type. You can’t use any other type as array subscript. p is declared as float *p , i.e, a pointer to float . You can’t use a pointer as array indices.
You can use an int:
or, you can use p as a pointer directly, which is, I suppose, what you intended.
Note, however, that you never increment p , so the loop will never end. Also, you never initialise total , so the result will be a random garbage value.
C11dr 6.5.2.1 Array subscripting . "One of the expressions shall have type ‘‘pointer to complete object type’’, the other expression shall have integer type, and the result has type ‘‘type’’." .
With [] , you get to do:
The usage of float is not the issue here, but the usage of a pointer.
An integer type includes types int , unsigned , size_t , long , etc.
I think the OP wanted the following (or something like this)
following this previous question Malloc Memory Corruption in C, now i have another problem. I have the same code. Now I am trying to multiply the values contained in the arrays A * vc and store in res. Then A is set to zero and i do a second multiplication with res and vc and i store the values in A. (A and Q are square matrices and mc and vc are N lines two columns matrices or arrays). Here is my code :
When i try to compile, i have this error :
The corresponding lines are where I store the results in res and A :
Can someone explain me what is this error and how can i correct it? Thanks for your help. 😉
6 Answers 6
mc is of type double. It has to be integral type
mc is pointer to double.
In above, you are indexing A with a value in mc (double array). And, there are other similar cases. If you are sure of the values, cast to int
Your declaration of mc :
And then you use mc multiple times in your array access. For example:
Can you change mc to be an int array instead of a double ?
Looks like you’re using entries in mc , which are doubles, as a part of array subscripts, thus making the entire subscript a double.
If you meant to do this, try casting back to an integer. I don’t know what the context of this problem is, but I’d take a real good look at what you’re doing to ensure you really want to use the contents of mc as a subscript.
The compiler is complaining because the expression you use as an array index evaluates to type double .
In other words, the expression:
. will give you a result which is of type double . You may want to look into the rules for usual arithmetic type conversions in C if you don’t understand why this expression evaluates to a double .
The problem is that array indices must be integral types, like int or long . This is because the array subscript operator in C is basically shorthand for pointer arithmetic. In other words, saying array[N] is the same as saying *(array + N) . But you can’t do pointer arithmetic with non-integral types like float or double , so of course the array subscript operator won’t work that way either.
To fix this, you’ll need to cast the result of your array-indexing expression to an integral type.