473,769 Members | 5,878 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 17230
James Kuyper <ja*********@ve rizon.netwrites :
Ark Khasin wrote:
...
>Is it so that a consensus emerges that ^ | & on negative numbers
depend

I can't speak for the consensus; this is just my understanding of what
the standard says. In many cases my understanding differs distinctly
from the consensus understanding.
>on representation (or, to tell the truth, act on representations )
and so are implementation-defined (although only 3 ways to implement
are recognized)?

Those operators don't act on the representations of objects; otherwise
cases like

x += (a+1) | (b-2);
You have written a correct statement because, of course, a+1 is not an
object. The question is, does '(a+1) | (b-2)' depend (or act) on the
representation of 'a+1' and 'b-2' and I'd say it does.

Section 6.2.6 is called "Representation s of types" (not objects) and
6.2.6.1 p4 takes some pains to define a new term -- the "object
representation" -- which is not quite the same thing as the
representation of the type.

I don't want to suggest that | operates on padding bits. I don't
think one can determine that either way, but it seems to be pushing the
meaning of representation to its limits (and beyond that used in the
standard) to say that |, &, and ^ act on the value not the
representation.
would be undefined. The way that they operate on or generate negative
values must, however, be consistent with whichever of 3 permitted ways
of handling the sign bits that the implementation chose for the
relevant type.
These "permitted ways" are described in the section called
"Representa tion of types". Your sentence would be simpler if you had
said that the bits they operate on (and the meaning of the bits that
are produced) are determined by the representation used for signed
integer types.

It is possible to define the result of | on signed types by talking
only about the value of the operands, but it is complicated to do so
and the standard clearly distinguishes between the terms
"representation " and "object representation" so that there is simple
way to understand the operation. The standard uses only few words to
define | (and the others) because the term "correspond ing bits" is
clearly intended to refer back to the bits used to represent the
value as described in 6.2.6.
I see no way for those operators to operate on the
padding bits in any sense that is meaningful within the context of the
standard.
I agree, but padding bits are only one part of the representation of
the type.

--
Ben.
Nov 5 '07 #41
Ben Bacarisse wrote:
James Kuyper <ja*********@ve rizon.netwrites :
....
>Those operators don't act on the representations of objects; otherwise
cases like

x += (a+1) | (b-2);

You have written a correct statement because, of course, a+1 is not an
object. The question is, does '(a+1) | (b-2)' depend (or act) on the
representation of 'a+1' and 'b-2' and I'd say it does.

Section 6.2.6 is called "Representation s of types" (not objects) and
6.2.6.1 p4 takes some pains to define a new term -- the "object
representation" -- which is not quite the same thing as the
representation of the type.
That is not clear to me. The standard defines what "object
representation" means, but it never defines what "representation " means.
As far as I can tell, the term "representation " is almost always used
in the standard as a short form for "object representation" . The
standard uses the term "value representation" exactly once, in
6.2.6.2p2, and oddly enough, while it is worded as a definition, it not
italicized as a definition should be. If it were an official definition,
from context it would appear to be defined only for unsigned types.

When the word "representation " is used without either "object" or
"value" preceding it, I found only a few cases where it was clear that
it was not a reference to the object representation; in every case, it
was also clear that it did not refer to the value representation.
Examples include such things as the representation of characters on the
display screen, or the representation of data in the output file. In
both of those cases, that only thing the standard says is that those are
details outside of its scope.

I'm not saying there's no instances where a lone "representation "
clearly refers to the value representation, where inserting the word
"object" before "representation " would clearly change the intended
meaning. There's too many instances for me to check reliably. However, I
didn't find any - do you know of any?
>would be undefined. The way that they operate on or generate negative
values must, however, be consistent with whichever of 3 permitted ways
of handling the sign bits that the implementation chose for the
relevant type.

These "permitted ways" are described in the section called
"Representa tion of types". Your sentence would be simpler if you had
said that the bits they operate on (and the meaning of the bits that
are produced) are determined by the representation used for signed
integer types.
Which is the way that the standard says the same thing. I don't think
there's a difference in meaning, just a difference in clarity.

