hello Ive been trying to learn pointers and Im having a hard time with them, after doing all the exercises from various websites and searching through posts Im still none the wiser really, my little pointer exercise program is not changing the value of an array of struct im sending it.
#include <iostream>
#include <conio.h>
usingnamespace std;
struct mystruct{
int number;
char name;
};
int passmypointer(int p, int l);
void changemyarray(int *pointer, int size);
void changemystruct(int *arraypointer, int size);
int main()
{
mystruct myarraystruct[10];
for (int i = 0; i < 10; i++){
myarraystruct[i].number = i + 1;
myarraystruct[i].name = 'A';
}
for (int i = 0; i < 10; i++)
cout << myarraystruct[i].number << " " << myarraystruct[i].name << endl;
int *myarraypointer;
myarraypointer = newint[10];
if (myarraypointer == '\0')
exit(0);
elsefor(int i = 0; i < 10; i++)
myarraypointer[i] = i * i;
for(int i = 0; i < 10; i++)
cout << myarraypointer[i] << " " << &myarraypointer[i] << endl;
int *p;
int **l;
int ***z;
int n = 2523;
int v = 450;
int end = 101011;
cout << "Address of V : " << &v << endl;
cout << "Value of n : " << n << endl;
cout << "Address of n : " << &n << endl;
p = &n;
cout << "Value of *p : " << *p << endl;
cout << "Address of p : " << &p << endl;
l = &p;
cout << "Value of **l : " << **l <<endl;
cout << "Address of l : " << &l << endl;
cout << "Value after passing :" << passmypointer(**l, *p) << endl;
cout << "Value after function :" << **l << endl;
z = &l;
cout << "Z = " << ***z << endl;
int *y;
y=&v;
cout << "Address of *y :" << &y << endl;
cout << "Value pointed to by *y :" << *y << endl;
cout << endl << "Point at something else " << endl << endl;
p = &end;
y = &end;
*l = &end;
cout << "P = " << *p << " Address = " << &p << endl;
cout << "Y = " << *y << " Address = " << &y << endl;
cout << "l = " << **l << " Address = " << &l << endl;
changemyarray(myarraypointer,10);
cout << "After passing my array" << endl;
for(int i = 0; i < 10; i++)
cout << myarraypointer[i] << " " << &myarraypointer[i] << endl;
cout << "After passing my struct" << endl;
changemystruct(myarraystruct,10);
for (int i = 0; i < 10; i++)
cout << myarraystruct[i].number << " " << myarraystruct[i].name << endl;
getch();
return 0;
}
int passmypointer(int p, int l)
{
return p + l;
}
void changemyarray(int *pointer, int size)
{
for(int i = 0; i < size; i++)
pointer[i] = 0;
}
void changemystruct(int *arraypointer, int size)
{
for(int i = 0; i < size; i++){
arraypointer[i].number = 0;
arraypointer[i].name = 'B';
}
}
Error Im getting is
1>------ Build started: Project: pointer, Configuration: Debug Win32 ------
1>Compiling...
1>point.cpp
1>c:\users\mikey\documents\visual studio 2008\projects\pointer\pointer\point.cpp(95) : error C2664: 'changemystruct' : cannot convert parameter 1 from 'mystruct [10]' to 'int *[]'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\mikey\documents\visual studio 2008\projects\pointer\pointer\point.cpp(121) : error C2228: left of '.number' must have class/struct/union
1> type is 'int *'
1>c:\users\mikey\documents\visual studio 2008\projects\pointer\pointer\point.cpp(122) : error C2228: left of '.name' must have class/struct/union
1> type is 'int *'
struct mystruct{
int number;
char name;
};
int passmypointer(int p, int l);
void changemyarray(int *pointer, int size);
void changemystruct(int *arraypointer, int size); //<<====
.....
//skip a lot of stuff here
void changemystruct(int *arraypointer, int size)//<<=====
{
for(int i = 0; i < size; i++){
arraypointer[i].number = 0;
arraypointer[i].name = 'B';
}
}
Check carefully - is int* the correct type for the first parameter ????