详细阐述Joachim Pileborg的回答,pal
是一个指向角色的指针。 *pal
是一个char
,它是一个整数值,表示pal指向的字符。 char,是整数的最小可寻址类型。
如果您打算使用strtol,将coords
更改为long
是最明智的选择,以便类型匹配。好处在于,strtok在这种解析方面非常糟糕,你可以完全抛弃它。
typedef struct {
long x;
long y;
} Coord;
/* returns the number of coordinates read. */
size_t read_coordinates(char *str, Coord destination[], size_t capacity);
int main(void) {
#define EXERCISE_CAPACITY 1000
Coord exercise[EXERCISE_CAPACITY];
size_t count = read_coordinates("1 2 3 4 5 6", exercise, EXERCISE_CAPACITY);
}
size_t read_coordinates(char *str, Coord destination[], size_t capacity) {
size_t x;
for (x = 0; x < capacity; x++) {
char *endptr = NULL;
destination[x].x=strtol(str, &endptr, 10);
if (endptr - str == 0) {
// Stop when strtol says it can't process any more...
break;
}
str = endptr + 1;
destination[x].y=strtol(str, &endptr, 10);
if (endptr - str == 0) { break; }
str = endptr + 1;
}
return x;
}
如果必须使用int
为您的坐标中的类型,这将是明智的使用sscanf的,有点像这样:
typedef struct {
int x;
int y;
} Coord;
/* returns the number of coordinates read. */
size_t read_coordinates(char *str, Coord destination[], size_t capacity);
int main(void) {
#define EXERCISE_CAPACITY 1000
Coord exercise[EXERCISE_CAPACITY];
size_t count = read_coordinates("1 2 3 4 5 6", exercise, EXERCISE_CAPACITY);
}
size_t read_coordinates(char *str, Coord destination[], size_t capacity) {
size_t x;
for (x = 0; x < capacity; x++) {
int n;
if (sscanf(str, "%d %d%n", &destination[x].x, &destination[x].y, &n) != 2) {
// Stop when sscanf says it can't process any more...
break;
}
str += n;
}
return x;
}
http://www.asciitable.com/ – 2013-03-23 00:56:27
我米数比骡子...谢谢先生。 – PablodeAcero 2013-03-23 00:58:33
@Armin http://en.wikipedia.org/wiki/EBCDIC – Sebivor 2013-03-23 01:25:04