I have a code, but have a warning when it was compiling, this was the warning:
1_redis.c: In function \342\200\230main\342\200\231:
1_redis.c:131:23: warning: assignment makes integer from pointer without a cast
[enabled by default]
it says assignment makes integer from pointer without a cast, but cFile and lQueryData are all char* type, why?
#define MAX_LINE_NUM 8000000
#define EACH_THREAD_NUM 10000
long i,posF,posDB;
for (i=0;i<DB_NUM;i++) { lQueryPerDB[i] = 0; }
char *lQueryData = (char *)malloc(DB_NUM*MAX_LINE_NUM*sizeof(char));
lLineCount = lFileLen / lLineLen;
for (i=0;i<lLineCount;i++) {
posF = i * lLineLen;
iDB = get_DB(cFile[posF]);
posDB = iDB * MAX_LINE_NUM + lQueryPerDB[iDB];
lQueryData[posDB] = &cFile[posF]; // this line have warning!!!!
lQueryPerDB[iDB]++;
}
If
cFileis achar *, indexing it likecFile[posF]is the same as doing*(cFile + posF), or “give me the value of whatever isposFplaces from the start of the array.” Your address-of operator (&) is unnecessary, and if it weren’t, it would probably be the opposite of what you wanted (you want dereference —*— which is automatically done for you with the subscript notation).As other people have suggested, the correct code is most likely:
The reason you get the particular warning you do is because using
&cFile[posF]gets the address of (i.e., a pointer to) the character incFileatposF. However, when you then try to assign it to a place in thelQueryDataarray, it has to first cast that (internally) into a scalar type. A pointer is just a number that indexes into memory, and thus when used for anything but “pointing” to things, it’s just an integer. Therefore, what you have is making an integer, from a pointer, without an explicit cast.You might consider compiling with clang instead of (what I presume is) GCC if you have access to it, it has much more accessible error messages. For instance, the error message for your code above is: warning: incompatible pointer to integer conversion assigning to ‘char’ with an expression of type ‘char *’; remove & (with a little visual pointer to the offending “&” sign).