473,837 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multi dimensional arrays as one dimension array

The subject might be misleading.
Regardless, is this code valid:

#include <stdio.h>

void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); }
int main(void) {
double array[2][1] = { { 3.14 }, { 42.6 } };
f((double *)array, sizeof array / sizeof **array);
return 0;
}

Assuming casting double [2][1] to double * is implementation defined
or undefined behavior, replace the cast with (void *).

Since arrays are not allowed to have padding bytes in between of
elements, it seems valid to me.
Aug 29 '08
152 9946
Harald van Dijk <tr*****@gmail. comwrites:
On Tue, 02 Sep 2008 06:26:22 +0000, Richard Heathfield wrote:
>Now show me a string literal that doesn't contain *any* strings.

#if 0
"Hello"
#endif
Note that that string literal is a preprocessing token, not a token;
by the time it would have been converted into a token (in phase 7),
it's been eliminated (in phase 4).

But yes, a string-literal is a preprocessing-token (C99 6.4, Lexical
elements). Nicely done.

But I would argue that the string literal "Hello" doesn't contain any
strings even if it isn't eliminated during preprocessing. The object
specified by the string literal certainly does contain a string, but
that object exists only during execution; the string literal itself
exists in a C source file, where no objects of any kind yet exist.

The anonymous static array object whose existence is specified by a
string literal is not the string literal itself, though we often refer
to it as a string literal as a convenient verbal shorthand.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 2 '08 #101
Barry Schwarz wrote:
On Tue, 02 Sep 2008 11:09:37 -0400, Eric Sosman <Er*********@su n.com>
wrote:
>Barry Schwarz wrote:
>>On Tue, 02 Sep 2008 10:14:45 -0400, Eric Sosman <Er*********@su n.com>
wrote:

Richard Heathfield wrote:
vi******@gmail. com said:
>On Sep 2, 8:57 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>>vipps...@ gmail.com said:
>>>On Sep 2, 8:23 am, Keith Thompson <ks...@mib.orgw rote:
<snip>
>
>>>>Perhaps , but in C string has a very specific meaning.
>>>And "string literal" has a completely different one.
>>No, it doesn't. It is a specification, that's all. A string literal /is/
>>a string, /and/ it's a literal. Hence, string literal.
>A string literal needs not to be a string, for example "hello\0wor ld"
>is not a string.
Right. It's several strings. (I count twelve.) I should have said "a string
literal contains at least one string".
>
Now show me a string literal that doesn't contain *any* strings.
char hello[5] = "Hello";
hello is not a literal.
Agreed. Neither is char, [, 5, ], =, ;, or the white space.
Everything else in the source line is a string literal.
>> The string literal used to initialize it, if
it does exist in the object module (it need not), will certainly
contain the terminating '\0'.
Chapter and verse?

6.4.5-5 seems to fit. 6.4.5-6 adds confirmation.
>> The code that initializes hello with
the literal will not copy the '\0'.
It certainly cannot "copy the '\0'," just as it cannot copy
a three-kilogram slab of luminiferous ether. As far as I can tell,
the Standard says the same thing about the existence of the former
and the latter, to wit, nothing at all.

So you think the initialization of an automatic array of char does not
involve any code to copy the initial value into the array?
No, but I think the initialization of this automatic array
(if it is in fact automatic) does not involve any code to copy
a '\0' as part of the initial value.

The Standard citations you and Richard Heathfield offer do
appear to make your case: A character string literal always ends
with a '\0', whether it is needed or not, whether it is observable
or not. That seems to me a defect in the Standard, but since it's
survived many editorial inspections I guess I'll just have to accept
it. (Margaret Fuller: "I accept the Universe." Thomas Carlyle:
"Gad! She'd better!")

Here's another string literal whose stringiness is of no
importance:

size_t twelve = 2 * sizeof "Hello";

I can think of no C program that could test whether the six
characters generated by this string literal do or do not actually
exist, and it seems a shame that the Standard promulgates an
untestable requirement. Maybe "as if" saves the day.

