Question

I've made a simple test program to access and increment an int stored at a memory address, which runs correctly, and output 1 when run, however when I make it, I get a warning saying the following: warning: passing argument 1 of ‘insert’ from incompatible pointer type However, I can't see what the issue is, as I'm passing an address to insert, which is declared to accept a pointer.

#include <stdio.h>

static void insert(int *nextfreeplace) {
        (*nextfreeplace)++;
        printf("%i\n", *nextfreeplace);

}
int main() {
        int * latestint = 0;
        insert(&latestint);
        return 0;
}
Was it helpful?

Solution 2

latestint is a pointer to int. Change it to int type.

OTHER TIPS

&latestint is int ** while insert() expects a int *.
As to why it runs correctly, the compiler cast the int ** to int * implicitly, so the address of latestint is passed to the insert(), its value is 0, so you get the output by coincidence. A table would illustrate this clearly.

+------------+------------+--------+-----------+
|  Address   |   Value    |  Type  |   label   |
+------------+------------+--------+-----------+
| 0x12345678 | 0x00000000 | int *  | latestint |
| whatever   | 0x12345678 | int ** | &latestint|
+------------+------------+--------+-----------+

In C language, a pointer is a variable. And like any variable, each pointer has a type. In your code, the variable "latestint" is a pointer of type "int":

int * latestint = 0;

Values also has their types (5 is int, 'a' is char, etc.). The expression "&latestint" is the address of the variable "latestint". This address is a value (a number). Since "latestint" is a pointer (a variable that contains the address of another variable), "&latestint" is the adress of a pointer(an int pointer), so the type of "&latestint" is int**.

Then, when you pass &latestint to the function "insert()", the compiler tell you that "&latestint" is of type int** but the function "insert()" wait for an int*.

You just have to remove the "*" in the declaration of "latestint" and then the type of "&latestint" will be "int *".

(You can also modify "insert" so that it wait for a int**. I guess it's not what you wanted to do, and the results may be surprising)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top