【发布时间】:2017-11-02 00:54:22
【问题描述】:
我正在对我的一个程序进行一些测试,并且想知道为什么程序在进入我的函数时会崩溃。不要介意程序的逻辑,因为我仍处于确保我了解如何使用我手头的工具的阶段。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Constants */
#define HEX_CAPITAL_LETTERS_BEGIN 65
#define HEX_CAPITAL_LETTERS_END 90
#define HEX_NUMBERS_BEGIN 48
#define HEX_NUMBERS_END 57
#define EXIT_SUCCES 0
/* Prototypes */
void codeToField(char *charArray, int i, int hexFloor, int hexCeil, char *outputArray);
/* Main Function */
int main(void) {
char *code, warehouse, product, qualifiers;
int i = 0;
printf("Enter a MMOC product code: ");
scanf("%s", &code);
codeToField(code, i, HEX_CAPITAL_LETTERS_BEGIN, HEX_CAPITAL_LETTERS_END, &warehouse);
codeToField(code, i , HEX_NUMBERS_BEGIN, HEX_NUMBERS_END, &product);
strcpy(&qualifiers, code + i);
printf("\n\nWarehouse: %s\nProduct: %s\nQualifiers: %s\n", &warehouse, &product, &qualifiers);
return EXIT_SUCCES;
}
void codeToField(char *charArray, int i, int hexFloor, int hexCeil, char *outputArray) {
int j = 0;
while (charArray[i] >= hexFloor && charArray[i] <= hexCeil) {
outputArray[j] = charArray[i];
i++;
j++;
}
}
提前致谢。
【问题讨论】:
-
为什么使用
65和90之类的数字而不是'A'、'Z'、'0'等? -
你为什么要把
strcpy变成一个字符? -
@M.M 正如我所说,除了手头的问题,别介意。仍在试图找出我想要的东西,并且真的只是测试了一些东西。发帖前把问题都注释掉了哈哈。
标签: c arrays pointers syntax char