나는 인덱스 [1 ... 20] 배열을 가지고 있습니다. indicesArray의 처음 4 개 요소는 특정 유형의 파일 (유형 A)에 연결되고 다른 16 개는 유형 B에 연결됩니다.enum을 사용하여 정수 범위에 유형을 지정 하시겠습니까?
임의로 배열을 임의로 섞습니다. 이제 인덱스 중 4 개를 추출하려고하지만 최대 4 개 중 하나만 A 유형이 될 수 있습니다.
인덱스 1 - 4를 "유형 A"로 정의하려면 여기에서 enum 함수를 사용해야한다고 생각합니다. & 인덱스 "유형 B"로 5-20, 그렇다면 내가 보았다면 내 새로 랜덤 화 된 indicesArray [0]의 첫 번째 요소는 어떤 유형인지 알 수 있습니다. & 이에 따라.
내가 열거 본 예에서 사용했던 방법은 간다 같은 :
enum category { typeA = 0, typeB };
그것은 &에게 b를 입력에 나머지 TYPEA하는 인덱스 1-4를 할당하거나 내가 잘못된 궤도에 오전 가능 이리? 미리 감사드립니다.
편집 내가이 & 바로 오류로
내가 오류를 받기를 컴파일하려고
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int* indices = malloc(20*sizeof(int));
for (int i=0; i<20; i++) {
indices[i] = i;
}
enum category {typeA, typeB};
enum category categoryForIndex(int index) {
if (index >= 1 && index <= 4) {
return typeA;
} else {
return typeB;
}
}
[pool drain];
return 0;
}
를 실행 테스트하려고 코드
을 포함하는 "중첩 된 기능, 사용 불가능 - fnested-functions to re-enable "은 우연히 두 번째 메인이 믹스에 던져 지거나, 그런 식으로 일어날 때 주로 발생합니다. 어떤 아이디어?
편집 연습
당신은 트랙 떨어져있어#import <Foundation/Foundation.h>
enum category {typeA, typeB};
enum category categoryForIndex(int index) {
if (index >= 1 && index <= 4) {
return typeA;
} else {
return typeB;
}
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int* indices = malloc(20*sizeof(int));
for (int i=1; i<=20; i++) {
indices[i] = i;
}
NSLog(@"index[0] is %i:", indices[16]);
enum category index;
index = indices[16];
switch (categoryForIndex(index)) { //this tests to see what category 16 belongs to
case typeA:
NSLog(@"index is of type A");
break;
case typeB:
NSLog(@"index is of type B");
break;
default:
NSLog(@"index not valid");
break;
}
[pool drain];
return 0;
}
이 포함 된'N-1 '에'0 '에서 이동 C에서 인덱스를 조심. – pmg
감사합니다 pmg :) 예 참조 할 배열은 인덱스 (1에서 시작하는)를 보유하고 있습니다. – Octave1