Error in Peter D Minns book on C

Discussion in 'C++' started by Rocky48, Jun 16, 2016.

  1. Rocky48

    Rocky48 New Member

    Joined:
    Jun 11, 2016
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Retired
    Location:
    Hastings, UK
    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);
    }
     
  2. Rocky48

    Rocky48 New Member

    Joined:
    Jun 11, 2016
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Retired
    Location:
    Hastings, UK
    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?
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Just assign &i directly to pi itself, without the dereferencing. i.e.:
    Code:
    pi = &i;
    
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice