By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,417 Members | 1,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,417 IT Pros & Developers. It's quick & easy.

const char * to char[]

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
In article <bp*********@news.freedom2surf.net>, Allan Bruce wrote:
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);

--
Andreas Kähäri
Nov 13 '05 #2

P: n/a
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
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
Nov 13 '05 #3

P: n/a

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f****************@news.nl.net...
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
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
Nov 13 '05 #4

P: n/a
I forgot to mention - the error I get is an access violation and my program
just crashes - no compiler error or warning.
Thanks
Allan
Nov 13 '05 #5

P: n/a
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
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
Nov 13 '05 #6

P: n/a

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f****************@news.nl.net...
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
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
Nov 13 '05 #7

P: n/a
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<bp*********@news.freedom2surf.net>...
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f****************@news.nl.net... <snip> 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.
Nov 13 '05 #8

P: n/a

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f****************@news.nl.net...
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
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
Nov 13 '05 #9

P: n/a
gr******@the-junkyard.net (Charles Banas) wrote in message news:<a7*************************@posting.google.c om>...
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<bp*********@news.freedom2surf.net>...
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f****************@news.nl.net... <snip>
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.
Nov 13 '05 #10

P: n/a
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<bp**********@news.freedom2surf.net>...
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f****************@news.nl.net...
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
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.
Nov 13 '05 #11

P: n/a
gr******@the-junkyard.net (Charles Banas) wrote:
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message news:<bp*********@news.freedom2surf.net>...
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

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
Nov 13 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.