#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <iostream>
using namespace std;
char a[21]; // If this is put inside the function unter -> junk output
char* b="";
void unter()
{
char *new_str = "";
strcpy(a, new_str);
char str_temp[10]="";
int chnum =0, neighbor1=3, neighbor2=5, mynode=4;
sprintf(str_temp,"%d",chnum);
b = strcat(a, str_temp);
b = strcat(b, "_from");
sprintf(str_temp,"%d",mynode);
b = strcat(b, str_temp);
b = strcat(b, "to");
sprintf(str_temp,"%d",neighbor1);
b = strcat(b, str_temp);
}
int main()
{
unter();
cout << a;
cout << b;
std::cin.get();
}
這是我在C代碼++。我不確定字符數組'a'與'b'是否有相同的值。此外,當我聲明'一個'內部函數unter(),我得到一些'垃圾'值(作爲輸出)。謹慎解釋如何?字符串連接查詢C++
只是因爲它應該說:雖然我認爲這是明顯的,你這樣做是比生產代碼等原因,只要記住這一切被包裹在'std :: string'中。 – GManNickG