linux下使用strcpy_s将const char*转为char*时, strcpy_s’在此作用域中尚未声明

本文探讨了在Linux环境下,由于未声明'strcpy_s'引发的问题,并提出使用'memcpy'作为其替代方案的方法。通过具体的代码示例,展示了如何安全地将C++字符串转换为字符数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

char *ch = new char[10];
string str = "Hello";
strcpy_s(ch, str.length()+1, str.c_str());

在linux 下strcpy_s’在此作用域中尚未声明,可以用memcpy替换

memcpy(ch, str.c_str(), str.length() + 1);

struct node { char path[1024]; struct node *next; struct node *prev; }; static const char * getenv_default(const char * name, const char * dflt) { return getenv(name) ?: dflt; } #if LV_USE_LINUX_FBDEV static void lv_linux_disp_init(void) { const char * device = getenv_default("LV_LINUX_FBDEV_DEVICE", "/dev/fb0"); lv_display_t * disp = lv_linux_fbdev_create(); lv_linux_fbdev_set_file(disp, device); } #elif LV_USE_LINUX_DRM static void lv_linux_disp_init(void) { const char * device = getenv_default("LV_LINUX_DRM_CARD", "/dev/dri/card0"); lv_display_t * disp = lv_linux_drm_create(); lv_linux_drm_set_file(disp, device, -1); } #elif LV_USE_SDL static void lv_linux_disp_init(void) { const int width = atoi(getenv("LV_SDL_VIDEO_WIDTH") ?: "800"); const int height = atoi(getenv("LV_SDL_VIDEO_HEIGHT") ?: "480"); lv_sdl_window_create(width, height); } #else #error Unsupported configuration #endif lv_obj_t *btn; lv_obj_t *btn1; lv_obj_t *btn2; lv_obj_t *btn3; struct node *pos = NULL; struct node * up_showpic(){ lv_obj_t *img1 = lv_image_create(lv_screen_active()); pos = pos -> prev; char path[1024]; char ch = 'A'; sprintf(path,"%c:%s",ch,pos->path); lv_image_set_src(img1,path); return pos; } struct node * down_showpic(){ lv_obj_t *img1 = lv_image_create(lv_screen_active()); pos = pos -> next; char path[1024]; char ch = 'A'; sprintf(path,"%c:%s",ch,pos->path); lv_image_set_src(img1,path); return pos; } static void button_cb(lv_event_t *e){ void *obj = lv_event_get_target(e); printf("按钮被点击%p\n",obj); struct node*pos = NULL; if(btn == obj){ printf("往上切换图片\n"); pos = up_showpic(); } else if(btn1 == obj){ printf("往下切换图片\n"); pos = down_showpic(); } else if(btn2 == obj){ lv_obj_t *img1 = lv_image_create(lv_screen_active()); char path[1024]; char ch = 'A'; sprintf(path,"%c:%s",ch,pos->path); printf("放大图片\n"); lv_image_set_scale(path,256); } else if(btn3 == obj){ printf("缩小图片\n"); } }放大图片为什么会段错误
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值