2017-04-06 75 views
-5
char boardCoordinates[15]; 
    void setupBoardCoordinates(char boardCoordinates[]) 
{ 
    boardCoordinates[0] = "0,0"; 
    boardCoordinates[1] = "0,1"; 
    boardCoordinates[2] = "1,1"; 
    boardCoordinates[3] = "0,2"; 
    boardCoordinates[4] = "1,2"; 
    boardCoordinates[5] = "2,2"; 
    boardCoordinates[6] = "0,3"; 
    boardCoordinates[7] = "1,3"; 
    boardCoordinates[8] = "2,3"; 
    boardCoordinates[9] = "3,3"; 
    boardCoordinates[10] = "0,4"; 
    boardCoordinates[11] = "1,4"; 
    boardCoordinates[12] = "2,4"; 
    boardCoordinates[13] = "3,4"; 
    boardCoordinates[14] = "4,4"; 
} 

int getCoordinateX(const char boardCoordinates[], int number) 
{ 
    char coordinates[] = boardCoordinates[number]; 
    char xValue = coordinates[2]; 

    return (int) xValue; 
} 

所以我試圖建立一個座標系統,其中每個boardCoordinate [數字]作爲一個「數字」的座標,作爲boardCoordinate的索引值輸入。在C中使用char數組的座標系統

我試圖做的是設置一個系統,其中每個boardCoordinate都有一個包含座標的char數組(字符串),並且可以使用返回X座標的getCoordinateX值進行提取。

由於geCoordinateX函數中存在一些指針錯誤,它似乎沒有工作。

任何想法如何去解決這個問題,所以我可以檢索X座標?感謝提前:)

+0

Don'tr垃圾郵件的標籤! C++是一種不同的語言。 – Olaf

+1

你會發現它更容易和更易於維護,可以通過結構來實現。 –

+0

最好儘量避免使用結構體,並且不要使用它們@ChrisTurner :) – Imdad

回答

1

在評論,你說:

你也有沒有的Structs的實現?我試圖避免使用Structs進行學習。並感謝您的解釋良好的帖子!

您可以使用一個int來存儲的座標。如果座標值限制爲0-9,則可以使用數字x*10 + y來表示它們。如果座標值限制爲0-99,則可以使用數字x*100 + y來表示它們。

E.g.假設x*10 + y

int boardCoordinates[15]; 

void setupBoardCoordinates(char boardCoordinates[]) 
{ 
    boardCoordinates[0] = 0; 
    boardCoordinates[1] = 1; 
    boardCoordinates[2] = 11; 
    boardCoordinates[3] = 2; 
    boardCoordinates[4] = 12; 
    boardCoordinates[5] = 22; 
    boardCoordinates[6] = 3; 
    boardCoordinates[7] = 13; 
    boardCoordinates[8] = 23; 
    boardCoordinates[9] = 33; 
    boardCoordinates[10] = 4; 
    boardCoordinates[11] = 14; 
    boardCoordinates[12] = 24; 
    boardCoordinates[13] = 34; 
    boardCoordinates[14] = 44; 
} 

int getCoordinateX(const char boardCoordinates[], int number) 
{ 
    return boardCoordinates[number]/10; 
} 

int getCoordinateY(const char boardCoordinates[], int number) 
{ 
    return boardCoordinates[number]%10; 
} 

,如果你需要使用x*100 + y你可以完善它。

4

有一些問題,你的做法,

  1. 你不需要的座標是字符串所有,只需創建一個結構

    struct coordinate { 
        int x; 
        int y; 
    }; 
    
  2. char *指針是char類型的沒有,則顯然存儲與const char *型指針字符串文字到char[]類型的陣列與char類型的元素,它可能編譯,因爲char是一個整數類型,但它是不正確的,因爲轉換不保證定義的行爲,實際上它是未定義的在c標準中指定。但是,它應該對轉換產生一些警告。

如果使用struct coordinate,你可以寫