....
and the standard clearly distinguishes between the terms
"representation " and "object representation" so that there is simple
Citation, please? The definition of "object representation" does not
clearly distinguish them.
Nov 6 '07 #42
Ben Bacarisse wrote:
....
If section 6.2.6 was called "Object representation of types" so that
it was unambiguously about how values are stored and nothing else,
then I believe '-1 | -2' would be undefined. If values, not stored in
objects, do not have a representation, then what "bits" are there to
or together? It is because values may be represented as collections
of bits (as it happens in three different ways) that such expressions
can have a meaning defined by combining "correspond ing bits".
6.5p4 says "Some operators (the unary operator ~, and the binary
operators <<, >>, &, ^, and |, collectively described as bitwise
operators) are required to have operands that have integer type. These
operators yield values that depend on the internal representations of
integers, and have implementation-defined and undefined aspects for
signed types".

As I understand it, that statement about the "internal representations "
refers to the object representation, and does not imply that the
standard attaches any meaning to the representation of a value that is
not currently stored in any C object. It merely requires that the
bitwise operators act on values in such a way that if the resulting
value were saved in an object of that value's type, it would have the
correct bit pattern.

....
I have no other citation than the definition I already cited. I agree
the distinction is not crystal clear, but if your reading of the words
is that there is essentially no difference between "representation "
and "object representation" then how do you give '-1 | -2' a meaning?
You could, of course, say that | (and friends) behaves as if its
operands where stored in objects and the corresponding bits are
combined together, but you'd *still* be referencing the allowed
representations .
That's exactly how I understand it. I never denied that the allowed
representations were referenced, only that the concept of a
representation of a value only aquires a meaning in the event that it is
stored in an object.
Nov 7 '07 #43
James Kuyper <ja*********@ve rizon.netwrites :
Ben Bacarisse wrote:
...
>If section 6.2.6 was called "Object representation of types" so that
it was unambiguously about how values are stored and nothing else,
then I believe '-1 | -2' would be undefined. If values, not stored in
objects, do not have a representation, then what "bits" are there to
or together? It is because values may be represented as collections
of bits (as it happens in three different ways) that such expressions
can have a meaning defined by combining "correspond ing bits".

6.5p4 says "Some operators (the unary operator ~, and the binary
operators <<, >>, &, ^, and |, collectively described as bitwise
operators) are required to have operands that have integer type. These
operators yield values that depend on the internal representations of
integers, and have implementation-defined and undefined aspects for
signed types".

As I understand it, that statement about the "internal
representations " refers to the object representation, and does not
imply that the standard attaches any meaning to the representation of
a value that is not currently stored in any C object.
I can accept that this is the intent, but I do not think it is clear
and unambiguous. Since it can't be detected by a C program, I don't
really care if there is one representation (when a value is stored) or
another, transient one, as well. The latter just seemed to me a
convenient expository device for exactly these cases, but if it is a
fiction of my imagination, so be it!
It merely
requires that the bitwise operators act on values in such a way that
if the resulting value were saved in an object of that value's type,
it would have the correct bit pattern.

...
>I have no other citation than the definition I already cited. I agree
the distinction is not crystal clear, but if your reading of the words
is that there is essentially no difference between "representation "
and "object representation" then how do you give '-1 | -2' a meaning?
You could, of course, say that | (and friends) behaves as if its
operands where stored in objects and the corresponding bits are
combined together, but you'd *still* be referencing the allowed
representation s.

That's exactly how I understand it. I never denied that the allowed
representations were referenced,
Well that was exactly what I thought you were doing when you said:

"In general, the bitwise operations are defined in terms of their
actions on the values, not the representations ."

I just can't square this with your last remark above. For four of the
six, the representation is a required part of the definition.
only that the concept of a
representation of a value only aquires a meaning in the event that it
is stored in an object.

--
Ben.
Nov 7 '07 #44

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

Similar topics

1
2128
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
9589
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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
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...
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...
0
8872
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...
1
7410
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
5307
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...
1
3964
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
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.