我有一个简单的软件用于在控制台中打印电路板。我使用addBoard()(添加组件的名称不当的方法)运行下面的程序,它只运行打印。即使它是序列中的最后一个。如果我将其注释掉,程序运行正常,并在Visual Studio Code终端窗口中打印出所有内容。
这可能是什么原因造成的? (还有关于我使用指针的任何指示......或其他任何不是最佳实践的内容都非常感激。)谢谢!
#include <stdio.h>
#include <stdlib.h>
char *posArr[10][30];
void printInstructions(){
printf("Hello and welcome! Here you can configure your own breadboard\n");
printf("To do this you will use coordinates using this syntax:\n");
printf("width: 3\n");
printf("height: 5\n");
printf("After that a component will be choosen.\n");
printf("It would look like below:\n");
}
void printBoard(){
for (int i = 1; i <= 10; i++) //Height
{
printf("\n");
for (int j = 1; j <= 30; j++) //Width
{
printf("%s", posArr[i][j]);
}
}
}
void clearBoard(){
for (int i = 1; i <= 10; i++) //Height
{
for (int j = 1; j <= 30; j++) //Width
{
posArr[i][j] = ". ";
}
}
}
void buildBoard(int *width, int *height){
//*height -= 1;
//*width -= 1;
for (int i = 1; i <= 10; i++) //Height
{
//printf("\n");
for (int j = 1; j <= 30; j++) //Width
{
if (*height == i && *width == j){
posArr[i][j] = "¤ ";
//printf("¤ ");
}
else {
posArr[i][j] = ". ";
//printf(". ");
}
}
}
}
void addBoard(){
int h, w;
while(1)
{
printf("Width: ");
scanf("%d", w);
printf("Height: ");
scanf("%d", h);
buildBoard(&w,&h);
//Add length later
printBoard();
}
}
int main() {
printInstructions();
int a=3, b=5;
buildBoard(&a,&b);
printBoard();
clearBoard();
//addBoard();
}
答案 0 :(得分:0)
您可以尝试使用此代码(稍微修改代码)
#include <stdio.h>
#include <stdlib.h>
char *posArr[10][30];
void printInstructions(){
printf("Hello and welcome! Here you can configure your own breadboard\n");
printf("To do this you will use coordinates using this syntax:\n");
printf("width: 3\n");
printf("height: 5\n");
printf("After that a component will be choosen.\n");
printf("It would look like below:\n");
}
void printBoard(){
for (int i = 0; i < 10; i++)
{
printf("\n");
for (int j = 0; j < 30; j++)
{
printf("%s", posArr[i][j]);
}
}
}
void clearBoard(){
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 30; j++)
{
posArr[i][j] = ". ";
}
}
}
void buildBoard(int *width, int *height){
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 30; j++)
{
if (*height == i && *width == j){
posArr[i][j] = "¤ ";
}
else {
posArr[i][j] = ". ";
}
}
}
}
int main() {
printInstructions();
int a=3, b=5;
buildBoard(&a,&b);
printBoard();
clearBoard();
}