--
Er*********@sun .com
Sep 2 '08 #102
Antoninus Twink wrote:
On 1 Sep 2008 at 23:01, Keith Thompson wrote:
>pete <pf*****@mindsp ring.comwrites:
>>Keith Thompson wrote:
memcpy, for example, is
a function that doesn't have anything directly to do with strings.
Has it occurred to you that "library functions" are called that,
because they are part of the library,
and not because of what they do?
Of course.

So you're saying that memcpy is a string function because it's
declared in <string.h>? The same reasoning implies that size_t is a
string type, and NULL is a string macro.

FFS... it must be, like, 6 months or something since we last went
through this completely absurdly argument that excites such passion in
the breasts of the clc pedants club.
They love to discuss this stuff over and over.
Like void main, or casting the result of malloc.

There we go. Another exciting discussion in perspective

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Sep 2 '08 #103
jacob navia wrote:
Antoninus Twink wrote:
>[...]

They love to discuss this stuff over and over.
Like void main, or casting the result of malloc.

There we go. Another exciting discussion in perspective
Jacob, please stop feeding the troll.

--
Er*********@sun .com
Sep 2 '08 #104
Keith Thompson wrote:
So you're saying that memcpy is a string function because it's
declared in <string.h>? The same reasoning implies that size_t is a
string type, and NULL is a string macro.
No.
The same reasoning implies that size_t is a standard type
and that NULL is a standard macro.
All conforming C implementations define those in <stddef.h>.

Hosted implementations also define those
in various headers.

--
pete
Sep 2 '08 #105
pete <pf*****@mindsp ring.comwrites:
Keith Thompson wrote:
>So you're saying that memcpy is a string function because it's
declared in <string.h>? The same reasoning implies that size_t is a
string type, and NULL is a string macro.

No.
The same reasoning implies that size_t is a standard type
and that NULL is a standard macro.
All conforming C implementations define those in <stddef.h>.
Of course, but they're *also* defined in <string.h>.

