473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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("KDGKBMODE");
exit(1);
}
switch(oldkbmode) {
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 1763
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("KDGKBMODE");
exit(1);
}
switch(oldkbmode) {
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.com...
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...@gmail.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...@gmail.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...@gmail.comwrote:
On Mar 12, 12:55 pm, Peter Nilsson <ai...@acay.com.auwrote:
Brice Rebsamen <brice.br...@gmail.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
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...
8
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...
4
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...
2
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...
44
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...
5
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...
4
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...
2
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.