473,566 Members | 3,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initialisation of a char pointer using char *s = "something"

Reading the code from showkey.c (from package kbd) I found this type
of code:

char *m;
m = "RAW";

See below for the complete code. How can this work? I would have used
strdup, or allocation of the memory for m (static or dynamic) then
strncpy.

Thanks
Brice
static void
get_mode(void) {
char *m;

if (ioctl(fd, KDGKBMODE, &oldkbmode)) {
perror("KDGKBMO DE");
exit(1);
}
switch(oldkbmod e) {
case K_RAW:
m = "RAW"; break;
case K_XLATE:
m = "XLATE"; break;
case K_MEDIUMRAW:
m = "MEDIUMRAW" ; break;
case K_UNICODE:
m = "UNICODE"; break;
default:
m = _("?UNKNOWN?" ); break;
}
printf(_("kb mode was %s\n"), m);
if (oldkbmode != K_XLATE) {
printf(_("[ if you are trying this under X, it might not work\n"
"since the X server is also reading /dev/console ]\n"));
}
printf("\n");
}
Mar 12 '08 #1
7 1773
Brice Rebsamen wrote:
Reading the code from showkey.c (from package kbd) I found this type
of code:

char *m;
m = "RAW";
The character pointer m (which should probably be a const char*) points
to the string literal "RAW".
See below for the complete code. How can this work? I would have used
strdup, or allocation of the memory for m (static or dynamic) then
strncpy.
Remember m it a pointer.

--
Ian Collins.
Mar 12 '08 #2
On Tue, 11 Mar 2008 21:10:20 -0700,Brice Rebsamen wrote:
Reading the code from showkey.c (from package kbd) I found this type of
code:

char *m;
m = "RAW";