const struct coordinate * 
get_coordinate_x(int index) 
{ 
    static const struct coordinate board_coordinates[15] = { 
     {0, 0}, {0, 1}, ..., {4, 4} 
    }; 
    return &board_coordinates[index]; 
} 

或等效的,只是remeber,你不能returna指針到一個局部變量,因爲你會導致不確定的行爲,這就是爲什麼我已經使用了static,你也可以像你的例子那樣傳遞數組。

+0

謝謝你! :)你還有沒有Structs的實現?我試圖避免使用Structs進行學習。並感謝您的解釋良好的帖子! – Imdad

+0

如果你避免使用這個結構,並使用一個字符串作爲座標,你將會遇到麻煩,並且在學習語言方面遇到的困難比你想象的要多。相反,如果您不想使用結構,請避開座標系。結構是c語言中非常重要的一部分,沒有它們,你就不會寫任何有趣的東西,而且它們也不難理解或使用。如果我現在在學習c,我會避免使用指針和字符串,但不要使用結構。 –

+0

@Imdad也注意到其他人也提出了這個解決方案,這是因爲他們是經驗豐富的c程序員,他們明白爲什麼這是最簡單和更直接的解決方案。 –

2

要走的路是struct如已經在另一個答案解釋(見https://stackoverflow.com/a/43259364/4386427)。

但你說:

最好儘量避免結構體和做沒有他們

好吧,如果你真的想保存的座標爲逗號分隔字符串,即"x,y",它可以的原因,但目前的代碼有幾個問題。

首先

char boardCoordinates[15]; 

這不是15串的陣列。這是一個單個字符串,包含零終止的15個字符的空間。要製作15個長度達3個的字符串,您需要:

char boardCoordinates[15][4]; // Notice 4 to make room for string termination 

下一個問題是分配。取而代之的

boardCoordinates[0] = "0,0"; 

你需要

strcpy(boardCoordinates[0], "0,0"); 

,或者你可以像初始化變量:

char boardCoordinates[15][4] = {"0,0", "0,1", "1,1", ...... }; 

對於getCoordinateX功能,你可以這樣做:

int getCoordinateX(const char boardCoordinates[][4], int number) 
{ 
    int xValue = boardCoordinates[number][0] - '0'; 

    return xValue; 
} 

但是通知那將只有爲0和9之間的座標工作。如果座標可以是10或更高(或負值),則需要更多代碼才能正確提取xy(並且每個元素的空間多於4串)。

所以這裏是一個小例子:

#include <stdio.h> 

int getCoordinateX(const char boardCoordinates[][4], int number) 
{ 
    int xValue = boardCoordinates[number][0] - '0'; 
    return xValue; 
} 

int getCoordinateY(const char boardCoordinates[][4], int number) 
{ 
    int yValue = boardCoordinates[number][2] - '0'; 
    return yValue; 
} 

int main(void) { 
    char boardCoordinates[2][4] = {"1,2", "3,4"}; 

    printf("x[0]=%d\n", getCoordinateX(boardCoordinates, 0)); 
    printf("y[0]=%d\n", getCoordinateY(boardCoordinates, 0)); 
    printf("x[1]=%d\n", getCoordinateX(boardCoordinates, 1)); 
    printf("y[1]=%d\n", getCoordinateY(boardCoordinates, 1)); 
    return 0; 
} 

輸出:

x[0]=1 
y[0]=2 
x[1]=3 
y[1]=4 

只是重複:它可以用文本字符串來完成,但最好的方法是struct通過解釋@ Iharob Al Asimi

1

你可以像這樣使用int數組的數組:

int coords[15][2] = {{0,0},{0,1},{1,1},{0,2},{1,2},{2,2},{0,3},{1,3},{2,3},{3,3},{0,4},{1,4},{2,4},{3,4},{4,4}}; 

訪問它像coords[3][1]這會從{0,2}

返回2如果您想打印出 「X,Y」 你總是可以做:

std::cout << coords[i][0] << ',' << coords[i][1] << std::endl;