ponter doubt abc = *(int*)test;

This is a discussion on ponter doubt abc = *(int*)test; within the C++ Programming forums, part of the General Programming Boards category; ................. int abc; char test[20] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; abc = *(int*)test; .................. Above is a part of c program Can anybody ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    ponter doubt abc = *(int*)test;

    .................
    int abc;
    char test[20] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    abc = *(int*)test;
    ..................

    Above is a part of c program
    Can anybody predict the contents of abc

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by abhijit_mohanta View Post
    Above is a part of c program
    Can anybody predict the contents of abc
    Yeah, since "test" is a pointer to an array abc will contain a memory address. Or part of one on a 64-bit system.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    Yeah, since "test" is a pointer to an array abc will contain a memory address. Or part of one on a 64-bit system.
    Eh, I think you missed the * in front. It would contain the contents of that memory address, interepreted as an int.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Whatever the 4-bytes of "aaaa" points to; effectively the output of [97979797] in low-byte, high-byte order which is in this case 1633771873. Not that it matters b/c that code has an error in it, trying to shove 29 bytes into a 20-byte sequence so the OP should have wrote:
    Code:
    const char * foo = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    Trick question. As-written the code will never compile (unless your compiler is an idiot) so what it will output is undetermined.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    Quote Originally Posted by jeffcobb View Post
    Whatever the 4-bytes of "aaaa" points to; effectively the output of [97979797] in low-byte, high-byte order which is in this case 1633771873. Not that it matters b/c that code has an error in it, trying to shove 29 bytes into a 20-byte sequence so the OP should have wrote:
    Code:
    const char * foo = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    Trick question. As-written the code will never compile (unless your compiler is an idiot) so what it will output is undetermined.
    well, test is an array of char, which converts implicitly to a char pointer, and in this code, it's being explicitly converted to an int pointer, and then dereferenced. this means that abc should contain the value 0x61616161.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Eh, I think you missed the * in front. It would contain the contents of that memory address, interepreted as an int.
    Oh yeah. Well it will definitely go out of bounds then. But as Mr. Cobb points out, hopefully the compiler will save the OP from his/her own silliness.
    Last edited by MK27; 05-18-2010 at 09:31 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    I don't think VS will complain (at least not 2005). I've seen some code that compiles (and works) using syntax like this. It is code that copies an IP address between differing structures, specifically a byte array of length 4 and an in_addr struct.

    *(int *)&a->ip = *(int *)&s->sin_addr;

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by Elkvis View Post
    well, test is an array of char, which converts implicitly to a char pointer, and in this code, it's being explicitly converted to an int pointer, and then dereferenced. this means that abc should contain the value 0x61616161.
    "test is an array of char" .. that's too short. GCC sez:
    Code:
    error: initializer-string for array of chars is too long
    And since code with errors will not compile, test will never be anything. And yes 0x61 is indeed 97 dec.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting ABC order
    By rodrigorules in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2010, 12:37 AM
  2. fscanf search file HELP!
    By datoneperson in forum C Programming
    Replies: 5
    Last Post: 09-05-2008, 07:27 PM
  3. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  4. Doubt abt Storage!
    By kalamram in forum C Programming
    Replies: 1
    Last Post: 04-21-2006, 05:30 AM
  5. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 03:41 AM

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