473,770 Members | 4,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const char * to char[]

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
11 2695
In article <bp*********@ne ws.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(somestr uct.thefield, WCS->AppName, 64);

--
Andreas Kähäri
Nov 13 '05 #2
"Allan Bruce" <al*****@TAKEAW AYf2s.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

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f******** ********@news.n l.net...
"Allan Bruce" <al*****@TAKEAW AYf2s.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 uCallbackMessag e;
HICON hIcon;
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDAT A;

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(TrayIco n.szTip, mWCS->AppName, 64); // tooltip text to display

Where am I going wrong?
Thanks
Allan
Nov 13 '05 #4
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
"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote:
The struct is declared as:
typedef struct _NOTIFYICONDATA { char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDAT A;

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(TrayIco n.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

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f******** ********@news.n l.net...
"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote:
The struct is declared as:
typedef struct _NOTIFYICONDATA {
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDAT A;

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(TrayIco n.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
"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message news:<bp******* **@news.freedom 2surf.net>...
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f******** ********@news.n l.net... <snip> The struct is declared as:
typedef struct _NOTIFYICONDATA {
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessag e;
HICON hIcon;
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDAT A;

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(TrayIco n.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(TrayIco n.szTip, mWCS->AppName, 22);

seems to me that'd be the safer way to go.
Nov 13 '05 #8

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f******** ********@news.n l.net...
"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote:
The struct is declared as:
typedef struct _NOTIFYICONDATA {
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDAT A;

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(TrayIco n.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
gr******@the-junkyard.net (Charles Banas) wrote in message news:<a7******* *************** ***@posting.goo gle.com>...
"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message news:<bp******* **@news.freedom 2surf.net>...
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f******** ********@news.n l.net... <snip>
The struct is declared as:
typedef struct _NOTIFYICONDATA {
<snip>
char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDAT A;

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(TrayIco n.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(TrayIco n.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2238
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. Someone on the SuSE programming mailing list suggested my problem is that I'm trying to execute a function (I assume he meant the constructor) at compile time. The same source code compile if I don't try to split it up into separate libraries. ...
5
2165
by: TechCrazy | last post by:
What do each of these mean? Thanks. I am incredibly confused. char foo (const char * &p ); char foo (const char &* p ); char foo (const &char * p ); char foo (const char * const &p ); char foo (const char * &const p ); char foo (const char &* const p ); char foo (const &char * const p );
7
4363
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
8
2596
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g. char * -> const char *. However, this does not appear to work when I add another level of indirection: void test1 (char **value) {}
6
10047
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he hoped I could solve, and I couldn't. Some code he's using, written in 1999, that compiled fine in 1999, no longer does in 2006 with g++ 4. This little bit of code: SimS::SimS (ostream &s) {
10
5323
by: dwaach | last post by:
Hi, I am trying to compile the following program, #include <iostream> using namespace std; typedef char* CHAR; typedef const CHAR CCHAR;
42
32186
by: S S | last post by:
Hi Everyone I have const char *p = "Hello"; So, here memory is not allocated by C++ compiler for p and hence I cannot access p to modify the contents to "Kello" p = 'K'; // error at runtime
10
2792
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1875
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle, and you'll be left with just noisy compiler warnings and confusion. if you start a project with all char *, and char ** and even char ***, if you begin at the low level weeding out references of 'passing const char * to char * ( such as...
9
10528
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int strspcmp(const char * s1, const char * s2) {
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.