Thread: pointer issue

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    37

    pointer issue

    I have two questions,

    1
    Code:
    main(){
    
    int i =257;
    
    int *iptr =&i;
    
    printf (“%d  %d”,*((char*)iptr),* ((char *)iptr+1));
    
    }
    the output is 1 1

    here, iptr at first should print the value stored in first 8 bits of variable i and in next it should print value of second 8 bit block.
    would you please explain why the out put is so?


    2--
    Code:
       main()
    
    {
    
    extern int iExtern;
    
    iExtern = 20;
    
    printf(“%d”,iExtern);
    
    }
    why there is an linker error, how should we use the variable so that the error wont come?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What if you change it to:
    Code:
    int i = 258;
    Think about what 257 looks like in binary.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    1) you are casting your int into a char, and 257 as a char is 1.

    2) Prove there is a linker error. I see no link command and no linker error message.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    There certainly is, Dino:
    test.obj : error LNK2001: unresolved external symbol "int iExtern"

    Try to be less sarcastic and more helpful.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    In 1 write out the integer 257 in binary and you will see why the output of your program 1 1 is correct.

    In 2 remove the extern qualifier so the scope of the variable iExtern becomes local instead of global and try not to use reserved keywords for variable names.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. file pointer
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 03:52 PM