如何加入两个指针以将其串在一起并将其存储在数组或字符中

How to concatenate two pointers to string and store it in an array or characters?

本文关键字:存储 在一起 数组 字符 何加入 两个 指针      更新时间:2023-10-16

我所知道的是 xFFxFF处的ptr点。假设PTR的值是(例如0x004E0000),在xFFxFF处,如何使FOO数组包含"x41x42x43x00x00x4Ex00"

代码:

#include <string.h>
#include <iostream>
#include <Windows.h>
int main()
{
    char foo[20];
    char *alpha = "x41x42x43";
    char *test = "xFFxFF";
    void *ptr = VirtualAlloc(NULL, strlen(test), 0x3000, 0x40);
    RtlMoveMemory(lpvAddr, test, strlen(test));
}

我正在使用Visual Studio2017。

我假设您只想:

  • foo的开头复制alpha字符串
  • foo中的字符串后立即复制test指向的地址

memcpy是复制任意对象时的朋友,但是使用辅助指针会更简单。代码可能是:

char foo[20];
char *alpha = "x41x42x43";
char *test = "xFFxFF";
char **pt = &test;       // pt now points to the address where "xFFxFF" lies
memcpy(foo, alpha, strlen(alpha));
memcpy(foo + strlen(alpha), pt, sizeof(char *));  // copy the address

但是谨慎:我刚刚回答了您的问题,但是我真的看不到它的真实用例。只是假设您是为了学习目的探索地址副本。

复制foo的任何内容与VirtualAlloc有什么关系?您有20个字符的数组,只需将您想要的值复制到该内存即可。您从不声明LPVADDR,您的意思是是PTR吗?

如果您的意思是(lpvaddrto为ptr,那将是类似的(尽管以这种方式获取8字节地址的高4个字节似乎是毫无意义的):

memcpy(foo, test, 4);
*((PULONG)foo+1) = *((PULONG)&ptr+1);