두 문자열을 바꿔 치기를 원한다고 생각합니다. 그러나 이것이 효과가 없을 것입니다. 이유를 설명하려고합니다.
int main(){
char c[] = "hey";
char d[] = "hellow";
cout << c << "," << d << endl;
f(c, d);
cout << c << "," << d << endl;
save();
}
void f(char* p, char* q) {
// When you pass the two strings to this function the arrays turn into
// pointers to the string.
int x = *(char*)p; // You now say you want x to be equal to the first
// letter of what p points to, which is "hey".
// So now x equals 'h', although its value is as
// an int.
int y = *(char*)q; // Here you assign the first letter of "hellow"
// to y. y now equals 'h' also
int temp= x; // temp now equals 'h'
y = x; // y now equals 'h' also
y = temp; // y now equals 'h' also
// Basically this whole thing does nothing.
}
이 기능을 사용하면 포인터를 바꿀 수 있지만 모든 것을 설명하기에는 너무 많습니다.
#include <iostream>
using namespace std;
void f(char** p, char** q);
int main() {
char* c = "hey";
char* d = "hellow";
cout << "befor/" << c << "," << d << endl;
f(&c, &d);
cout << "after/" << c << "," << d << endl;
}
void f(char** p, char** q) {
char* temp = *p;
*p = *q;
*q = temp;
}
이유는 함수 인자가 단지 포인터 단지 기능에게 사본을 제공하기 때문에, 포인터, 또는 포인터를 참조하려면 다음 중 하나를 포인터로주의해야하며, 사본은 원본 포인터를 변경 충분하지 않습니다 . 죄송합니다. 아무도 이해가되지 않는다면 코드에 많은 문제가 있으며 모든 것을 실제로 설명 할 수는 없습니다.
설명해 주실 수 있습니다. 어떤 문제가 발생했는지, 무엇을 시도하고 있는지 명확하지 않습니다. 분명한 것은 당신이 불행하다는 것입니다.하지만 슬프게도, 불충분합니다. –
오류가 없다 http://coliru.stacked-crooked.com/view?id=88a670352c63d925 –
두 문자열의 값을 바꾸려는 경우'char * temp = p; p = q; 함수'f'에서 q = temp; 하지만 좀 더 자세하고 명확하게 문제를 설명하면 좋을 것입니다. :) – tkhurana96