代码:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
typedef struct
{
double dvalue;
double* dpointer;
double dArray[20];
int nmax;
}test_t;
int initTest(test_t* test)
{
memset(test, 0, sizeof(test_t));
test->nmax = 20;
test->dpointer = (double*)malloc(sizeof(double) * test->nmax);
return 1;
}
int main()
{
test_t test1 = { 0 };
test_t test2 = { 0 };
initTest(&test1);
initTest(&test2);
test1.dpointer[1] = 10.0;
test1.dArray[1] = 10.0;
printf("test1 dpointer addr:%p,test2 daary addr:%p\n", test1.dpointer, test2.dpointer);
printf("test1 dArray addr:%p,test2 dArray addr:%p\n", test1.dArray, test2.dArray);
cout << "*****after test2=test1*****" << endl;
test2 = test1;
test2.dpointer[1] = 20.0;
test2.dArray[1] = 20.0;
cout << "test1 dpointer value:" << test1.dpointer[1] << ",test2 dpointer value:" << test2.dpointer[1] << endl;
cout << "test1 dArray value:" << test1.dArray[1] << ",test2 dArray value:" << test2.dArray[1] << endl;
printf("test1 dpointer addr:%p,test2 daary addr:%p\n", test1.dpointer, test2.dpointer);
printf("test1 dArray addr:%p,test2 dArray addr:%p\n", test1.dArray, test2.dArray);
getchar();
}
输出: