|
-
Jan 17th, 2003, 12:24 PM
#1
Thread Starter
Frenzied Member
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?
-
Jan 17th, 2003, 12:57 PM
#2
Monday Morning Lunatic
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
-
Jan 17th, 2003, 01:08 PM
#3
Thread Starter
Frenzied Member
What other operators do that?
-
Jan 17th, 2003, 01:14 PM
#4
Monday Morning Lunatic
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
-
Jan 17th, 2003, 01:39 PM
#5
Thread Starter
Frenzied Member
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.
-
Jan 17th, 2003, 11:05 PM
#6
Hyperactive Member
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.
-
Jan 19th, 2003, 11:30 PM
#7
Thread Starter
Frenzied Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|