Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 days ago.
Improve this question
assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
31 | array[kategori] = "Best Pick";
| ^
t02.c:36:25: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
36 | array[kategori] = "Must Read";
| ^
t02.c:40:25: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
40 | array[kategori] = "Recommended";
| ^
t02.c:44:25: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
44 | array[kategori] = "Average";
| ^
t02.c:47:25: warning: assignment to 'char' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
47 | array[kategori] = "Low";
i want to know, what " makes integer from pointer without a cast" does mean ?
It means you're trying to set an integer equal to a pointer, like this:
const char * ptrToChar = "hello";
int x = ptrToChar; // error, trying to set an integer equal to the value of a pointer
Also in this case the type of the integer you're trying to set is char (aka a 7-bit or 8-bit integer that is commonly used to hold exactly 1 ASCII character):
const char * ptrToChar = "hello";
char x = ptrToChar; // error, trying to set a char equal to a (const char *)
From the looks of the warnings you are getting, the char you're trying to set is a member of a char-array, so probably it's more like this:
int kategori = 3;
char array[20]; // each element of (array) is a char
array[kategori] = "Recommended"; // error, trying to set a char equal to a (const char *)
Since each element in array is a char, you can only really store a char in it, like this:
array[kategori] = 'R'; // ok (note single quotes around 'R')
... if you wanted to copy an entire string into array, you could do it character-by-character, using a for-loop, or use a libC-supplied function like strncpy() to do it for you:
strncpy(array, "Recommended", sizeof(array));
Setting an integer to the value of a pointer is technically possible (sort of), but it almost never makes sense to do that, which is why the compiler is warning you that you are trying to have it do something that you probably didn't really intend.
Related
I am getting the following warning:
warning: incompatible pointer types passing 'char ()' to parameter of type 'char ()[5]' [-Wincompatible-pointer-types]
printField(field[5]);
The printField function looks like this:
void printField(char (*field)[5])
{
...
}
and the field I am giving to it is defined as follows:
char (*field) = get_field(input);
Here is the function call:
printField(field);
Now, I do understand, that there is obviously some sort of mismatch happening, but I can't tell what to change for it not to be there anymore. I would appreciate it very much, if someone could help me.
Assuming that get_field return a pointer to the first element (character) of an array (null-terminated string), then that happens to be the same as the pointer to the array itself.
If we illustrate it with a simple array:
char s[5];
Then that will look something like this in memory (with pointers added):
+------+------+------+------+------+
| s[0] | s[1] | s[2] | s[3] | s[4] |
+------+------+------+------+------+
^
|
&s[0]
|
&s
Now as can be seen that there are two pointers pointing to the same location: &a[0] and &s.
&s[0] is what using the array decay to, and it's the type char *.
&s is a pointer to the array itself, and have the type char (*)[5].
When we apply it to your case, field is the first pointer, it points to the first element of an array. Which happens to be the same location as the pointer to the array itself which is what printField expects.
That's the reason it "works". But you should not do like that, you should fix the function to take an char * argument instead:
void printField(char *field);
The types are different.
char *ptr; declares the pointer to char
char (*ptr)[5]; declared the pointer to an array of 5 characters.
Those pointer types are not compatible - thus compiler warning.
In your case, both pointers refer to the same place in the memory (and that is the reason why the program works).
Why do I receive a warning? pch is already the pointer I got and when I want to subtract the addresses I use &Origi for that.
C4047 '-' : 'char*' differs in levels of indirection from 'char (*)[12]'
// Substring
char Origi[] = { "Hallo Welt." };
char* pch = strstr(Origi, "Welt"); // “pch” is a pointer that in this example points 6 characters further than the start of “Origi”.
printf("%d\n", pch - &Origi);
printf("%c\n", Origi[pch - &Origi]);
In the snippet:
printf("%d\n", pch - &Origi);
Origi is already of type char* because when you pass an array as argument it decays to a pointer to its first element, passing its address will make it a pointer to array of chars, which is not the same as a pointer to char and will cause a type mismatch in the binary operation.
For the pointer arithmetic to work properly the operands must be of the same type. It should be:
printf("%d\n", pch - Origi);
|_____|____
|
---> same type -> char*
For the second case it's much the same logic, Origi is already a pointer to char. It should be:
printf("%c\n", Origi[pch - Origi]);
|_____|____
|
---> same type -> char*
I'll admit the error issued by msvc is not the best, it should be something like in gcc, for example, which is much more on point:
error: invalid operands to binary - (have 'char* ' and 'char (*)[12]')
Or better yet, in clang:
error: 'char* ' and 'char (*)[12]' are not pointers to compatible types
Here is a C program:
#include<stdio.h>
#include<stdint.h>
int main() {
int32_t *ptr = 5;
printf("ptr is %p\n", ptr);
printf("++ptr is %p\n", ++ptr);
}
The intention is to initialize a pointer with literal address (e. g. 5) and look how it changes after increment.
gcc produces following warning
warning: initialization of 'int32_t *' {aka 'int *'} from 'int' makes pointer from integer without a cast [-Wint-conversion]
6 | int32_t *ptr = 5;
| ^
What is the general way to avoid this warning?
Is "cast" word used as synonym for "explicit cast", so the intended fix is as following?
int32_t *ptr = (int32_t *)5;
Or maybe is there a special syntax for pointer literals?
For the purpose of the program it's possible to declare an additional variable to initialize pointer with its address (like int32_t x, *ptr = &x) or even leave pointer uninitialized, but the question is about this specific warning and initializing pointers with literal values.
You can silence the warning with an explicit cast as you've shown:
int32_t *ptr = (int32_t *)5;
However, the address 5 is most likely not one your process is allowed to write to and would invoke undefined behavior and would most likely crash.
I`ve this function in C, ISO C90
char **read_words(char *file_name);
But when I use it
char **words;
...
words = read_words("file.txt");
...
I've this problem
main.c:72:14: warning: assignment makes pointer from integer without a cast [enabled by default]
words=read_words("file.txt");
^
When I debugg to see what is the problem, the function returns the adress 0x60003bbe0 but the variable 'words' takes the value 0x3bbe0, and I`ve have no idea what's the problem
I am having trouble dynamically declaring a 2D array (one dimension of known size, the other not known until a file is read and the number of lines stored in the integer ·lines·).
int NColDataType = 16;
char *DataType[NColDataType];
DataType = (char *)malloc(sizeof(char)*lines);
When this is compiled, I receive the following error on the 3rd line:
incompatible types when assigning to type 'char
*[(sizetype)(NColDataType)]' from type 'char *'
I am clueless as to what the issue is here, particularly as to why an error is thrown even though the types do seem to match.
The variable DataType is an array of pointer, and you try to assign a pointer to it. I think you mean to do e.g.
DataType[0] = malloc(lines);
Or do you want DataType to be a pointer to an array, like
char (*DataType)[NColDataType];
DataType = malloc(sizeof(DataType[0]) * lines);
DataType is an array of pointers, you can't assign to it. Instead, assign to one of its elements, for instance, the first one:
DataType[0] = malloc(sizeof(char)*lines);
Note that you don't need to cast the result value of malloc. sizeof(char) is guaranteed to be 1, so you don't need it. However, using it can be considered self-documented.