So if any function declared in <string.his a "string function" (I'm
still not sure that's what you meant), wouldn't it follow that any
macro defined in <string.his a "string macro"?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 3 '08 #106
Keith Thompson wrote:
pete <pf*****@mindsp ring.comwrites:
>Keith Thompson wrote:
>>So you're saying that memcpy is a string function because it's
declared in <string.h>? The same reasoning implies that size_t is a
string type, and NULL is a string macro.
No.
The same reasoning implies that size_t is a standard type
and that NULL is a standard macro.
I should have said stddef type and stddef macro.
>All conforming C implementations define those in <stddef.h>.

Of course, but they're *also* defined in <string.h>.
Not every conforming implementation that defines those,
has <string.h>.
So if any function declared in <string.his a "string function" (I'm
still not sure that's what you meant),
It is what I meant.
wouldn't it follow that any
macro defined in <string.his a "string macro"?
No.
They would just be duplicates of stddef macros.
That's the way the standard describes it.
7.17 Common definitions <stddef.h>

[#1] The following types and macros are defined in the
standard header <stddef.h>. Some are also defined in other
headers, as noted in their respective subclauses.
and then for all the other headers, you have:

The types declared are size_t (described in 7.17);
The macros defined are NULL (described in 7.17);

--
pete
Sep 3 '08 #107
pete wrote:
Keith Thompson wrote:
>pete <pf*****@mindsp ring.comwrites:
>>Keith Thompson wrote:
So you're saying that memcpy is a string function because it's
declared in <string.h>? The same reasoning implies that size_t is a
string type, and NULL is a string macro.
No.
The same reasoning implies that size_t is a standard type
and that NULL is a standard macro.

I should have said stddef type and stddef macro.
>>All conforming C implementations define those in <stddef.h>.

Of course, but they're *also* defined in <string.h>.

Not every conforming implementation that defines those,
has <string.h>.
>So if any function declared in <string.his a "string function" (I'm
still not sure that's what you meant),

It is what I meant.
>wouldn't it follow that any
macro defined in <string.his a "string macro"?

No.
They would just be duplicates of stddef macros.
That's the way the standard describes it.
7.17 Common definitions <stddef.h>

[#1] The following types and macros are defined in the
standard header <stddef.h>. Some are also defined in other
headers, as noted in their respective subclauses.
and then for all the other headers, you have:

The types declared are size_t (described in 7.17);
The macros defined are NULL (described in 7.17);
But the standard doesn't refer to any types or macros
as string types or string macros,
or stddef types or stddef macros,
so I don't think I would either.

--
pete
Sep 3 '08 #108
On Tue, 2 Sep 2008 10:19:35 -0700 (PDT), ja*********@ver izon.net
wrote:
>Barry Schwarz wrote:
>On Tue, 02 Sep 2008 11:09:37 -0400, Eric Sosman <Er*********@su n.com>
wrote:
>Barry Schwarz wrote:
On Tue, 02 Sep 2008 10:14:45 -0400, Eric Sosman <Er*********@su n.com>
wrote:
...
>> char hello[5] = "Hello";

hello is not a literal.

Agreed. Neither is char, [, 5, ], =, ;, or the white space.
Everything else in the source line is a string literal.

The string literal used to initialize it, if
it does exist in the object module (it need not), will certainly
contain the terminating '\0'.

Chapter and verse?

6.4.5-5 seems to fit. 6.4.5-6 adds confirmation.

That applies only to code like

char *hello = "Hello";
There is nothing in 6.4.5-5 that mentions what the string literal is
used for. It simply states that the '\0' is appended automatically
every time.
>
When a string literal is used as an initializer for a char array,
6.7.8p14 is the relevant clause, and according to that clause a '\0'
comes into play only if the the array has enough room for it, or if
the array size is unknown. In this case, the array has a known length
It says the '\0' is used as part of the initialization process only if
there is room. It does not say anything about the contents of the
string literal itself so you must fall back to 6.4.5-5.
>of 5, which is not sufficient room for the terminating null; therefore
a terminating null is not even required to exist.
The entire literal is not required to exist. It would be perfectly
acceptable for the initialization code to consist of five independent
move instructions, each moving one character into the array.
>
It certainly cannot "copy the '\0'," just as it cannot copy
a three-kilogram slab of luminiferous ether. As far as I can tell,
the Standard says the same thing about the existence of the former
and the latter, to wit, nothing at all.

So you think the initialization of an automatic array of char does not
involve any code to copy the initial value into the array? How does
recursion work if code is not involved?

He didn't say that code was not involved, he said that a '\0' was not
involved. One obvious possibility is that the initialization is
achieved by copying 5 chars from some location which need not contain
a null character after the fifth char.
True, but then the string literal doesn't exist either which is why my
first response above included the clause "if it exists". 6.4.5-5 does
not allow the string literal to exist without the terminating '\0'.

--
Remove del for email
Sep 3 '08 #109
On Tue, 02 Sep 2008 13:31:54 -0400, Eric Sosman <Er*********@su n.com>
wrote:
>Barry Schwarz wrote:
>On Tue, 02 Sep 2008 11:09:37 -0400, Eric Sosman <Er*********@su n.com>
wrote:
>>Barry Schwarz wrote:
On Tue, 02 Sep 2008 10:14:45 -0400, Eric Sosman <Er*********@su n.com>
wrote:

Richard Heathfield wrote:
>vi******@gmail. com said:
>>On Sep 2, 8:57 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>>>vipps... @gmail.com said:
>>>>On Sep 2, 8:23 am, Keith Thompson <ks...@mib.orgw rote:
><snip>
>>
>>>>>Perhap s, but in C string has a very specific meaning.
>>>>And "string literal" has a completely different one.
>>>No, it doesn't. It is a specification, that's all. A string literal /is/
>>>a string, /and/ it's a literal. Hence, string literal.
>>A string literal needs not to be a string, for example "hello\0wor ld"
>>is not a string.
>Right. It's several strings. (I count twelve.) I should have said "a string
>literal contains at least one string".
>>
>Now show me a string literal that doesn't contain *any* strings.
char hello[5] = "Hello";
hello is not a literal.
Agreed. Neither is char, [, 5, ], =, ;, or the white space.
Everything else in the source line is a string literal.

The string literal used to initialize it, if
it does exist in the object module (it need not), will certainly
contain the terminating '\0'.
Chapter and verse?

6.4.5-5 seems to fit. 6.4.5-6 adds confirmation.
>>> The code that initializes hello with
the literal will not copy the '\0'.
It certainly cannot "copy the '\0'," just as it cannot copy
a three-kilogram slab of luminiferous ether. As far as I can tell,
the Standard says the same thing about the existence of the former
and the latter, to wit, nothing at all.

So you think the initialization of an automatic array of char does not
involve any code to copy the initial value into the array?

No, but I think the initialization of this automatic array
(if it is in fact automatic) does not involve any code to copy
a '\0' as part of the initial value.
I agree.
>
The Standard citations you and Richard Heathfield offer do
appear to make your case: A character string literal always ends
with a '\0', whether it is needed or not, whether it is observable
or not. That seems to me a defect in the Standard, but since it's
survived many editorial inspections I guess I'll just have to accept
it. (Margaret Fuller: "I accept the Universe." Thomas Carlyle:
"Gad! She'd better!")

Here's another string literal whose stringiness is of no
importance:

size_t twelve = 2 * sizeof "Hello";
Since sizeof "Hello" is a compile time constant, it would seem to a
QOI issue if the literal even existed in the program. 6.4.5-5 does
not say every string literal in the source results in a string literal
in the object. It only says that if it does it must have the
terminating '\0'.
>
I can think of no C program that could test whether the six
characters generated by this string literal do or do not actually
exist, and it seems a shame that the Standard promulgates an
untestable requirement. Maybe "as if" saves the day.
There seems to be agreement in this group that the optimizer can do
away with unreferenced variables and unreachable code. I see no
reason why the same should not apply to unused constants.

--
Remove del for email
Sep 3 '08 #110

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

Similar topics

0
1689
by: James | last post by:
I see that variations on this question have appeared before but I'm still completely stumped. I'm developing an application with a fairly robust graphics component for 3D rendering. I've written all the 3D rendering scripts, but what's really hanging me up is how to I pass information from an one-dimensional arraylist to a two-dimensional array. Here's what I'm trying to do: 1) My program generates an arraylist called "mpoints" of 2D...
6
9524
by: Gregory L. Hansen | last post by:
I'm sure I saw this mentioned somewhere, but I can't find it. How can I dynamically allocate a multi-dimensional array in C++? My next question, if this gets figured out, is if I should use the delete command to remove it, or would that be delete ? -- "What are the possibilities of small but movable machines? They may or may not be useful, but they surely would be fun to make."
4
2254
by: Richard Hayden | last post by:
Hi, Why does gcc (3.3.2) give me a 'initialization from incompatible pointer type' warning when compiling: int main(int argc, char** argv) { int testa; int** testp = testa; }
8
2254
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along that a declaration like: int intArray means that there is an array of pointers to int with a size of 5, each of whose elements is an array of int with a size of 3. This definition seemed intuitive to me since a declaration like
11
4479
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
1
6305
by: Fayez Al-Naddaf | last post by:
I got this message when I tried to browse my web service "Multi-dimensional arrays are not supported. Use a jagged array instead" Can someone told me why?
0
1366
by: volx | last post by:
Hello all: What is the proper way to implement in MC++ a web service which accepts a multi dimensional array as a parameter to one of its methods? This does compile: double pass2DArray(Double array __gc);
5
1634
by: David T. Ashley | last post by:
Hi, Aside from the obvious recursive syntax for initialization, $x = array( ... array( ...)), what is the best way to deal with multi-dimensional arrays in PHP? It seems there is no syntax like $x???
3
2129
by: ben.r.wood | last post by:
I am not entirely sure, but after scanning the web believe I need to use multi-dimensional arrays for my problem - although I have not seen any examples of what I am trying to achieve. I have a form with one field, for which a user will submit a lump of delimited data. The data will always be in the same format, ie. 5 fields per record, each field has a delimeter and a new row starts a new record. for example:
0
9837
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
9682
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
10563
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
10621
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
10267
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
9392
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...
0
5668
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
5845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4038
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.