Simple Pointers Problem.

This is a discussion on Simple Pointers Problem. within the C Programming forums, part of the General Programming Boards category; Code: int a[]={0,1,2,3,4}; int *p; for(p=a+4;p>=a;p--) { printf("%d",a[p-a]); puts(""); } by address airthematic... let base address is 100 then a+4=108... ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Simple Pointers Problem.

    Code:
    
    int a[]={0,1,2,3,4};
    int *p;
    for(p=a+4;p>=a;p--)
    {
      printf("%d",a[p-a]);
      puts("");
    }
    
    by address airthematic...
    let base address is 100 then a+4=108... because int usses 2 bytes....(100+4*2=108)...
    so a[p-a] should be a[8]...
    and should be error...
    but output is...
    4
    3
    2
    1
    0


    why so please someone explain.....

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,817
    Quote Originally Posted by tushars
    by address airthematic...
    let base address is 100 then a+4=108... because int usses 2 bytes....(100+4*2=108)...
    so a[p-a] should be a[8]...
    You are being inconsistent

    Consider the first iteration. We know that p = a + 4. Hence, by substitution, a[p-a] would be a[a+4-a], which is a[4].

    Now, if we consider your argument with addresses: the value of p is 108, and the value of a is 100. p-a = 8. But then we have to divide by 2 (i.e., by sizeof(int)), thus 8/2 = 4. So, you remembered to multiply the constant by sizeof(int) when adding it to the pointer, but forgot to divide by sizeof(int) when subtracting pointers to get an index offset.
    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
    Join Date
    Sep 2010
    Posts
    3
    thanks for your help.... but i really was not knowing that we have to divide it by 2.... anyways thank you....do you know code for 0/1 KNAPSACK PROBLEM... BY DYNAMIC PROGRAMMINg....????

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    I Am Having A Subject Of Computer Graphics In this Sem.... In This Subject Practicals Are Done In 'C'..... But I dont Know Nothing about functions of <graphics.h>.... so please can you suggest some good BEGINNERS book for learning graphics programming using 'C'..... Thanks A LoT.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  2. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Replies: 5
    Last Post: 12-03-2003, 04:47 PM
  5. Simple class problem
    By savageag in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2003, 10:50 AM

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