Pinter aurithmetic

This is a discussion on Pinter aurithmetic within the C++ Programming forums, part of the General Programming Boards category; Hi Guys. I am doing the final question for my homework sheet and I am having alot of trouble with ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    Pinter aurithmetic

    Hi Guys.

    I am doing the final question for my homework sheet and I am having alot of trouble with it.

    I have to print the following:

    Code:
    int brainTeaser[ ARRAY_SIZE ] = { 1, 7, 11, 27, 77, 107, 111, 127, 177 }
    Using pointer aurithmetic and I am not allowed to use indexing notation.
    This is proving harder than I thought. I have come up with this:

    Code:
    #include <iostream>
    
    // main function - driver /////////////////////////////////////////////////////
    //
    int main ( void ) {
    	const int ARRAY_SIZE = 9;
    
    	int brainTeaser[ ARRAY_SIZE ] = { 1, 7, 11, 27, 77, 107, 111, 127, 177 };
    
    	int *ptr = brainTeaser;
    
    	for ( ; *ptr < ARRAY_SIZE; ( ptr )++ ) {
    		std::cout << *ptr << " ";
    	}
    
    	std::cin.get();
    
    	return 0;
    }
    Which appears to work ok, but then prints out only 1 and 7. If I change the
    < to != it prints the array ok but also it prints two more uknown addreses which means I have gone beyond the array bounds.

    Could anyone give me any help on where I could be going wrong? I am sure my (ptr)++ is correct. But then again could it be the size I have got wrong in the for loop?

    Any hints apprciated.
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    *ptr < ARRAY_SIZE;
    That is not the right way to determine that you have reached the end [unless of course your array looks like this
    Code:
    int brainTeaser[ ARRAY_SIZE ] = { 1, 7, 11, 27, 77, 107, 111, 127, 177, ARRAY_SIZE };
    (Of course, the ARRAY_SIZE should then be 1 larger than now).

    Perhaps you want to take the difference between ptr and brainTeaser?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The *ptr < ARRAY_SIZE is wrong.
    Code:
    for (int i =0 ; i < ARRAY_SIZE; ++i)
       cout << *(ptr + i) << ' ';
    your code wold make sense like this:
    Code:
    int last_element = 177
    for (; *ptr != last_element; ++ptr)
        cout << *ptr << ' ';
    cout << *ptr << ' ';  //print also last element

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    C_ntua, don't spoil the answer.
    Furthermore, your code is not much better than swgh's.
    Still, I am curious...
    What do you think *ptr < ARRAY_SIZE does, swgh? Why are you using it in the first place?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote Originally Posted by swgh
    Using pointer aurithmetic and I am not allowed to use indexing notation.
    Cheat: first write it out using index notation, then convert every a[n] to *(a+n).

    On the other hand, you are probably not supposed to use a counter at all, so this cheat might be forbidden... but it can still help since then you just change the counter to a pointer, and instead of using some upper bound, use a pointer that is one past the end (cosmetic changes, as it were, since an optimising compiler will do something similiar anyway).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Elysia I assumed at first *ptr began at zero like an array does. so i could use it as i woud in a normal array

    Code:
    for ( int i = 0; // this is the pointer starting value i < ARRAY_SIZE; i++ )
    I had learned that to use pointer aurethmetic you had to do this: (ptr)++ so that is why I had that as the end
    of the for loop instead of using the i++ index as i could with a normal array.

    I can see now how wrong that is but it is good to make mistakes as you learn from them, and pointers are never simple.
    Last edited by swgh; 01-08-2009 at 05:01 AM.
    I'm just trying to be a better person - My Name Is Earl

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Code:
    p < brainTeaser + ARRAY_SIZE
    brainTeaser + ARRAY_SIZE computes the address one past the in the array

    p holds the current address

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    Quote Originally Posted by swgh View Post
    Elysia I assumed at first *ptr began at zero like an array does. so i could use it as i woud in a normal array
    Your mindset is correct, I think.
    You must stop "assuming" and "stealing" common ways to do things like this.
    First, you must learn the basics. Then you can look at those ways and see how they really work, and then implement your solution.
    The "*ptr" simply dereferences the pointer, and what does that do, do you know?

    I had learned that to use pointer aurethmetic you had to do this: (ptr)++ so that is why I had that as the end
    of the for loop instead of using the i++ index as i could with a normal array.
    ptr++ will advance the pointer by sizeof(T) bytes. And in an array, each element is sizeof(T) bytes, so it will advance to the next element.

    I can see now how wrong that is but it is good to make mistakes as you learn from them, and pointers are never simple.
    Tsk, tsk. Pointers are so simple. People really never learn the proper basics.

    So let us begin with this:
    How would you iterate over a normal array, not using pointers?

    Quote Originally Posted by KIBO View Post
    ...Solution...
    KIBO, are we handing out solutions again?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590
    it's interesting that I never knew until now that if you increment a pointer to a type T, it actually adds sizeof(T) to the pointer address. I always thought it would just add 1 to the address. I'm sure that this is something very elementary in C++, but the books I have don't really cover pointers very well, so I never learned that bit of info. definitely good to know.

  10. #10
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by laserlight View Post
    Cheat: first write it out using index notation, then convert every a[n] to *(a+n).

    On the other hand, you are probably not supposed to use a counter at all, so this cheat might be forbidden... but it can still help since then you just change the counter to a pointer, and instead of using some upper bound, use a pointer that is one past the end (cosmetic changes, as it were, since an optimising compiler will do something similiar anyway).
    wouldn't it be

    a[n] = *(a+n*sizeof(type)) ?

    edit: i really should read the whole thread before posting... :\

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote Originally Posted by m37h0d
    wouldn't it be

    a[n] = *(a+n*sizeof(type)) ?
    No, it would not. As Elysia stated, ptr++ causes ptr to point to the next element. The address of the next element is sizeof(*ptr) (or equivalently, sizeof(type)) bytes away from the address that the pointer contains, but conceptually (and in the text of the C++ standard) that is secondary to the fact that the ptr would point to the next element.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Start with the address of the first item, then take the address of one-past the last item, stop iterating when your loop hits that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick pinter question
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 11-11-2005, 12:54 AM
  2. how to control pinter port read/write using C++
    By lwong in forum Windows Programming
    Replies: 4
    Last Post: 11-25-2003, 09:02 PM

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