[flydream@flydream ThinkingInC++]$ cat -n array.cpp
1 #include <iostream>
2
3 using namespace std;
4
5 int main(int argc, char **argv)
6 {
7 int a[10];
8 cout << a << endl;
9 cout << &a[0] << endl;
10
11 int *ip = a;
12 cout << ip << endl;
13 for (int i = 0; i < 10; i++)
14 {
15 ip[i] = i;
16 }
17
18 for(int i = 0; i < 10; i++)
19 {
20 cout << a[i];
21 }
22
23 return 0;
24 }
[flydream@flydream ThinkingInC++]$ g++ array.cpp
[flydream@flydream ThinkingInC++]$ ./a.out
0xbfc2db0c
0xbfc2db0c
0xbfc2db0c
0123456789[flydream@flydream ThinkingInC++]$
[flydream@flydream ThinkingInC++]$ cat ArrayArg.cpp
#include <iostream>
#include <string>
using namespace std;
void func1(int a[], int size)
{
for(int i = 0; i < size; i++)
{
a[i] = i * i - i;
}
}
void func2(int *a, int size)
{
for(int i = 0; i < size; i++)
{
a[i] = i * i + i;
}
}
void print(int a[], string name, int size)
{
for(int i = 0; i < size; i ++)
{
cout << name << "[" << i << "] = " << a[i] << endl;
}
}
int main(int argc, char **argv)
{
int a[5], b[5];
print(a, "a", 5);
print(b, "b", 5);
func1(a, 5);
func1(b, 5);
print(a, "a", 5);
print(b, "b", 5);
func2(a, 5);
func2(b, 5);
print(a, "a", 5);
print(b, "b", 5);
return 0;
}
[flydream@flydream ThinkingInC++]$ g++ ArrayArg.cpp
[flydream@flydream ThinkingInC++]$ ./a.out
a[0] = 134516040
a[1] = 1
a[2] = 65535
a[3] = 1
a[4] = 134514345
b[0] = 134516256
b[1] = 1176874996
b[2] = 1183695512
b[3] = 1183695504
b[4] = -1080637320
a[0] = 0
a[1] = 0
a[2] = 2
a[3] = 6
a[4] = 12
b[0] = 0
b[1] = 0
b[2] = 2
b[3] = 6
b[4] = 12
a[0] = 0
a[1] = 2
a[2] = 6
a[3] = 12
a[4] = 20
b[0] = 0
b[1] = 2
b[2] = 6
b[3] = 12
b[4] = 20
[flydream@flydream ThinkingInC++]$