Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Modify value of LPWSTR

i am currently developing program with winhttp and getting stuck while modify value of LPWSTR in URL_COMPONENT struct.

I am make simple script that describe my problem

#include <windows.h>
#include <stdio.h>
 
int main(int argc, char const *argv[])
{
  LPWSTR country = L"America, England, Belgium";
  size_t pos = 0;
  while(country[pos] != 44) pos++;
  country[pos] = 0;
  printf("%ls\n", country); // 0x4015b0
  return 0;
}

The program receive signal SIGSEGV in line country[pos] = 0
Thanks in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

LPWSTR country = L"America, England, Belgium";

country is a pointer to the string literal. Attempt to modify string literal invokes Undefined Behaviour which in your case expresses itself as SEGFAULT.

You need to define modifiable wchar array:

wchar_t country[] = L"America, England, Belgium";

You can also use compound literal to initialize pointer:

LPWSTR country = (wchar_t[]){L"America, England, Belgium"};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading