I am new to C programming and am going through Peter D Minns book (C Programming for PC and MAC and the Arduino). I am doing the exercise on Pointers (Program 5.1) but it errors when I compile. The error is: Invalid conversion from int* to int. From looking at the internet I guess its because i is defined as int = 10, but *pi is an adddress. How do you correct this? Here is the code: Code: #include <stdio.h> #include <conio.h> int main() { int i=10; int *pi; *pi = &i; i++; printf("\n\n\nThis is address of i %x\n ",&i); printf("and this is contents of i %d\n",i); printf("This is address of pointer pi %x\n",pi); printf("and this is contents of this address, pointed to by *pi %x\n",*pi); printf("data i is %d\n",i); return(0); }
I looked at TutorialsPoint on the web and tried their tutorial on pointers. I noticed that the line: *pi = &i in their tutorial did not have an * before it, so I removed and it compiles OK. However I was getting the same error on the next exercise (Program 4.2), but the same does not apply here. Here is program4.2: Code: #include <stdio.h> int main(void) { int x; /*What is happening here? */ int address; address=&x; /*What is happening here? */ x=25; /*What is the value of address after this instruction? */ printf("%x\n",address); printf("%d\n",*x); printf("%x\n", x); return(0); }/*end of main.*/ It errors on line 5. It also has another error on line 8 :- Invalid type argument unary! Can any one help me understand what is going on?
Regarding your second post, x and address are both integers and neither is a pointer. If address is meant to be used as the address of x then it should be defined accordingly, i.e.: Code: int *address; And to answer the question "x=25; /*What is the value of address after this instruction? */": address will be unchanged, since x will stay at the same location in memory after its value is changed. If they mean "What is the value of *address after this instruction?", which would make sense, then the answer would be 25.