f



Solution to the invalid "int[][]" -> "const int[][]" conversion

First, here's the code I'm talking about:

/* -------------------------- */
typedef int TYPE[8][8];
extern void g(const TYPE);
extern TYPE data;
void f()
{
        g(data);
}
/* -------------------------- */

The above code gives the following compiler warning on gcc-3.4.3:
warning: passing arg 1 of `g' from incompatible pointer type

I do understand that it's generally unsafe to convert "Foo **" to
"const Foo **", as it's pointed out here:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.15
(Yes, I know, that's a C++ FAQ and we're talking about C here, but I think
this const issue also applies to C.  Correct me if I'm wrong.)

I guess that the problem here is very similar to the "const Foo **" problem.
The solution for pointers is to convert "Foo **" to "const Foo * const *",
which is safe.  But how do I do that when it's about arrays?  Especially, how
do I do it when the array isn't so explicit, but typedef'ed to TYPE?

What I actually want should be clear: g()'s prototyp shall promise that g()
won't change any element of the array it's receiving.

Thanks a lot,
jlh
-- 
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
0
jlh
1/6/2005 11:40:28 PM
comp.lang.c.moderated 1019 articles. 0 followers. Post Follow

2 Replies
268 Views

Similar Articles

[PageSpeed] 37

On Thu, 6 Jan 2005 23:40:28 -0000, jlh@gmx.ch (jlh) wrote in
comp.lang.c.moderated:

> First, here's the code I'm talking about:
> 
> /* -------------------------- */
> typedef int TYPE[8][8];
> extern void g(const TYPE);
> extern TYPE data;
> void f()
> {
>         g(data);
> }
> /* -------------------------- */
> 
> The above code gives the following compiler warning on gcc-3.4.3:
> warning: passing arg 1 of `g' from incompatible pointer type
> 
> I do understand that it's generally unsafe to convert "Foo **" to
> "const Foo **", as it's pointed out here:
> http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.15
> (Yes, I know, that's a C++ FAQ and we're talking about C here, but I think
> this const issue also applies to C.  Correct me if I'm wrong.)
> 
> I guess that the problem here is very similar to the "const Foo **" problem.
> The solution for pointers is to convert "Foo **" to "const Foo * const *",
> which is safe.  But how do I do that when it's about arrays?  Especially, how
> do I do it when the array isn't so explicit, but typedef'ed to TYPE?

You can't do it with a typedef involved.  A cv(r) qualifier can't
"penetrate" a typedef, and always applies to the identifier, no matter
where it is placed.

    typedef char *CHAR_PTR;

    const CHAR_PTR c1;

....is not equivalent to the simple text substitution that a macro
would generate:

   const char * c1;

Instead it is equivalent to:

   char *const c1;

....and there is no way to write it to make it any different.

> What I actually want should be clear: g()'s prototyp shall promise that g()
> won't change any element of the array it's receiving.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
-- 
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
0
Jack
1/7/2005 4:41:32 AM
Thanks a lot for this explanation.  Though, I don't understand the error message
then, neither why the conversion is a problem.  If the 'const' in "const TYPE"
applies to the identifier only, then why does the caller of g() care about that
const?  Taking your example with "char *": The conversion from "char *" to
"char * const" isn't invalid or unsafe.

Further, I noticed that the exact same code doesn't generate a warning if passed
to the C++ compiler instead.  Is the meaning of my code above different when
read as C and as C++?

Thanks,
jlh
-- 
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
0
jlh
1/10/2005 12:58:36 AM
Reply:

Similar Artilces:

"static const int" versus "const int"
Hello, I would like to ask what the difference is between "static const int" versus "const int". I mean of course the former goes in the static data segment of code whereas the latter is pushed on the stack, but since a const does not change it might as well be declared static, but to what advantage. What I am asking is how to decide whether to use "static const int" versus "const int". Thanks, John Goche On 11/11/2011 11:39 AM, John Goche wrote: > I would like to ask what the difference is between > "static const int" versus "...

""""""""""""""""""""""ADD ME""""""""""""""""""""
Hi , Hope you are doing great. Please let me take this opportunity to introduce myself, Iam Karthik working with BhanInfo Inc, a NY based company. We have consultants on our bench on various technologies, my request is to add me to your distribution list and kindly do send me the requirements. i have the below list available 1. Mainframe 2. Java 3.. Financial Analyst 4. Data Architect If there is any vendor ship agreement which has to be signed then I would like to take an opportunity to represent my company and expect your cooperation... We look forward to build a ve...

Conversion from "const int*" to "int*" may change the pointer value?
I cannot find anywhere in the spec where it requires that the following program yield the same value into "p" and "p1". int x; int *p = &x; int const *q = p; int *p1 = (int*)q; This seems to hit the paragraph of 6.3.2.3 saying "A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type.", which makes no guarantee of value equality of the pointers, which seems very weird. On 24.07.2011 16:43, Johannes Schaub wrote: > I cannot find anywhere in the spec where it requires tha...

When to use "INT" or "int"?
In windef.h there is a type definition typedef int INT; Windows itself uses both types. For example, in the declaration of SetSysColors: BOOL WINAPI SetSysColors( int cElements, // number of elements to change CONST INT *lpaElements, // address of array of elements CONST COLORREF *lpaRgbValues // address of array of RGB values ); Then I have a question: What is the consideration beneath the choice of using either "int" or "INT"? In this case, why cElements should better be declared as "int&...

"""""""""ADD ME""""""""""
Hi , Hope you are doing great. Please let me take this opportunity to introduce myself, Iam Karthik working with BhanInfoi Inc, a NY based company. We have consultants on our bench on various technologies, my request is to add me to your distribution list and kindly do send me the requirements. i have the below list available 1. Mainframe 2. Java 3.. Financial Analyst 4. Data Architect If there is any vendor ship agreement which has to be signed then I would like to take an opportunity to represent my company and expect your cooperation... ...

Urgent Requirement in """""""""""""NEW YORK""""""""""""""""
Hello Partners, Please find the requirement below. Please send the updated resume along with rate and contact no. REQ#1: Title : Java Developer ( Rating Project) Duration : 6 months Rate : open Location : NY strong java, WebLogic 9.2, Web Services, Oracle REQ#2: Title : Java Developer Duration : 4 months Rate : open Location : NY Strong java, SQL REQ#3: Title : VB.Net Consultant Location : NY Duration : 4 months Rate : open Primarily looking at someone who has Excel, VB.net a...

about "++" and "--"
why this program snippet display "8,7,7,8,-7,-8" the program is: main() { int i=8; printf("%d\n%d\n%d\n%d\n%d\n%d\n",++i,--i,i++,i--,-i++,-i--); } > why this program snippet display "8,7,7,8,-7,-8" Ask your compiler-vendor because this result is IMHO implementation-defined. Check this out: http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15 http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.16 Regards, Irina Marudina fxc123@gmail.com wrote: > why this program snippet display "8,7,7,8,-7,-8&q...

"/a" is not "/a" ?
Hi everybody, while testing a module today I stumbled on something that I can work around but I don't quite understand. >>> a = "a" >>> b = "a" >>> a == b True >>> a is b True >>> c = "/a" >>> d = "/a" >>> c == d True # all good so far >>> c is d False # eeeeek! Why c and d point to two different objects with an identical string content rather than the same object? Manu Emanuele D'Arrigo wrote: >>>> c = "/a" >>>&...

why "::", not "."
Why does the method of modules use a dot, and the constants a double colon? e.g. Math::PI and Math.cos -- Posted via http://www.ruby-forum.com/. On Oct 26, 2010, at 01:48 , Oleg Igor wrote: > Why does the method of modules use a dot, and the constants a double > colon? > e.g. > Math::PI and Math.cos For the same reason why inner-classes/modules use double colon, because = they're constants and that's how you look up via constant namespace. Math::PI and ActiveRecord::Base are the same type of lookup... it is = just that Base is a module and PI is a float....

"out" and "in out"
Hi i found the following explaination: In Ada, "in" parameters are similar to C++ const parameters. They are effectively read-only within the scope of the called subprogram. Ada "in out" parameters have a reliable initial value (that passed in from the calling subprogram) and may be modified within the scope of the called procedure. Ada "out" parameters have no reliable initial value, but are expected to be assigned a value within the called procedure. What does "have no reliable initial value" mean when considering the "out" parameter? By c...

"or" and "and"
Hi, I'm just getting to discover ruby, but I find it very nice programming language. I just still don't understand how the "or" and "and" in ruby... I was playing with ruby and for example made a def to print Stem and Leaf plot (for those who didn't have a statistics course or slept on it, e.g. http://cnx.org/content/m10157/latest/) Here is the Beta version of it: class Array def n ; self.size ; end def stem_and_leaf(st = 1) # if st != (2 or 5 or 10) then ; st = 1 ; end k = Hash.new(0) self.each {|x| k[x.to_f] += 1 } k = k.sort{|a, b| a[0].to_f <=&g...

"If then; if then;" and "If then; if;"
I have a raw data set which is a hierarchical file: H 321 s. main st P Mary E 21 F P william m 23 M P Susan K 3 F H 324 S. Main St I use the folowing code to read the data to creat one observation per detail(P) record including hearder record(H): data test; infile 'C:\Documents and Settings\retain.txt'; retain Address; input type $1. @; if type='H' then input @3 Address $12.; if type='P' then input @3 Name $10. @13 Age 3. @16 Gender $1.; run; but the output is not what I want: 1 321 s. main H 2 321 s. main P Mary E 21 F 3 321 s...

"my" and "our"
Hi, while testing a program, I erroneously declared the same variable twice within a block, the first time with "my", the second time with "our": { my $fz = 'VTX_Link'; .... ( around 200 lines of code, all in the same block) our $fz = 'VTX_Linkset'; ... } So the initial contents of the $fz declared with "my" is lost, because "our" creates a lexical alias for the global $fz, thus overwriting the previous "my" declaration. It was my error, no question. But I wonder why Perl doesn't mention this - even with "use s...

Urgent Requirement for """""""""""""""INFORMATICA DEVELOPER"""""""""""""
Hello Partners, How are you ? Please find the requirements below. Title: Database/ETL Developer Duration: 6 months Location: NY Exp: 7+ Locals preferred Database/ETL requirements (Mandatory) Candidate must have worked with financial instruments, preferably Mutual Funds but, Equities are also ok. PL/SQL - packages, Stored procs, Functions, Aggregate functions, Pipelined Functions Informatica 8.6 - especially complex mappings, complex maplets, complex workflows, transformations Oracle 10g/11g Unix/Linux shell scripting ...

Urgent need """""""""""INFORMATICA DEVELOPER"""""""""""""
Hello Partners, How are you ? Please find the requirements below. Title: Database/ETL Developer Duration: 6 months Location: NY Exp: 7+ Locals preferred Database/ETL requirements (Mandatory) Candidate must have worked with financial instruments, preferably Mutual Funds but, Equities are also ok. PL/SQL - packages, Stored procs, Functions, Aggregate functions, Pipelined Functions Informatica 8.6 - especially complex mappings, complex maplets, complex workflows, transformations Oracle 10g/11g Unix/Linux shell scripting Database/ETL requirements (Optional) ...

Web resources about - Solution to the invalid "int[][]" -> "const int[][]" conversion - comp.lang.c.moderated

Resources last updated: 2/9/2016 10:27:09 PM