| | |
*Pointer program problem.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2004
Posts: 65
Reputation:
Solved Threads: 0
#include <iostream>
using namespace std;
int main()
{
int age;
for (int i=0;i<5;i++)
{
cout << "Enter :";
cin >> age;
}
int *sum=&age;
for ( i=0;i<5;i++)
{
cout <<*(sum+i) << endl;
}
return 0;
}
here is one of my program that i wrote.
i do not know whats the problem with it.
it seems can't display back the values that i've entered in.
can sir/miss help me to correct my mistake(s).
Thank you!
using namespace std;
int main()
{
int age;
for (int i=0;i<5;i++)
{
cout << "Enter :";
cin >> age;
}
int *sum=&age;
for ( i=0;i<5;i++)
{
cout <<*(sum+i) << endl;
}
return 0;
}
here is one of my program that i wrote.
i do not know whats the problem with it.
it seems can't display back the values that i've entered in.
can sir/miss help me to correct my mistake(s).
Thank you!
C++ Syntax (Toggle Plain Text)
int age;
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
An array is declared:
datatype array_name[int_size];
Eg. int vals[10];
int *p = vals;
Then p[0] = vals[0] and p[1] = vals[1] and so on.
Also you can access the vals by *(p+i) instead of the [], and the expression is read as the value at the address of p+i.
An array var is also a pointer so you dont need a & in front of it when you assign it to another pointer.But if you want to just point to a single int for example:
int *p = &val; //means p = address of val
and you can access the value a the address pointed to by p usinf another_int = *p ;
---Helps???
datatype array_name[int_size];
Eg. int vals[10];
int *p = vals;
Then p[0] = vals[0] and p[1] = vals[1] and so on.
Also you can access the vals by *(p+i) instead of the [], and the expression is read as the value at the address of p+i.
An array var is also a pointer so you dont need a & in front of it when you assign it to another pointer.But if you want to just point to a single int for example:
int *p = &val; //means p = address of val
and you can access the value a the address pointed to by p usinf another_int = *p ;
---Helps???
![]() |
Similar Threads
- Modifying pointer program (C)
- Incompatible pointer type problem (C)
- Null Pointer Exception problem (Java)
- pointer passing problem (C)
Other Threads in the C++ Forum
- Previous Thread: Input a "password" string
- Next Thread: New to C++ Pointers and need help with identifying where the errors are
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






