473,396 Members | 2,024 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,396 software developers and data experts.

character assignment

I came across this bit of code:

char devname[4] = "wl0";
....
devname[2]++;

This looks wrong to me.... I am chasing a bad pointer issue in this
code, and I suspect this is not valid C.

--Yan
Jan 2 '07 #1
15 5212
"CptDondo" <ya*@NsOeSiPnAeMr.comwrote in message
news:12*************@corp.supernews.com...
>I came across this bit of code:

char devname[4] = "wl0";
...
devname[2]++;

This looks wrong to me.... I am chasing a bad pointer issue in this code,
and I suspect this is not valid C.
The second assignment makes sense. The collation sequence for the ASCII
digits is linear, so someone is trying to go from , for example, "wl0" to
"wl1".

The first assigment doesn't make sense to me. "wl0" (which will include a
zero terminator) is a statically allocated string managed by the compiler.
I'm not aware that a typical compiler will allow this syntax. The string
should have the type of (char *), perhaps with const.

How about:

char devname[4] = {'w', 'l', '0', 0};

?

And there are no warnings or errors from the compiler?
Jan 2 '07 #2
David T. Ashley wrote:
"CptDondo" <ya*@NsOeSiPnAeMr.comwrote in message
news:12*************@corp.supernews.com...
>I came across this bit of code:

char devname[4] = "wl0";
...
devname[2]++;

This looks wrong to me.... I am chasing a bad pointer issue in this code,
and I suspect this is not valid C.

The second assignment makes sense. The collation sequence for the ASCII
digits is linear, so someone is trying to go from , for example, "wl0" to
"wl1".
Right - except, as you point out, the string is static, so I don't see
how the bloody thing is incremented later.
>
The first assigment doesn't make sense to me. "wl0" (which will include a
zero terminator) is a statically allocated string managed by the compiler.
I'm not aware that a typical compiler will allow this syntax. The string
should have the type of (char *), perhaps with const.

How about:

char devname[4] = {'w', 'l', '0', 0};
OK, I'll try that.
>
?

And there are no warnings or errors from the compiler?

None that I've seen, but the development environment hides a lot of
information. As it apparently works for the developer, but not for me,
there resulting errors may well be gcc-version dependent.

In the older, working version of the code, they assign

char *devname = "wl0";

and then increment that. (??? This really makes no sense.)

I'm going to rewrite it correctly, and see if that fixes the issue.
Jan 2 '07 #3
In article <Ub******************************@giganews.com>,
David T. Ashley <dt*@e3ft.comwrote:
>"CptDondo" <ya*@NsOeSiPnAeMr.comwrote in message
news:12*************@corp.supernews.com...
>>I came across this bit of code:
>char devname[4] = "wl0";
>The first assigment doesn't make sense to me. "wl0" (which will include a
zero terminator) is a statically allocated string managed by the compiler.
I'm not aware that a typical compiler will allow this syntax.
char *devname = "wl0";

would be the case of a (theoretically read-only) string managed by
the compiler. (If my memory holds, the compiler is not -required- to
statically allocate the string... that's just by far the easiest way to
meet the lifetime requirements.)
char devname[4] = "wl0";

is completely valid C. The C standard specifically indicates that
an array of char may be initialized by a string literal, with
successive elements of the literal being assigned into successive
elements of the array. The resulting array will be writable.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Jan 2 '07 #4

"David T. Ashley" <dt*@e3ft.comwrote in message
news:Ub******************************@giganews.com ...
"CptDondo" <ya*@NsOeSiPnAeMr.comwrote in message
news:12*************@corp.supernews.com...
>>I came across this bit of code:

char devname[4] = "wl0";
...
devname[2]++;

This looks wrong to me.... I am chasing a bad pointer issue in this code,
and I suspect this is not valid C.

The second assignment makes sense. The collation sequence for the ASCII
digits is linear, so someone is trying to go from , for example, "wl0" to
"wl1".
Yes, and the language guarantees that the digit characters ('0'
through '9') do indeed have consecutive values.
>
The first assigment doesn't make sense to me.
The first line is not an assignment, it's an initialization,
and is indeed valid. It creates an array of four characters,
initializes the first with 'w', the second with '1', the third
with '0', and the fourth with zero. (i.e. a zero-terminated
string).
"wl0" (which will include a zero terminator) is a statically allocated
string managed by the compiler.
... which is placed in the array 'devname'.
I'm not aware that a typical compiler will allow this syntax.
Any conforming compiler must.
>The string should have the type of (char *), perhaps with const.
No, that would not be a string. That would be a pointer.
>
How about:

char devname[4] = {'w', 'l', '0', 0};
That is also valid. But what OP posted is just as valid,
and far more common (I suppose because it's easier to type
and to read).

I think you need to do some reading about arrays and pointers
in C.

See the reviews at www.accu.org for book selections.

-Mike
And there are no warnings or errors from the compiler?
There shouldn't be any. Nothing is wrong.

-Mike
Jan 2 '07 #5
In article <12*************@corp.supernews.com>,
CptDondo <ya*@NsOeSiPnAeMr.comwrote:
>David T. Ashley wrote:
>"CptDondo" <ya*@NsOeSiPnAeMr.comwrote in message
news:12*************@corp.supernews.com...
>>char devname[4] = "wl0";
>>devname[2]++;
>The second assignment makes sense. The collation sequence for the ASCII
digits is linear, so someone is trying to go from , for example, "wl0" to
"wl1".
>Right - except, as you point out, the string is static, so I don't see
how the bloody thing is incremented later.
The string is not static. The relevant section of C89 (X3.159-1989)
is 3.5.7; I don't know the corresponding ISO section number.

devname[2]++; is not going to create any pointer problems for you.

However, note that devname will have lifetime appropriate to its
scope, whereas char *devname = "wl0"; would point to something
with essentially indefinite lifetime but which is theoretically
read-only.

Based on what you have said, the code previously relied upon
the indefinite lifetime, and relied upon literals being writable.
The corresponding correct code would be

static char devname[4] = "wl0";

That would only be initialized once, not once per visit to the
function.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Jan 2 '07 #6
>>>>"DTA" == David T Ashley <dt*@e3ft.comwrites:

DTA"CptDondo" <ya*@NsOeSiPnAeMr.comwrote in message
DTAnews:12*************@corp.supernews.com...
>I came across this bit of code:

char devname[4] = "wl0"; ... devname[2]++;

This looks wrong to me.... I am chasing a bad pointer issue in
this code, and I suspect this is not valid C.
It is, actually, valid C.

DTAThe first assigment doesn't make sense to me. "wl0" (which
DTAwill include a zero terminator) is a statically allocated
DTAstring managed by the compiler.

....which is used as an initializer for the char array that's allocated
in that line.

DTAHow about:

DTA char devname[4] = {'w', 'l', '0', 0};

DTA?

Functionally identical to what's there, but a lot harder to make sense of.

DTAAnd there are no warnings or errors from the compiler?

It's valid C - why should the compiler complain?

Charlton

--
Charlton Wilbur
cw*****@chromatico.net
Jan 2 '07 #7
On Tue, 02 Jan 2007 11:52:27 -0800, CptDondo <ya*@NsOeSiPnAeMr.com>
wrote:
>I came across this bit of code:

char devname[4] = "wl0";
...
devname[2]++;

This looks wrong to me.... I am chasing a bad pointer issue in this
code, and I suspect this is not valid C.
Keep looking <g>. The expression is one I would avoid, but it's not
causing your problem.

--
Al Balmer
Sun City, AZ
Jan 2 '07 #8
"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.cawrote in message
news:en**********@canopus.cc.umanitoba.ca...
>
>>The first assigment doesn't make sense to me. "wl0" (which will include a
zero terminator) is a statically allocated string managed by the compiler.
I'm not aware that a typical compiler will allow this syntax.

char *devname = "wl0";

would be the case of a (theoretically read-only) string managed by
the compiler. (If my memory holds, the compiler is not -required- to
statically allocate the string... that's just by far the easiest way to
meet the lifetime requirements.)

char devname[4] = "wl0";

is completely valid C. The C standard specifically indicates that
an array of char may be initialized by a string literal, with
successive elements of the literal being assigned into successive
elements of the array. The resulting array will be writable.
Thanks for the correction. I've never seen that initialization syntax.

I'm only familiar with:

char *p = "I like coconuts and grapes.";

and it was my understanding that in that case that compiler was assigning a
pointer into a static string pool.

But I may be wrong there again, too! Beat me up more if I blew that one,
too!

Thanks.
Jan 2 '07 #9
>>>>"CD" == CptDondo <ya*@NsOeSiPnAeMr.comwrites:

CDRight - except, as you point out, the string is static, so I
CDdon't see how the bloody thing is incremented later.

The string literal is used as an initializer for the array that's
allocated.

CDIn the older, working version of the code, they assign

CDchar *devname = "wl0";

CDand then increment that. (??? This really makes no sense.)

When you write

char devname[4] = "wl0";

you are allocating an array of 4 chars and initializing it to 'w',
'l', '0', 0. That char string is modifiable, and has automatic
storage duration. In many implementations, it would be stored on the
stack.

When you write

char *devname = "wl0";

you allocate a pointer that points to that string literal. The
compiler may store it anywhere it wants; you can't modify it without
invoking undefined behavior. The pointer variable has automatic
storage duration. In some implementations, the string would be stored
in a read-only segment of the executable.

CDI'm going to rewrite it correctly, and see if that fixes the
CDissue.

