簡體   English   中英

在C ++中使用strcat函數時出現奇怪的字符

[英]Strange characters appear when using strcat function in C++

我是C ++的新手,可以從《 MSDN C ++入門指南》中學習。

嘗試使用strcat函數時,它可以工作,但是一開始我得到了三個奇怪的字符。

這是我的代碼

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main() {
    char first_name[40],last_name[40],full_name[80],space[1];
    space[0] = ' ';
    cout << "Enter your first name: ";
    gets(first_name);
    cout << "Enter your last name: ";
    gets(last_name);
    strcat(full_name,first_name);
    strcat(full_name,space);
    strcat(full_name,last_name);
    cout << "Your name is: " << full_name;
    return 0;
}

這是輸出

Enter your first name: Taher
Enter your last name: Abouzeid
Your name is: Y}@Taher Abouzeid

我不知道為什么Y} @出現在我的名字之前?

您不會通過將第一個字符設置為'\\ 0'來初始化full_name,因此其中包含垃圾字符,當您執行strcat時,您將在垃圾字符之后添加新數據。

您正在創建的數組充滿了隨機數據。 C ++將為數據分配空間,但不會使用已知數據初始化數組。 strcat會將數據附加到字符串的末尾(第一個'\\ 0'),因為字符數組尚未初始化(並且充滿了隨機數據),這將不是第一個字符。

可以通過更換來糾正

char first_name[40],last_name[40],full_name[80],space[1];

char first_name[40] = {0};
char last_name[40] = {0};
char full_name[80] = {0};
char space[2] = {0};

= {0}將把第一個元素設置為'\\ 0',它是字符串終止符,並且c ++將自動用'\\ 0'填充所有未指定的元素(前提是至少指定了一個元素)。

變量full_name在附加到變量之前未初始化。

更改此:

strcat(full_name,first_name);

對此:

strcpy(full_name,first_name);

您在測試中看不到任何問題,但是在使用' '初始化其唯一字符后,您的space字符串也不以空值結尾。

正如其他人所說,您必須初始化數據,但是您是否考慮過學習標准c ++庫? 有時它更直觀,甚至可能更有效。

它將是:

string full_name=first_name+" "+last_name;

而且您不必為終止空字符而煩惱。 有關參考,請訪問cplusplus

哦,還有一個完整的工作示例,使您可以更好地理解(來自operator + =):

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string name ("John");
  string family ("Smith");
  name += " K. ";         // c-string
  name += family;         // string
  name += '\n';           // character

  cout << name;
  return 0;
}

問題出在您的space文字上。

strcat函數需要C樣式的字符串,該字符串為零個或多個字符,后跟一個以null結尾的字符。 因此,當為C樣式的字符串分配數組時,您需要為終止的空字符分配一個額外的字符。

因此,您的space array needs to be of length 2, one for the space character and one for the null character.

Since space is constant, you can use a string literal instead of an array: const char space[] = " ";

Since space is constant, you can use a string literal instead of an array: const char space[] = " ";

Also, since you are a newbie, here are some tips:
1. Declare one variable per line.
This will be easier to modify and change variable types.

2. Either flush std::cout , use std::endl , or include a '\\n'.
This will flush the buffers and display any remaining text.

3. Read the C++ language FAQ.
Click here for the C++ language Frequently Asked Questions (FAQ)

4. You can avoid C-style string problems by using std::string


5.投資Scott Myers 有效的C ++更有效的C ++書籍。

字符串在C和C ++中以null終止(strcat函數是C的遺產)。 這意味着當您指向一個隨機的內存地址(新的char []變量指向具有未初始化的隨機內容的堆棧地址)時,編譯器將把直到第一個\\ 0(空)字符的所有內容都解釋為字符串。 (如果使用指針算術,它將超出分配的大小)。

這可能會導致非常模糊的錯誤,安全性問題(緩沖區溢出漏洞)以及非常難以理解和維護的代碼。 現代編譯器具有可幫助檢測此類問題的功能。

這是您的選擇的一個很好的總結

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM