My code is as follows(In bold-part question):
#include "stdafx.h"
#include
using namespace std;;
#define MAX_ARR_SIZE 10
typedef int ElementType;
typedef struct
{
ElementType arr[MAX_ARR_SIZE];
int length;
}MyStr;
void outputE(MyStr *ms){
int i;
for (i = 0; i < ms->length; i++) cout << ms->arr[i] << ",";
cout << ms->arr[i] << endl;
}
int _tmain(int argc, _TCHAR* argv[]){
MyStr ms;
ms.length = MAX_ARR_SIZE;
// try a one-time assignment. but a compiler error has occurred(In addition to each assignment, but are there other ways?).
ms.arr[MAX_ARR_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
outputE(&ms);
system("pause");
return 0;
}
Thanks.

VulpesPosted Dec 29, 2014, 6:30 AM
ElementType et[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
ElementType et[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
as the compiler would infer the size of the array to be 10.
However, this wouldn't work:
So you'll need to use a loop to assign values to the struct member, arr:
for(int i = 0; i < ms.length; i++) ms.arr[i] = i + 1;
Incidentally, I noticed a mistake in your output() method:
Ken HPosted Dec 29, 2014, 9:30 AM