I've been looking for the thread re swapping values of two string pointers but I couldn't findit and I don't know if it has already been solved. Assuming it's still on the lines waiting, I think I've got the simple code to solve it:

#include <windows.h>

void swap(LPCTSTR*,LPCTSTR*);

int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{
	char szBuf[1024];
	LPCTSTR ptr1 = "Pointer 1";
	LPCTSTR ptr2 = "Other pointer";
	wsprintf(szBuf, "This is the first order before calling swap():\n\nptr1: %s\nptr2: %s",
		ptr1, ptr2);
	MessageBox(0, szBuf, "swap demo", MB_ICONINFORMATION);
	swap(&ptr1, &ptr2);
	wsprintf(szBuf, "and after calling swap():\n\nptr1: %s\nptr2: %s", ptr1, ptr2);
	MessageBox(0, szBuf, "swap demo", MB_ICONINFORMATION);
	return 0;
}

void swap(LPCTSTR *ppstr1, LPCTSTR *ppstr2)
{
	LPCTSTR ptemp = *ppstr1;
	*ppstr1 = *ppstr2;
	*ppstr2 = ptemp;
}

If there's an abvious reason why I came too late, think it's maybe because I don't have my own internet connection.

Recommended Answers

All 2 Replies

Kurt Kubing,
>I don't know if it has already been solved.
It doesn't matter. Feel free to ask. The code in above post is an example of "swapping pointers".

I appriciate adatapost for this kind of encouragements.

While swaping try call by value and also try call by reference.

commented: and you too. +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.