Results 1 to 7 of 7

Thread: Pointer question

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Pointer question

    [code]#include <iostream>
    #include <cstdlib>
    void funk(int *);
    const int size=8;
    int arr[size]={7,6,8,1,4,3,6,2};

    int main()
    {
    funk(arr);

    system("PAUSE");
    return 0;
    }

    void funk(int *a){
    for (int j=0; j<size; ++j)
    if (a[j]> a[j+1]) cout<<a[j]<<" ";
    }[\code]

    Why doesn't this part: if (a[j]> a[j+1]) cout<<a[j]<<" "; have to have * before a?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    a[i] is equivalent to *(a + i). The [] operator automatically dereferences, by selecting a specific index into a pointed-at memory location.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    What other operators do that?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Um, none. [] is the array-index operator, and that's all it does. I don't see what you want here.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I just want to understand everything becuase if I don't I will be lost.

    I know they are not formatted correctly.

    This program works fine without ++*p

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    void UP(char *, int *);
    int main() { char n[50]="hi 5T/+f"; int g=2;    UP(n, &g);    cout<< n<< g;
    system("PAUSE");    return 0;}
    
    void UP(char *p, int *d) {
    while (*p!='\0') { *p=toupper(*p);    ++p; }   *d=*d * *d; }
    This one doesn't:

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    void UP(int *);
    const int size=5;
    
    int main()
    {
    int n[size]={2, 3, 3, 7, 8};
    
    UP(n);
    for (int i=0; i<size; i++)
    cout<<n[i]<<endl;
    system("PAUSE");
    return 0;
    }
    
    void UP(int *p){
    for (int i=0; i<size; i++){
    *p=p[0] + p[1];
    ++*p;
    }}
    Last edited by aewarnick; Jan 17th, 2003 at 02:04 PM.

  6. #6
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    The second program ++*p increment the contents of what p points to, while the first program increment the pointer p.


    I just want to understand everything becuase if I don't I will be lost.
    True.

  7. #7

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width