See below for the complete code. How can this work? I would have used
strdup, or allocation of the memory for m (static or dynamic) then
strncpy.
In C, you should treat the string "RAW" as a pointer to char.
>
Thanks
Brice
static void
get_mode(void) {
char *m;

if (ioctl(fd, KDGKBMODE, &oldkbmode)) {
perror("KDGKBMO DE");
exit(1);
}
switch(oldkbmod e) {
case K_RAW:
m = "RAW"; break;
case K_XLATE:
m = "XLATE"; break;
case K_MEDIUMRAW:
m = "MEDIUMRAW" ; break;
case K_UNICODE:
m = "UNICODE"; break;
default:
m = _("?UNKNOWN?" ); break;

Hmm, you must have a function named "_", and it takes a (const) char*
parameter, right?

Mar 12 '08 #3
"WANG Cong" <xi************ @gmail.comwrote in message
news:fr******** **@news.cn99.co m...
On Tue, 11 Mar 2008 21:10:20 -0700,Brice Rebsamen wrote:
>Reading the code from showkey.c (from package kbd) I found this type of
code:

char *m;
m = "RAW";

See below for the complete code. How can this work? I would have used
strdup, or allocation of the memory for m (static or dynamic) then
strncpy.

In C, you should treat the string "RAW" as a pointer to char.
[...]

What about treating it as a pointer to a const char?

[...]

Mar 12 '08 #4
Brice Rebsamen <brice.br...@gm ail.comwrote:
Reading the code from showkey.c (from package kbd) I
found this type of code:

char *m;
m = "RAW";

See below for the complete code. How can this work?
What makes you think it can't?

I suspect your confusion stems from m not being const
qualified, and string literals having type char[] in C.
I would have used strdup, or allocation of the memory
for m (static or dynamic) then strncpy.
<snip>

Why bother?

Take a look at the following and see if the penny drops...

#include <stdio.h>

void foo(const char *m)
{
puts(m);
}

void bar(void)
{
const char *m = "Hello"; /* what's the diff? */
puts(m);
}

int main(void)
{
foo("Hello");
bar();
return 0;
}

Given the points I mentioned earlier, realise that
I could leave out the const-s, though it wouldn't
be good form.

--
Peter
Mar 12 '08 #5
Brice Rebsamen wrote:
Reading the code from showkey.c (from package kbd) I found this type
of code:

char *m;
m = "RAW";

See below for the complete code. How can this work? I would have used
strdup, or allocation of the memory for m (static or dynamic) then
strncpy.

...
This is not a meaningful question. It obviously does work, and my
immediate response to your question was "how can it not work". You need
to explain why you find this surprising, then we'll be able to sort out
your confusion.
Mar 12 '08 #6
On Mar 12, 12:55 pm, Peter Nilsson <ai...@acay.com .auwrote:
Brice Rebsamen <brice.br...@gm ail.comwrote:
Reading the code from showkey.c (from package kbd) I
found this type of code:
char *m;
m = "RAW";
See below for the complete code. How can this work?

What makes you think it can't?

I suspect your confusion stems from m not being const
qualified, and string literals having type char[] in C.
I would have used strdup, or allocation of the memory
for m (static or dynamic) then strncpy.

<snip>

Why bother?

Take a look at the following and see if the penny drops...

#include <stdio.h>

void foo(const char *m)
{
puts(m);
}

void bar(void)
{
const char *m = "Hello"; /* what's the diff? */
puts(m);
}

int main(void)
{
foo("Hello");
bar();
return 0;
}

Given the points I mentioned earlier, realise that
I could leave out the const-s, though it wouldn't
be good form.

--
Peter

Still I'm confused. Take a look at the following:

const char *getm1(void) { char m[] = "Hello"; return m; }
const char *getm2(void) { return "Hello"; }

getm1 raises a warning about returning the address of a local variable
and returns a corrupted string. This I have known for a long time. My
explanation is that the memory is released when the function returns,
therefore whatever happens to m is undefined (possibly overwritten).
I don't understand why it's not the case with getm2().

Mar 12 '08 #7
On Mar 12, 2:05 pm, Brice Rebsamen <brice.br...@gm ail.comwrote:
On Mar 12, 12:55 pm, Peter Nilsson <ai...@acay.com .auwrote:
Brice Rebsamen <brice.br...@gm ail.comwrote:
Reading the code from showkey.c (from package kbd) I
found this type of code:
char *m;
m = "RAW";
See below for the complete code. How can this work?
What makes you think it can't?
I suspect your confusion stems from m not being const
qualified, and string literals having type char[] in C.
I would have used strdup, or allocation of the memory
for m (static or dynamic) then strncpy.
<snip>
Why bother?
Take a look at the following and see if the penny drops...
#include <stdio.h>
void foo(const char *m)
{
puts(m);
}
void bar(void)
{
const char *m = "Hello"; /* what's the diff? */
puts(m);
}
int main(void)
{
foo("Hello");
bar();
return 0;
}
Given the points I mentioned earlier, realise that
I could leave out the const-s, though it wouldn't
be good form.
--
Peter

Still I'm confused. Take a look at the following:

const char *getm1(void) { char m[] = "Hello"; return m; }
const char *getm2(void) { return "Hello"; }

getm1 raises a warning about returning the address of a local variable
and returns a corrupted string. This I have known for a long time. My
explanation is that the memory is released when the function returns,
therefore whatever happens to m is undefined (possibly overwritten).
I don't understand why it's not the case with getm2().
Messages come really fast here! I found a clear answer in c-faq 1.32.
Thanks all.
Mar 12 '08 #8

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

Similar topics

14
2021
by: juglesh | last post by:
"$string = isset($xyz) ? $xyz : "something else";" Hello, someone gave code like this in another thread. I understand (by inference) what it does, but have not found any documentation on this type of syntax. Any one have links to this shortuct(?) syntax and other types of
8
2210
by: Sam Sungshik Kong | last post by:
Hello! I use Python for ASP programming. I found something weird. Response.Write(Request("something")) It draws "None" when there's no value for something. Actually I expect "" instead of "None". So I changed it like
4
2041
by: Rob Smeets | last post by:
Hi all, I have the following problem: I have to revise a c++ dll. And i'm new to c++. I have to change a function, but i cannot change it's structure. I want to check the parameters and act according to those parameters. I've tried several ways to resolve it, but without success. Could anybody help? Thanks in advance. Rob Smeets
2
1431
by: Helen | last post by:
I've written a web control that allows you to hide certain bits of your page when the specified selectbox has a certain value. It's a simple enough bit of logic, but I got sick of coding it again and again in different pages. Now I have a page that needs the same functionality except with a checkbox rather than a dropdown. The code I need...
44
4164
by: Tolga | last post by:
As far as I know, Perl is known as "there are many ways to do something" and Python is known as "there is only one way". Could you please explain this? How is this possible and is it *really* a good concept?
5
2302
by: tuxedo | last post by:
The way the <body onload="something()"works ensures that the complete html document is loaded before something() is executed. Can the same be achieved when placing the onload call in document somewhere except within the body tag, or must it always be in the body tag? For example, if this is placed elsewhere ... window.onload(something())
4
1570
by: marcwentink | last post by:
This probably is a very noob question What is the return type of Session("Something")? I want to change: If Not Session("Name") Is Nothing Then LoginId = Session("Name").ToString to something like
2
4023
by: =?ISO-8859-1?Q?Pekka_J=E4rvinen?= | last post by:
Hi, How I can find <?something ?stuff? XML: <Wix> <?define UpgradeCode="{foobar-quux-xyzzy}"?> <?define Manufacturer="Company"?> <!-- update this ALWAYS --> <?define PackageCode="REPLACE"?>
0
7673
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...
0
7893
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. ...
1
7645
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
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...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
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...
0
3643
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...
0
926
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...

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.