2015-12-03 124 views
6

如何添加'。'到C中的char數組:=「Hello World」,所以我得到一個char數組:「Hello World」。問題看起來很簡單,但我很掙扎。如何將char/int添加到C中的char數組?

嘗試了以下內容:

char str[1024]; 
char tmp = '.'; 

strcat(str, tmp); 

但它不工作。它顯示了我的錯誤:「傳遞'strcat'的參數2使得整形指針沒有強制轉換」 我知道在C中char可以被轉換爲int。我是否必須將tmp轉換爲char數組或者是否有更好的解決方案?

+0

小號tr包含沒有字符串,只是大小聲明嘿嘿 –

+0

可能重複[我怎麼strcat一個字符數組字符在c + +](http://stackoverflow.com/questions/27522984/how-can-i-strcat-one- character-to-array-character-in-c) – MikeCAT

+0

這只是用Hello World來描述問題的一個例子。它在我的真實程序中必須是空的。程序將在稍後填寫。問題只是添加一個char/int到char數組 – missjohanna

回答

11

strcat具有聲明:

char *strcat(char *dest, const char *src) 

該公司預計2串。 While this compiles:

char str[1024] = "Hello World"; 
char tmp = '.'; 

strcat(str, tmp); 

這會導致因爲strcat正在尋找一個空記性不好的問題終止的CString。你可以這樣做:

char str[1024] = "Hello World"; 
char tmp[2] = "."; 

strcat(str, tmp); 

Live example.

如果你真的想追加一個字符,你需要讓自己的函數。這樣的事情:

void append(char* s, char c) { 
     int len = strlen(s); 
     s[len] = c; 
     s[len+1] = '\0'; 
} 

append(str, tmp) 

當然,你也可能想檢查你的字符串大小等,以使其內存安全。

+1

我刪除了我的答案,並支持你的答案,你更好地解釋了它 –

+0

這是要走的路。你的追加功能將完成這項工作。 – bruceg

+0

我必須用char類型的變量定義char tmp [2]。 (char temp = x; char tmp [2] = temp;)但它告訴我我有一個無效的初始化程序。哪裏有問題? – missjohanna

0

我想你已經忘記了初始化你的字符串「str」:你需要在使用strcat之前初始化字符串。而且你也需要tmp是一個字符串,而不是一個字符。嘗試改變這一點:

char str[1024]; // Only declares size 
char tmp = '.'; 

char str[1024] = "Hello World"; //Now you have "Hello World" in str 
char tmp[2] = "."; 
1

該錯誤是由於要傳遞一個錯誤的strcat()的事實。看看strcat()的原型:

char *strcat(char *dest, const char *src); 

但是你通過char作爲第二個參數,這顯然是錯誤的。

改爲使用snprintf()

char str[1024] = "Hello World"; 
char tmp = '.'; 
size_t len = strlen(str); 

snprintf(str + len, sizeof str - len, "%c", tmp); 

作爲評論由OP:

That was just a example with Hello World to describe the Problem. It must be empty as first in my real program. Program will fill it later. The problem just contains to add a char/int to an char Array

在這種情況下,snprintf()可以很容易地處理它爲 「追加」 的整數類型爲char緩衝器太。 snprintf()的優點在於,將各種類型的數據連接到char緩衝區更加靈活。

例如來連接一個字符串,字符和一個int:

char str[1024]; 
ch tmp = '.'; 
int i = 5; 

// Fill str here 

snprintf(str + len, sizeof str - len, "%c%d", str, tmp, i); 
1

在C/C++的字符串是一個空字節('\0')終止字符的陣列;

  1. 您的字符串str尚未初始化。
  2. 您必須連接字符串,並試圖將單個字符(不含空字節,因此不是字符串)連接到字符串。

的代碼應該是這樣的:

char str[1024] = "Hello World"; //this will add all characters and a NULL byte to the array 
char tmp[2] = "."; //this is a string with the dot 
strcat(str, tmp); //here you concatenate the two strings 

請注意,您只能在其聲明中指定一個字符串到一個數組。
例如下面的代碼是不允許的:

char str[1024]; 
str = "Hello World"; //FORBIDDEN 

,並應與

char str[1024]; 
strcpy(str, "Hello World"); //here you copy "Hello World" inside the src array 
0

本稿更換此替換:與此

char str[1024]; 
char tmp = '.'; 

strcat(str, tmp); 

char str[1024] = {'\0'}; // set array to initial all NUL bytes 
char tmp[] = "."; // create a string for the call to strcat() 

strcat(str, tmp); // 
+0

這是我作爲例子給出的問題的解決方案。但我說一個是char數組,另一個是char。如果它只是2個char數組,它根本就沒有問題。在我的主程序中,我從fgetc中得到一個char或int。它可能工作,如果我寫:char tmp [2]; tmp [1] = fgetc();但不確定! – missjohanna

+0

考慮到您對問題的修改,您可以嘗試:char newstr [strlen(str)+2] = {'\ 0'}; strcpy(newstr,str); newstr [strlen(newstr)] = tmp; – user3629249