Thread: Printing pointers

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    Printing pointers

    I came across printing pointers example on C programming book (C programming for the Absolute Beginner, Second Edition). Unfortunately, the example doesn't work. I typed it correctly and i believe it's the correct code. I just doesn't know it dont work. I tried both C Free and Visual Studio.
    Code:
    #include <stdio.h>
    
    main(){
    	
    	int x = 1;
    	int *Ptr;
    	
    	iPtr = &x;
    	*iPtr = 5;
    	
    	printf("\n*iPtr = %p\n&x = %p\n", iPtr, &x);
    	getchar();
    }
    Keep saying iPtr is uninitalize ! Thanks
    Last edited by ncode; 11-19-2011 at 12:36 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... here are the errors I got from Pelles C...
    Code:
    D:\Programming\C_Code\Experiments\junk\main.c(3): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(8): error #2048: Undeclared identifier 'iPtr'.
    D:\Programming\C_Code\Experiments\junk\main.c(8): error #2168: Operands of '=' have incompatible types 'int' and 'int *'.
    D:\Programming\C_Code\Experiments\junk\main.c(9): error #2144: Type error: pointer expected.
    D:\Programming\C_Code\Experiments\junk\main.c(11): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'void *' but found 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(6): warning #2114: Local 'Ptr' is not referenced.
    Line 3 ... it's ... int main (void) ...
    line 8 ... iPtr is not declared... I think you meant ... Ptr = &x;
    line 9 ... again iPtr doesn't exist.
    line 11 ... it's that iPtr thing again
    Line 6 ... Ptr is declared but never used

    And... main() is supposed to return an integer value... so you need something like... return 0; ... before the closing brace.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    RE:

    Quote Originally Posted by CommonTater View Post
    Ok... here are the errors I got from Pelles C...
    Code:
    D:\Programming\C_Code\Experiments\junk\main.c(3): warning #2099: Missing type specifier; assuming 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(8): error #2048: Undeclared identifier 'iPtr'.
    D:\Programming\C_Code\Experiments\junk\main.c(8): error #2168: Operands of '=' have incompatible types 'int' and 'int *'.
    D:\Programming\C_Code\Experiments\junk\main.c(9): error #2144: Type error: pointer expected.
    D:\Programming\C_Code\Experiments\junk\main.c(11): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'void *' but found 'int'.
    D:\Programming\C_Code\Experiments\junk\main.c(6): warning #2114: Local 'Ptr' is not referenced.
    Line 3 ... it's ... int main (void) ...
    line 8 ... iPtr is not declared... I think you meant ... Ptr = &x;
    line 9 ... again iPtr doesn't exist.
    line 11 ... it's that iPtr thing again
    Line 6 ... Ptr is declared but never used

    And... main() is supposed to return an integer value... so you need something like... return 0; ... before the closing brace.
    OMG. I have a terrible book

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ncode View Post
    OMG. I have a terrible book
    Ok... so get a better one... like THIS

    Start on page 1... read every word, type up and compile all the examples, do the exercises and quizzes. Play with the code, change it, break it, fix it, learn what does and does not work... turn to the next page... repeat until you get to the end of the book.

    FWIW... I'm betting it's a typo on line 6 where it should have been... int *iPtr;

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Or get any of these books: C Book Recommendations

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    RE:

    I borrowed this from my school library. I like this book , because it's simple and easy to understand. Unfortunately, there are some kind of error in it.--------busted

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Is it just me, or is every C book ever written listed in that thread?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and printing distinct elements in array
    By dave_the_bear10 in forum C Programming
    Replies: 2
    Last Post: 10-29-2011, 01:31 AM
  2. Printing null pointers
    By SterlingM in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2010, 06:04 PM
  3. Printing pointers of an array
    By TIMBERings in forum C Programming
    Replies: 7
    Last Post: 10-10-2009, 11:35 PM
  4. printing elements of an array using pointers
    By zeebo17 in forum C Programming
    Replies: 3
    Last Post: 05-22-2009, 09:30 PM
  5. Printing pointers with printf()
    By blacknail in forum C Programming
    Replies: 2
    Last Post: 05-25-2008, 08:15 AM