我有一個簡單的軟件用於在控制檯中打印e電路板。我使用addBoard()(用於添加組件的命名方法很差)運行下面的程序,它只是不打印任何內容。即使它是序列中的最後一個。如果我將它註釋掉,程序可以正常工作,並在Visual Studio代碼終端窗口中打印所有內容。添加或刪除方法是順序最後呈現C程序無用
這可能是由於什麼原因造成的? (還有任何關於我使用指針hehe的指針......或者其他任何不是最佳實踐的指針都會非常感謝。)謝謝!
#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();
}
您沒有正確索引數組,從'1'到超出範圍。例如'for(int i = 1; i <= 10; i ++)'應該是'for(int i = 0; i <10; i ++)' –
您還正在傳遞'scanf'函數'int'而不是' INT *'。 – user694733
這是開始使用調試器的絕好機會。您可以逐步瀏覽代碼並隨時觀察變量。 – user694733