Reputation: 21
#include <stdio.h>
#include <string.h>
#include <math.h>
int main ()
{
char buf[] ="14:15:43";
int i;
char *p;
int *array[3];
i = 0;
p = strtok (buf,":");
while (p != NULL)
{
array[i++] = p;
p = strtok (NULL, ":");
}
for (i=0;i<3; ++i) {
//printf("%s\n", array[i]);
array[i] = array[i] * 2;
}
return 0;
}
When I compile the above code I get this error:
split.c: In function ‘main’:
split.c:16:16: warning: assignment from incompatible pointer type
array[i++] = p;
^
split.c:21:25: error: invalid operands to binary * (have ‘int *’ and ‘int’)
array[i] = array[i] * 2;
^
Upvotes: 1
Views: 11833
Reputation: 1901
I was missing a semicolon and got this same error because the compiler thought it was a multiply not a pointer dereference.
I had something like this:
void **ptr = foo;
m->ptr = (void*)(m+1) // missing ;
*ptr = m->ptr;
Oops! The line needs a semicolon because the wrap thought the next line was a multiply!
Upvotes: 0
Reputation: 611
It can also mean something completely different. Think about the meaning of "invalid operand". I was getting it on
fftout[lochead][i][0] * fftout[lochead][i][0]
When I forgot the [i] terms. I have 10 buffers, each with 4096 elements, each of those with 2 elements. I didn't have enough indexes.
And yes, I'm just squaring the number. It's part of a square root of sums of squares demodulation step.
Upvotes: 0
Reputation: 134326
Regarding the error
In your code, array[i]
is of type int *
, which cannot be used as the operand of multiply operator.
To quote the standard, chapter §6.5.5 , Multiplicative operators
Each of the operands shall have arithmetic type.
~~~~see note~~~~~
Regarding the warning
That said,
array[i++] = p;
looks very wrong. array[n]
is of type int *
, and p
is of type char *
and they are not compatible types. You may want to check your logic all over again.
Solution
It looks like, you want to perform arithmetic operation on the numeric value of the string content. FYI, just casting the char *
to int *
does not make the content of the string to appear as int
.
For that, you need to convert the string to arithmatic type first, say int
or long
. strtol()
may be of your help.
[NOTE]
arithmetic type:
Quoting C11
, chapter §6.2.5,
Integer and floating types are collectively called arithmetic types.
Upvotes: 4
Reputation: 223862
A few errors here:
You're defining your array as int *array[3]
i.e. an array of int *
, however what you probably want is int array[3]
, i.e. an array of int
.
Next, as was mentioned elsewhere, array[i++] = p
is invalid because you're trying to assign a char *
to an int *
(or an int
if you first apply the first fix), which is invalid. You want to convert the string to an integer using atoi
as array[i++] = atoi(p)
.
Lastly, your commented out printf
in the for
loop should be using %d
for the format specifier instead of %s
, since the array contains int
. You probably also want to move it down one line, right after array[i] = array[i] * 2
.
With the above changes, you now have this:
#include <stdio.h>
#include <string.h>
#include <math.h>
int main ()
{
char buf[] ="14:15:43";
int i;
char *p;
int array[3];
i = 0;
p = strtok (buf,":");
while (p != NULL)
{
array[i++] = atoi(p);
p = strtok (NULL, ":");
}
for (i=0;i<3; ++i) {
array[i] = array[i] * 2;
printf("%d\n", array[i]);
}
return 0;
}
Which outputs this:
28
30
86
Upvotes: 2