const char * to char [] [英] const char * to char[]

查看:73
本文介绍了const char * to char []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个需要char [64]的stuct(我不能改变它,因为它是

win32 api的一部分),但我有一个const char *指向一个字符串文字

声明为:


WCS-> AppName =" Some Text";


Appname是一个const char *。如何将此转换为结构所需的char [64]



谢谢

Allan

Hi there,

I have a stuct which requires a char[64] (I cant change it as it is part of
the win32 api), but I have a const char * pointing to a string literal
declared as:

WCS->AppName = "Some Text";

Appname is a const char *. How do I convert from this to the char[64]
required for the struct?
Thanks
Allan

推荐答案

在文章< bp ********* @ news.freedom2surf.net>中,Allan Bruce写道:
In article <bp*********@news.freedom2surf.net>, Allan Bruce wrote:
你好,

我有一个需要char [64]的stuct(我不能改变它,因为它是win32 api的一部分),但我有一个const char *指向一个字符串文字
声明为:

WCS-> AppName =" Some Text" ;;

Appname是一个const char *。如何将此转换为结构所需的char [64]
Hi there,

I have a stuct which requires a char[64] (I cant change it as it is part of
the win32 api), but I have a const char * pointing to a string literal
declared as:

WCS->AppName = "Some Text";

Appname is a const char *. How do I convert from this to the char[64]
required for the struct?



使用strncpy():

#include< string.h>


/ * ...稍后...... * /


strncpy(somestruct.thefield,WCS-> AppName ,64);


-

Andreas K?h?ri


Using strncpy():
#include <string.h>

/* ... later ... */

strncpy(somestruct.thefield, WCS->AppName, 64);

--
Andreas K?h?ri


" Allan布鲁斯" <人***** @ TAKEAWAYf2s.com>写道:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
我有一个需要char [64]的stuct(我不能改变它,因为它是win32 api的一部分),但我有一个const char *指向字符串文字
声明为:

WCS-> AppName =" Some Text" ;;

Appname是一个const char *。我如何从这个转换为结构所需的char [64]
I have a stuct which requires a char[64] (I cant change it as it is part of
the win32 api), but I have a const char * pointing to a string literal
declared as:

WCS->AppName = "Some Text";

Appname is a const char *. How do I convert from this to the char[64]
required for the struct?




我假设结构实际上包含char数组,而不是

指针?也就是说,它是


struct foo {

char bar [64];

/ *其他成员.. 。* /

} qux;


而不是


struct foo {

char * bar; / *必须指向64字节的内存* /

/ *其他会员...... * /

} qux;


如果是这样,你可以简单地strcpy(qux.bar,Appname)。


如果没有,发布结构的定义,声明

当前结构对象,以及使用结构的代码。


Richard



I assume the struct actually contains the char array, rather than a
pointer? That is, it''s

struct foo {
char bar[64];
/* other members... */
} qux;

rather than

struct foo {
char *bar; /* Must point to 64 bytes of memory */
/* other members... */
} qux;

If so, you can simply strcpy(qux.bar, Appname).

If not, post the definition of the struct, the declaration of your
current struct object, and the code in which you use the struct.

Richard




理查德博斯 < rl*@hoekstra-uitgeverij.nl>在消息中写道

news:3f **************** @ news.nl.net ...

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f****************@news.nl.net...
" Allan布鲁斯" <人***** @ TAKEAWAYf2s.com>写道:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
我有一个需要char [64]的stuct(我不能改变它,因为它是win32 api的
部分),但我有一个const char *指向字符串文字
声明为:

WCS-> AppName =" Some Text" ;;

Appname是一个const char *。我如何从这个转换为结构所需的char [64]
我假设结构实际上包含char数组,而不是
指针?也就是说,它是

struct foo {
char bar [64];
/ *其他成员...... * /
} qux;
I have a stuct which requires a char[64] (I cant change it as it is part of the win32 api), but I have a const char * pointing to a string literal
declared as:

WCS->AppName = "Some Text";

Appname is a const char *. How do I convert from this to the char[64]
required for the struct?
I assume the struct actually contains the char array, rather than a
pointer? That is, it''s

struct foo {
char bar[64];
/* other members... */
} qux;




就是这样,但它不起作用 - 我试过strncpy() - 我的代码在下面

如果是这样,你可以简单地strcpy( qux.bar,Appname)。

如果没有,请发布struct的定义,
当前struct对象的声明以及使用struct的代码。

Richard



This is the case, but it doesnt work - I tried strncpy() - my code is below

If so, you can simply strcpy(qux.bar, Appname).

If not, post the definition of the struct, the declaration of your
current struct object, and the code in which you use the struct.

Richard




结构声明为:

typedef struct _NOTIFYICONDATA {

DWORD cbSize ;

HWND hWnd;

UINT uID;

UINT uFlags;

UINT uCallbackMessage;

HICON hIcon;

char szTip [64];

} NOTIFYICONDATA,* PNOTIFYICONDATA;


和我自己的结构声明为:

typedef struct

{

UINT Xres;

UINT Yres;

const char * AppName;

WNDPROC WndProc;

} WinCore结构;


我用以下内容初始化我的结构:

WCS-> AppName =" WinCore Usage Program";

/ * ... * /


我尝试将我的AppName复制到NOTIFYICONDATA.szTip:

NOTIFYICONDATA TrayIcon; //设置结构

/ * ... * /

strncpy(TrayIcon.szTip,mWCS-> AppName,64); //工具提示文字显示


我哪里错了?

谢谢

Allan



The struct is declared as:
typedef struct _NOTIFYICONDATA {
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessage;
HICON hIcon;
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDATA;

and my own struct is declared as:
typedef struct
{
UINT Xres;
UINT Yres;
const char *AppName;
WNDPROC WndProc;
} WinCoreStruct;

I initialise my struct with:
WCS->AppName = "WinCore Usage Program";
/* ... */

and I attempt to copy my AppName to NOTIFYICONDATA.szTip by:
NOTIFYICONDATA TrayIcon; // set structure
/* ... */
strncpy(TrayIcon.szTip, mWCS->AppName, 64); // tooltip text to display

Where am I going wrong?
Thanks
Allan


这篇关于const char * to char []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