Thread: Simple c

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Simple c

    Code:
    int main()
       { 
              
     int a[] ={ 1,2,3,4,5,6,7};
    char c[] = {' a','x','h','o','k'};
    printf("%d %d ",&a[0],&a[3]);//prints address with with the diffence of 12
    printf("\n%d\t %d ", (&a[3]-&a[0]),(&c[3]-&c[0]));//but how come the diffrence is //3 ?????????????????????

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to C programming forum.

    Quote Originally Posted by dpp
    //prints address with with the diffence of 12
    You should actually be using the %p format specifier to print pointers, and cast the pointers to void* if they are not already of that type.

    Quote Originally Posted by dpp
    //but how come the diffrence is //3
    Because pointer arithmetic works that way. Add 1 to a pointer and you get a pointer to the next element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    So many types in your code. Please verify your code prior to a cut-n- paste. Please indent. This is requested so much in these forums. You are missing a dereference operator for your first/second printf() calsl, that is if I understand your intent here.

    Are you wanting to subtract the values the index references or the memory addresses; if you are wanting the memory values, see Laserlight's suggestion.
    Last edited by slingerland3g; 12-11-2009 at 12:14 PM. Reason: laserlights suggestion

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    This was a debugging question...I was puzzled with it...
    what if i need to print the diffrence in the address.. shud i store the address in a pointer variable and subtract it

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Try this, if you are wanting to view the addresses and their spread between each element.

    Code:
       printf ("%p %p ", &a[0], &a[3]);    /*Change specifier from %d to %p */
       printf ("\n%p\t %p ", (&a[3] - &a[0]), (&c[3] - &c[0]));      /*Difference should be 3 */

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    what if i need to print the diffrence in the address.. shud i store the address in a pointer variable and subtract it
    You just need to multiply the pointer difference by the sizeof the type.

    Quote Originally Posted by slingerland3g
    Try this, if you are wanting to view the addresses and their spread between each element.
    More accurately:
    Code:
    printf("%p %p\n", (void*)&a[0], (void*)&a[3]);       /* Change specifier from %d to %p */
    printf("%d %d\n", (&a[3] - &a[0]), (&c[3] - &c[0])); /* Difference should be 3 */
    I note that in the original code a multi-character literal was used, but that is probably just a typo error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by laserlight View Post
    You just need to multiply the pointer difference by the sizeof the type.


    More accurately:
    Code:
    printf("%p %p\n", (void*)&a[0], (void*)&a[3]);       /* Change specifier from %d to %p */
    printf("%d %d\n", (&a[3] - &a[0]), (&c[3] - &c[0])); /* Difference should be 3 */
    I note that in the original code a multi-character literal was used, but that is probably just a typo error.

    Ah.. (void*)...yes, thanks!

    PS: nice peer review there!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM