473,769 Members | 6,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assigning values to char arrays

Hi all,

here's an elementary question. Assume I have declared two variables,

char *a, **b;

I can then give a value to a like

a="hello world";

The question is, how should I assign values to b? A simple

b[0]="string";

results in a segmentation fault.

Answers greatly appreciated.

Regards, Emyl.

Nov 2 '07
43 17231
Ark Khasin wrote:
Ben Bacarisse wrote:
<snip>
>No. unsigned char may not have padding bits. All the bits must be
value bits.
Why?
6.2.6.2 says "For unsigned integer types other than unsigned char, the
bits of the object representation shall be divided into two groups:
value bits and padding bits (there need not be any of the latter).
But I couldn't find anything saying that unsigned char *may not* have
padding bits.
Well the above quote says that unsigned char may not have _both_ padding
and value bits. Obviously the bit type left out has to be padding
bits - otherwise one would not be able to potably use unsigned char
objects.

<snip>

Nov 3 '07 #11
santosh wrote:
Ark Khasin wrote:
>Ben Bacarisse wrote:

<snip>
>>No. unsigned char may not have padding bits. All the bits must be
value bits.
>Why?
6.2.6.2 says "For unsigned integer types other than unsigned char, the
bits of the object representation shall be divided into two groups:
value bits and padding bits (there need not be any of the latter).
But I couldn't find anything saying that unsigned char *may not* have
padding bits.

Well the above quote says that unsigned char may not have _both_ padding
and value bits. Obviously the bit type left out has to be padding
bits - otherwise one would not be able to potably use unsigned char
objects.
Is this "just a theory"? IMHO, 6.2.6.2 says *exactly nothing* about
unsigned char.
Nov 3 '07 #12
santosh wrote:
Ark Khasin wrote:
>Richard wrote:

<snip>
>I am not to argue who of us two is more of a newbie, but your post
sheds no light on the question asked. Ego bubbling?

This is most hilarious sentence I've read in c.l.c. this year.
Ty. But Richard offered a satisfactory explanation.
--
Ark
Nov 3 '07 #13
Ark Khasin wrote:
santosh wrote:
>Ark Khasin wrote:
>>Ben Bacarisse wrote:

<snip>
>>>No. unsigned char may not have padding bits. All the bits must be
value bits.
>>Why?
6.2.6.2 says "For unsigned integer types other than unsigned char,
the bits of the object representation shall be divided into two
groups: value bits and padding bits (there need not be any of the
latter). But I couldn't find anything saying that unsigned char *may
not* have padding bits.

Well the above quote says that unsigned char may not have _both_
padding and value bits. Obviously the bit type left out has to be
padding bits - otherwise one would not be able to potably use
unsigned char objects.
Is this "just a theory"? IMHO, 6.2.6.2 says *exactly nothing* about
unsigned char.
<quote n1256.pdf>

6.2.6.2 Integer types

1 For unsigned integer types other than unsigned char, the bits of the
object representation shall be divided into two groups: value bits and
padding bits (there need not be any of the latter).

<endquote>

Note closely the text within the parenthesis. To me it _strongly_
implies, to say the least, that value bits are mandatory for objects of
all unsigned integer types. Since unsigned char is disallowed from
having padding bits, it must be composed only of value bits.

<quote n1256.pdf>

If there are N value bits, each bit shall represent a different power of
2 between 1 and 2N-1, so that objects of that type shall be capable of
representing values from 0 to 2N -1 using a pure binary representation;
this shall be known as the value representation. The values of any
padding bits are unspecified.44)

6.2.6.1

3 Values stored in unsigned bit-fields and objects of type unsigned char
shall be represented using a pure binary notation.40)

<endquote>

Again 6.2.6.1(3) in conjunction with 6.2.6.2(1) reinforces the
requirement that unsigned char may not have padding bits.

<quote n1256.pdf>

4 Values stored in non-bit-field objects of any other object type
consist of n ´ CHAR_BIT bits, where n is the size of an object of that
type, in bytes. The value may be copied into an object of type unsigned
char [n] (e.g., by memcpy); the resulting setof bytes is
called the object representation of the value. Values stored in
bit-fields consist of m bits, where m is the size specified for the
bit-field. The object representation is the set of m bits the bit-field
comprises in the addressable storage unit holding it. Two values (other
than NaNs) with the same object representation compare equal, but values
that compare equal may have different object representations .

<endquote>

This answers the other issue that you raised concerning null pointers
not being all bits zero.

Nov 3 '07 #14
Ark Khasin wrote:
>
santosh wrote:
Ark Khasin wrote:
Ben Bacarisse wrote:
<snip>
>No. unsigned char may not have padding bits.
Is this "just a theory"?
No.

N869
5.2.4.2.1 Sizes of integer types <limits.h>

[#2] The value UCHAR_MAX+1
shall equal 2 raised to the power CHAR_BIT.

--
pete
Nov 3 '07 #15
pete wrote:
Ark Khasin wrote:
>santosh wrote:
>>Ark Khasin wrote:
Ben Bacarisse wrote:
<snip>

No. unsigned char may not have padding bits.
>Is this "just a theory"?

No.

N869
5.2.4.2.1 Sizes of integer types <limits.h>

[#2] The value UCHAR_MAX+1
shall equal 2 raised to the power CHAR_BIT.
Thanks to adding to my confusion :)
So I have an 11-bit machine bytes and UCHAR_MAX==8 and 3 padding most
significant bits. Anything wrong?
BTW, if I am not mistaken, in other integer types padding bits don't
have to be contiguous.
--
Ark
Nov 3 '07 #16
Ark Khasin wrote:
pete wrote:
>Ark Khasin wrote:
>>santosh wrote:
Ark Khasin wrote:
Ben Bacarisse wrote:
<snip>

>No. unsigned char may not have padding bits.
>>Is this "just a theory"?

No.

N869
5.2.4.2.1 Sizes of integer types <limits.h>

[#2] The value UCHAR_MAX+1
shall equal 2 raised to the power CHAR_BIT.
Thanks to adding to my confusion :)
So I have an 11-bit machine bytes and UCHAR_MAX==8 and 3 padding most
significant bits. Anything wrong?
What do you mean by UCHAR_MAX==8? Do you mean CHAR_BIT==8?

As far as the Standard is concerned a char i.e., a byte (as defined by
C) contains CHAR_BIT bits. Additionally unsigned char may not contain
padding bits.

I don't know what you mean by "machine bytes" above. Are they supposed
to be different from C bytes?
BTW, if I am not mistaken, in other integer types padding bits don't
have to be contiguous.
Yes. Padding bits need not be contiguous.

Nov 3 '07 #17
Ark Khasin wrote, On 03/11/07 20:05:
pete wrote:
>Ark Khasin wrote:
>>santosh wrote:
Ark Khasin wrote:
Ben Bacarisse wrote:
<snip>

>No. unsigned char may not have padding bits.
>>Is this "just a theory"?

No.

N869
5.2.4.2.1 Sizes of integer types <limits.h>

[#2] The value UCHAR_MAX+1 shall equal 2 raised to the
power CHAR_BIT.
Thanks to adding to my confusion :)
So I have an 11-bit machine bytes and UCHAR_MAX==8 and 3 padding most
significant bits. Anything wrong?
CHAR_BIT is the number of bits in a signed, unsigned and plain char.
Note, the number of bits, NOT the number of value bits. Therefore, as
UCHAR_MAX is 2 raised to the power of CHAR_BIT all of the bits must be
value bits.
BTW, if I am not mistaken, in other integer types padding bits don't
have to be contiguous.
The padding bits can be anywhere, but short of using an unsigned char
pointer to look at the representation they are hard to get at since the
bitwise operations are defined as operating on values.
--
Flash Gordon
Nov 3 '07 #18
Ark Khasin wrote:
Ben Bacarisse wrote:
>Ark Khasin <ak*****@macroe xpressions.comw rites:
[>Ben Bacarisse wrote:]
....
>RH's point was something else altogether -- that all bits zero is not
guaranteed to produce a null pointer (to be scrupulously correct, it
is not guaranteed to produce a value that compares equal to a null
pointer constant).
The parenthesized comment was not actually needed to make the statement
"scrupulous ly correct"; it would have been just as correct, and less
confusing, without it.
That's where I am lost and reading the standard doesn't help:
What's the difference between a value of an object and how it compares
equal? I mean, if a==b, whatever their representations , in what
context(s) does it make sense to say they may have different values?
There is no difference. Don't let the unnecessary "clarificat ion"
confuse you. The issue isn't having different values with the same
representation in a single type - that can't happen. The issue is that
there can be multiple different representations of the same value in a
given type. However, the values of objects of that type containing those
different representations must compare equal.

You're tripping over a minor issue; the fact that there can be multiple
representations of a null pointer. However, you've lost track of the key
issue: that a pointer object with all of its bits set to 0 doesn't have
to be one of those representations . In fact, it doesn't have to
represent a valid pointer value of any kind.
[NEGATIVE_ZERO comes to mind - and goes away. BTW, is it fair to say
that bitwise logic is a magic performed on representations , and not on
values?]
No. In general, the bitwise operations are defined in terms of their
actions on the values, not the representations . For instance, E>>1 is
defined as dividing the value of E by 2. The complicated exceptions all
involve sign bits, and most result in undefined behavior, which is why
it's strongly recommended that bitwise operations be restricted to
unsigned types, or at least restricted to values which are guaranteed to
be positive both before and after the operation.
> void *a;;
memset_as_above (&a, 0, sizeof a);
There is, at this point, no guarantee that 'a' contains a valid pointer
representation. Therefore, the next line renders the behavior of your
entire program undefined:
> if (a == 0) {
/* not guaranteed */
//Which is correct but implies
{
void **pNULL = 0;
if(a==*pNULL) {
/* not guaranteed */
I'm not sure what your point was; but you've just attempted to
dereference a null pointer, again making the behavior undefined.
Nov 3 '07 #19
Ark Khasin wrote:
>
pete wrote:
Ark Khasin wrote:
santosh wrote:
Ark Khasin wrote:
Ben Bacarisse wrote:
<snip>

No. unsigned char may not have padding bits.
Is this "just a theory"?
No.

N869
5.2.4.2.1 Sizes of integer types <limits.h>

[#2] The value UCHAR_MAX+1
shall equal 2 raised to the power CHAR_BIT.
Thanks to adding to my confusion :)
So I have an 11-bit machine bytes
That's what "CHAR_BIT equals eleven" means.

--
pete
Nov 3 '07 #20

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

Similar topics

1
2129
by: Ched | last post by:
Hi, I am performing a MySQL SELECT (which returns multiple rows) to $result and then extracting the results with mysql_fetch_array($result). I then want to build a number of arrays using the fields of each row. Here is the relevant code: <snip> $result = $i = 1;
2
3392
by: Zitan Broth | last post by:
Greetings All, Running pg 7.3.4 and was reading: http://archives.postgresql.org/pgsql-interfaces/2003-09/msg00018.php . Basically want to assign values to an array and then a 2d array. However I can't get this to run in properly I get a syntax error (at or near ", output_txt_arr TEXT, output_str text ); CREATE OR REPLACE FUNCTION F_TEST(TEXT) RETURNS NUMERIC AS '
14
74899
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0; myarray=0; myarray=1; myarray=8;
8
8021
by: chacha | last post by:
char cSubject; chat *h_ptr; If I want to copy the char array pointed by h_ptr to cSubject character by character, does the code looke like the following: strcpy(cSubject+i,*(h_ptr)+i);
2
4697
by: shan_rish | last post by:
Hi CLCers, In the below program the error message while compiling is /home1/murugan/prog/cprog >cc -o struct_eval struct_eval.c cc: "struct_eval.c", line 14: error 1549: Modifiable lvalue required for assignment operator. cc: "struct_eval.c", line 17: error 1549: Modifiable lvalue required for assignment operator. cc: "struct_eval.c", line 18: error 1549: Modifiable lvalue required for assignment operator.
2
7053
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs, input box for units of service, description of the service and each row has its own dropdown list of unit fees that apply. Each dynamically created row will return 3 values fee1_choice, fee1_unit and fee1_money. Note The above informaton is...
3
3495
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/ #include <cstdio> #define MAX_WORD_LEN 80 #define MAX_SIZE 1000
17
18770
by: Cliff | last post by:
Hi, I'm in the process of porting some code from a 3rd party and have hit a problem with the following: typedef struct { unsigned char Red; unsigned char Green; unsigned char Blue; }TBoxColour;
11
2061
by: rep_movsd | last post by:
Hi I program primarily in C++ , but once in a while one is forced to use the odd strcpy or call API functions that dump results into char* buffers. I believe that most security exploits that work by thrashing the stack to overwrite the return address, allowing arbitrary code execution. I have now fallen into the habit of declaring temporary buffers as static char arrays.
7
4515
by: daniel | last post by:
Hello , I always had the feeling that is better to have char arrays with the size equal to a power of two. For example: char a_str; // feels ok char b_str; //feels not ok.
0
9423
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
10216
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
10049
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...
1
9997
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5309
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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

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.