Thread: Pointer...eh?

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by elnerdo
    Don't try to be a smart.alleck

    Look, the point of that program is to output the numbers in the array. Yours doesn't accomplish that.
    Who's the idiot?

    Quote Originally Posted by 7stud
    Code:
    int a[] = {0,1,2,3,4,5,6,7,8,9};
    for(int i = 0; i < 10; cout<<a[ i ], i++)
    ;
    This most definately does display the numbers in an array. How exactly doesn't this display the contents of the array? You do know what an array is, right? You should then know that the variable a is in fact an array. You should then also know that "cout<<a[i]" is outputting the contents of the array at that element.

    Again, who's the idiot?

    Quote Originally Posted by elnerdo
    The one with the pointer accomplishes that task and uses one less variable than the one with the array and the index number.
    No it doesn't. It could. But there's nothing guarinteeing that it does in fact only use one variable. Perhaps you should actually look at you code. Here, I'll help, because you seem to be mistaken:
    Quote Originally Posted by elnerdo
    Code:
          int a[] = {0,1,2,3,4,5,6,7,8,9};
          int *ptr;
    
          ...snip...
    
          for(ptr = a; ptr < a + 10; ptr++)
            printf("%d",*ptr);
    There is absolutely nothing that states that the compiler must optimize that check. Therefore, you do in fact use two variables every single time through your loop. One is the pointer. The other is the variable a.

    Again, your compiler might optimize that test. But nothing at all in the C++ language says that will happen. Therefore, by the language itself, you are in fact using two variables every time through the loop.

    It's alright to be a smart ass, if you know what you're talking about. Otherwise you just end up looking like a dumbass.

    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    There is absolutely nothing that states that the compiler must optimize that check.
    How would a compiler go about optimizing that check?

  3. #18
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    Oh, I'm sorry, I didn't see the cout in there.

    for(ptr = a; ptr < 10; ptr++)
    printf("%d",*ptr);
    printf("\n");

    There, happy? now it's one variable plus a static against two plus a static.

    I'm fairly sure that it would be more efficient to do it the pointer way than the index way.
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by elnerdo
    Oh, I'm sorry, I didn't see the cout in there.

    for(ptr = a; ptr < 10; ptr++)
    printf("%d",*ptr);
    printf("\n");

    There, happy? now it's one variable plus a static against two plus a static.

    I'm fairly sure that it would be more efficient to do it the pointer way than the index way.
    Did you get that to compile? I'm wondering how you can compare an address like 006BFDEC to an integer like 10, e.g. ptr < 10.
    Last edited by 7stud; 05-05-2005 at 07:22 PM.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by 7stud
    How would a compiler go about optimizing that check?
    Theoretically, it would take the initial address, add the fixed value 10 to it, and replace all other checks with that new number. So if the address were "10", it would do a one-time add of 10 to it, making it "20", and then test against "20" every time through the loop. But again, it doesn't have to do that at all. Also, were 'a' an actual pointer, rather than an array name, it would most likely NOT optimize that, as the pointer 'a' could be changed in the loop, so it would definately test against it every single time through.

    Quote Originally Posted by elnerdo
    Oh, I'm sorry, I didn't see the cout in there.

    for(ptr = a; ptr < 10; ptr++)
    printf("%d",*ptr);
    printf("\n");

    There, happy? now it's one variable plus a static against two plus a static.
    [amused] I'll be amazed if this displays anything other than a newline. [/amused]

    Like I said, you can only be a smart ass if you know what you're talking about.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  4. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  5. 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