const char * to char[]

A

Allan Bruce

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
 
A

Andreas Kahari

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?


Using strncpy():


#include <string.h>

/* ... later ... */

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

Richard Bos

Allan Bruce said:
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;

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
 
A

Allan Bruce

Richard Bos said:
Allan Bruce said:
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;

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

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
 
A

Allan Bruce

I forgot to mention - the error I get is an access violation and my program
just crashes - no compiler error or warning.
Thanks
Allan
 
R

Richard Bos

Allan Bruce said:
The struct is declared as:
typedef struct _NOTIFYICONDATA {
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDATA;

and my own struct is declared as:
typedef struct
{
const char *AppName;
} 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

How have you declared WCS and mWCS? Do these actually point to the same
object? I can't find any problems with the code you've posted.

If you step through your code in a debugger, at what line does it
actually crash?

Richard
 
A

Allan Bruce

Richard Bos said:
Allan Bruce said:
The struct is declared as:
typedef struct _NOTIFYICONDATA {
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDATA;

and my own struct is declared as:
typedef struct
{
const char *AppName;
} 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

How have you declared WCS and mWCS? Do these actually point to the same
object? I can't find any problems with the code you've posted.

If you step through your code in a debugger, at what line does it
actually crash?

Richard

WCS and mWCS point to the same struct in memory, the code crashes on the
strncpy line. I thought my code was ok, could this be a microsoft issue?
Thanks
Allan
 
C

Charles Banas

Allan Bruce said:
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

call me naiive, but isn't it possible that the memory beyond the
string AppName points to could be code memory?

have you tried just strcpy() instead of strncpy()?

or have you tried this instead? :

strncpy(TrayIcon.szTip, mWCS->AppName, 22);

seems to me that'd be the safer way to go.
 
A

Allan Bruce

Richard Bos said:
Allan Bruce said:
The struct is declared as:
typedef struct _NOTIFYICONDATA {
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDATA;

and my own struct is declared as:
typedef struct
{
const char *AppName;
} 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

How have you declared WCS and mWCS? Do these actually point to the same
object? I can't find any problems with the code you've posted.

If you step through your code in a debugger, at what line does it
actually crash?

Richard

I found the problem - it turns out my mWCS was becoming invalid through a
memory trash elsewhere.
Thanks for the help
Allan
 
J

J. J. Farrell

Allan Bruce said:
The struct is declared as:
typedef struct _NOTIFYICONDATA {
<snip>
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDATA;

and my own struct is declared as:
typedef struct
{
<snip>
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?

call me naiive, but isn't it possible that the memory beyond the
string AppName points to could be code memory?
Yes.

have you tried just strcpy() instead of strncpy()?

or have you tried this instead? :

strncpy(TrayIcon.szTip, mWCS->AppName, 22);

seems to me that'd be the safer way to go.

Why? If he can't rely on strncpy() from a major implementation
working properly, he might as well give up.
 
J

J. J. Farrell

Allan Bruce said:
Richard Bos said:
Allan Bruce said:
The struct is declared as:
typedef struct _NOTIFYICONDATA {
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDATA;

and my own struct is declared as:
typedef struct
{
const char *AppName;
} 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);

How have you declared WCS and mWCS? Do these actually point to the same
object? I can't find any problems with the code you've posted.

If you step through your code in a debugger, at what line does it
actually crash?

WCS and mWCS point to the same struct in memory, the code crashes on the
strncpy line. I thought my code was ok, could this be a microsoft issue?

It could be, but it's more likely to be some error in your code
that we don't yet have enough information to see. How are you
sure that mWCS points to the right place at the time of the error?
Its being wrong is the most likely cause of the problem.

Can you show a complete code sequence that shows how WCS and mWCS
get initialized, and everything that happens to them before the
call to strncpy()? A complete short program that shows the problem
would be best.
 
R

Richard Bos

Be careful. You happen to know that your string fits now, but consider
what would happen if AppName were exactly 64 characters long, not
including the terminating null character.
call me naiive, but isn't it possible that the memory beyond the
string AppName points to could be code memory?

What has that got to do with it?
strncpy(TrayIcon.szTip, mWCS->AppName, 22);

You are confused about the behaviour of strncpy(), I suspect. It does
_not_ copy exactly N bytes - if it did, you might as well use memcpy().
What it does is copy up to either the end of the string, or N bytes,
whichever comes first. And then, superfluously, pads the rest with null
characters.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top