473,785 Members | 2,423 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
11 2698
"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message news:<bp******* ***@news.freedo m2surf.net>...
"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);


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
gr******@the-junkyard.net (Charles Banas) wrote:
"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message news:<bp******* **@news.freedom 2surf.net>...
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

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(TrayIco n.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 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
4365
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
2597
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
5324
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
32187
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
2794
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1876
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
10529
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
9483
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
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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
8982
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
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
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
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
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.