char devname[4] = "wl0"; *is* written correctly.

Charlton
--
Charlton Wilbur
cw*****@chromatico.net
Jan 2 '07 #10
In article <po******************************@giganews.com>,
David T. Ashley <dt*@e3ft.comwrote:
>I'm only familiar with:

char *p = "I like coconuts and grapes.";

and it was my understanding that in that case that compiler was assigning a
pointer into a static string pool.

But I may be wrong there again, too! Beat me up more if I blew that one,
too!
You are right, in that case the string may be in read-only memory and
(I think) shared with other strings.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jan 2 '07 #11
In article <en***********@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <ri*****@cogsci.ed.ac.ukwrote:
>In article <po******************************@giganews.com>,
David T. Ashley <dt*@e3ft.comwrote:
>>I'm only familiar with:
>>char *p = "I like coconuts and grapes.";
>>and it was my understanding that in that case that compiler was assigning a
pointer into a static string pool.
>You are right, in that case the string may be in read-only memory and
(I think) shared with other strings.
In C89, literal strings need not be distinct, and need not be writable.
But in C89, I cannot find any indication of the scope or lifetime
of a string literal, so I do not think it is correct to say that
the compiler is necessarily "assigning a pointer into a static string pool".

There must be -something- about the lifetime, as it is well understood
that pointers to literals may be returned and may be stored and passed
around. The common usages are such that string literals must have
a lifetime "as if" equivilent to that of static variables, but if the
compiler can determine that a particular literal's address is not
being squirreled away, I see no documented limitation that would
prevent the compiler from effectively making the string literal local
to a routine -- possibly even coding the contents right into
the instruction space. I have these flashbacks to disassembling
in my Z80 days...
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Jan 2 '07 #12
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In C89, literal strings need not be distinct, and need not be writable.
But in C89, I cannot find any indication of the scope or lifetime
of a string literal, so I do not think it is correct to say that
the compiler is necessarily "assigning a pointer into a static string pool".
C89 (or at least a late draft) says:

A character string literal has static storage duration and
type ``array of char,'' and is initialized with the given
characters.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Jan 2 '07 #13
Walter Roberson wrote:
>
.... snip ...
prevent the compiler from effectively making the string literal
local to a routine -- possibly even coding the contents right into
the instruction space. I have these flashbacks to disassembling
in my Z80 days...
What makes you think such coding into instructions isn't static
storage?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Jan 3 '07 #14
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>Walter Roberson wrote:
>... snip ...
>prevent the compiler from effectively making the string literal
local to a routine -- possibly even coding the contents right into
the instruction space. I have these flashbacks to disassembling
in my Z80 days...
>What makes you think such coding into instructions isn't static
storage?
a) It wouldn't be a "static pool of strings";
b) In my Z80 days, bank switching was used to extend memory, and
it was not uncommon for a swappable bank to include instructions. It
is debatable as to whether such a situation would be "static
storage" or not.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Jan 3 '07 #15

CptDondo wrote:
I came across this bit of code:

char devname[4] = "wl0";
...
devname[2]++;

This looks wrong to me.... I am chasing a bad pointer issue in this
code, and I suspect this is not valid C.
Both are valid C. I remember that old C compilers would require this:
static char devname[4] = "wI0";
But it seems that since c89 this restraint no longer exists.

Jan 5 '07 #16

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

Similar topics

37
by: chandy | last post by:
Hi, I have an Html document that declares that it uses the utf-8 character set. As this document is editable via a web interface I need to make sure than high-ascii characters that may be...
8
by: Sharon | last post by:
hi, I want to compare character, if the string contains character "-" then it will print Hello on the screen however, neither method (1) nor method (2) work in the code below: so what the correct...
15
by: ehabaziz2001 | last post by:
Hi, Till now I do not understand how the null character automatically added to the end of the string and it is not an element of the string array . All books said the null character (\0) added...
10
by: gk245 | last post by:
I have something like this: #include <stdio.h> main () { struct line { char write; char read;
6
by: nagrik | last post by:
Hello group, I am reading an 'mpeg file' from the socket. In read socket I specify a 'char* buffer' to read the file. However, the content of actual data contain '\0' characters at various...
12
by: amer.terzic | last post by:
Here's what my code is supposed to do (it seems to work fine) Take a char* array and remove a first character from it....as simple as that. The mentioned char* array is a 'global' var shared...
8
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer =...
2
AmberJain
by: AmberJain | last post by:
Hello everyone, Right now I am learning C and I'm using a BORLAND TURBO C++ 3.0 compiler to compile my programs. Well I am facing a problem with one of my program which I got as my assignment. I...
13
by: Ivan | last post by:
Hi, What is the best syntax to use a char to index into an array. /////////////////////////////////// For example int data; data = 1;
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
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...
0
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...
0
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...

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.