pointer issue

This is a discussion on pointer issue within the C Programming forums, part of the General Programming Boards category; I have two questions, 1 Code: main(){ int i =257; int *iptr =&i; printf (“%d %d”,*((char*)iptr),* ((char *)iptr+1)); } the ...

  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
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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,058
    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, 03:29 PM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2007, 12:26 AM
  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, 01:04 PM
  5. file pointer
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 04:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21