473,500 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

declaration error

Could anyone correct the error in my logic here?:

#include <stdio.h>
#include <stdlib.h>
main ()
{
const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
\n A B C"};
const char message2[] = {"\nCurved portion of graph -- R.A.C.\
\n A D E"};
const char message3[] = {"\nCurved portion of graph -- D.G.A.C.\
\nA\tB\tC"};
const char message4[] = {"\nCurved portion of graph R.A.C.\
\nA\tD\tE"};
char ( * const msgptr12 [] ) [] = { message1, message2 };
char ( * const msgptr34 [] ) [] = { message3, message 4};
/* lint gives a type mismatch error for the previous two lines. */
char stringbuf [85];

...

printf ( "%s", msgptr12 [i] );
numbchar = sprintf (stringbuf, "%s", msgptr34 [i] );
fwrite (stringbuf, numbchar, 1, ofptr);
Any thoughts or suggestions will be appreciated. L e e _ S h a c k e
l f o r d @ d o t . c a . g o v
Nov 14 '05 #1
7 2155
Jacob Schmidt <Ja***********@aol.com> wrote in
news:26********************************@4ax.com:
Could anyone correct the error in my logic here?:

#include <stdio.h>
#include <stdlib.h>
main ()
{
const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
\n A B C"};
const char message2[] = {"\nCurved portion of graph -- R.A.C.\
\n A D E"};
const char message3[] = {"\nCurved portion of graph -- D.G.A.C.\
\nA\tB\tC"};
const char message4[] = {"\nCurved portion of graph R.A.C.\
\nA\tD\tE"};
char ( * const msgptr12 [] ) [] = { message1, message2 };


message1, 2, 3, & 4 are not compile time constants. You can't do this in
C90. Also, main returns 'int' so why not be explicit?

--
- Mark ->
--
Nov 14 '05 #2
Jacob Schmidt <Ja***********@aol.com> writes:
Could anyone correct the error in my logic here?:

#include <stdio.h>
#include <stdlib.h>
main ()
Better: int main (void)
{
const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
\n A B C"};
const char message2[] = {"\nCurved portion of graph -- R.A.C.\
\n A D E"};
const char message3[] = {"\nCurved portion of graph -- D.G.A.C.\
\nA\tB\tC"};
const char message4[] = {"\nCurved portion of graph R.A.C.\
\nA\tD\tE"};
char ( * const msgptr12 [] ) [] = { message1, message2 };
char ( * const msgptr34 [] ) [] = { message3, message 4};
/* lint gives a type mismatch error for the previous two lines. */


This declares `msgptr12' and `msgptr34' as arrays of const pointers to
arrays of char. The elements therefore have to have type `const pointer
to array of char'; you cannot initialize them with something that has
type `pointer to const char'. Try this instead:

const char * msgptr12 [] = { message1, message2 };
const char * msgptr34 [] = { message3, message4 };

Note, however, that `message1', `message2', `message3', and `message4'
are not constant expressions, so you cannot use them to initialize
arrays (unless you have a C99 compiler, but I know you don't, since
`main ()' is invalid in C99). There are two things you can do: Either
declare `message1' etc. with static storage duration (i.e. `static const
char message1 [] = ...'), or assign instead of initializing:

const char * msgptr12 [2];
const char * msgptr34 [2];

msgptr12 [0] = message1;
msgptr12 [1] = message2;
msgptr34 [0] = message3;
msgptr34 [1] = message4;

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #3
Jacob Schmidt wrote:
Could anyone correct the error in my logic here?:

#include <stdio.h>
#include <stdlib.h>
main ()
{
const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
\n A B C"};
const char message2[] = {"\nCurved portion of graph -- R.A.C.\
\n A D E"};
const char message3[] = {"\nCurved portion of graph -- D.G.A.C.\
\nA\tB\tC"};
const char message4[] = {"\nCurved portion of graph R.A.C.\
\nA\tD\tE"};
char ( * const msgptr12 [] ) [] = { message1, message2 };
char ( * const msgptr34 [] ) [] = { message3, message 4};
/* lint gives a type mismatch error for the previous two lines. */
char stringbuf [85];

...

printf ( "%s", msgptr12 [i] );
numbchar = sprintf (stringbuf, "%s", msgptr34 [i] );
fwrite (stringbuf, numbchar, 1, ofptr);

#include <stdio.h>

int main(void)
{
const char message1[] = "Curved portion of graph -- D.G.A.C.\n"
" A B C\n";
const char message2[] = "Curved portion of graph -- R.A.C.\n"
" A D E\n";
const char message3[] = "Curved portion of graph -- D.G.A.C.\n"
"A\tB\tC\n";
const char message4[] = "Curved portion of graph R.A.C.\n"
"A\tD\tE\n";
const char *const msgptr12[] = { message1, message2 };
const char *const msgptr34[] = { message3, message4 };
char Strbuf[85];
size_t i = 0, numbchar;
printf("%s", msgptr12[i]);
numbchar = sprintf(Strbuf, "%s\n", msgptr34[i]);
fwrite(Strbuf, numbchar, 1, stdout);
return 0;
}

Nov 14 '05 #4
Jacob Schmidt <Ja***********@aol.com> wrote:

Could anyone correct the error in my logic here?:

#include <stdio.h>
#include <stdlib.h>
main ()
{
const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
\n A B C"};
I think newlines in string literals are considered bad form these days
const char message2[] = {"\nCurved portion of graph -- R.A.C.\
\n A D E"};
char ( * const msgptr12 [] ) [] = { message1, message2 }; /* lint gives a type mismatch error for the previous two lines. */
'msgptr12' is an array of const pointers to arrays (of unspecified size)
of char.
'message1' is the name of an array of const char. This is not a
pointer to (non-const) char. You need the '&' sign to take its
address, and you need to make msgptr12 point to arrays of const char:

const char (* const msgptr12[]) [] = { &message1, &message2 };

This compiles OK now but it is not terribly useful.
msgptr12[0] has type 'pointer to array (of unknown size) of const char'.
This is an incomplete type, so you cannot actually dereference that
pointer, so you can't access the chars in it safely.
The expression *msgptr12[0] would be a syntax error.
printf ( "%s", msgptr12 [i] );


Undefined behaviour - %s expects a pointer to char, but you gave it a
pointer to array. You were (un)lucky that you chose a variadic function,
if you tried:
puts(msgptr12[i])
you would get a compiler warning.

If you want msgptr12's members to point to complete arrays, then the
arrays all have to be the same size and you have to specify that, eg:

const char (* const msgptr12[]) [40] = { &message1, &message2 };
printf("%s", *msgptr12[1]);

Or, as others have suggested, you could simply use an array of
pointers to char (but this was a useful exercise in understanding
pointers to arrays).
Nov 14 '05 #5
ol*****@inspire.net.nz (Old Wolf) writes:
Jacob Schmidt <Ja***********@aol.com> wrote:

main ()
{
const char message1[] = {"\nCurved portion of graph -- D.G.A.C.\
\n A B C"};


I think newlines in string literals are considered bad form these days


Unescaped new-lines are not allowed in string literals, but this
new-line is immediately preceded by a \ that splices the two
lines together, so it's okay. (Another example in my .sig below,
simply by accident.)
--
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;}
Nov 14 '05 #6
On 2 Jun 2004 14:32:10 -0700, ol*****@inspire.net.nz (Old Wolf) wrote:
Jacob Schmidt <Ja***********@aol.com> wrote:
const char message1[] = <snip> [message 2 similar]
char ( * const msgptr12 [] ) [] = { message1, message2 };

/* lint gives a type mismatch error for the previous two lines. */


'msgptr12' is an array of const pointers to arrays (of unspecified size)
of char.
'message1' is the name of an array of const char. This is not a
pointer to (non-const) char. You need the '&' sign to take its
address, and you need to make msgptr12 point to arrays of const char:

const char (* const msgptr12[]) [] = { &message1, &message2 };

Agree so far.
This compiles OK now but it is not terribly useful.
msgptr12[0] has type 'pointer to array (of unknown size) of const char'.
This is an incomplete type, so you cannot actually dereference that
pointer, so you can't access the chars in it safely.
To be clear, 'array of unknown size (of anything)' is the incomplete
type; 'pointer to X' is complete even if X is incomplete. But I think
you _can_ dereference it: this is a corner case, where it is allowed
to have a pointer to incomplete and dereference it to form an lvalue
of incomplete type; you explicitly musn't fetch (convert to rvalue) or
store (assign) such, but AFAICS if as here it is an array you can let
it decay to pointer to element and legally use that, within the bound
of the array object it points "to" (rather, to the base element of).

What you can't have is an actual (defined) object of incomplete type,
and in C99 you explicitly can't have a declaration for 'array of
incomplete' (which this example isn't, mind you) whereas in C89 _as a
function parameter_ an implementation _might_ let you declare array of
incomplete and "rewrite" it to pointer to incomplete which is OK.
The expression *msgptr12[0] would be a syntax error.
The standard doesn't define 'syntax error' but I think it is usually
taken to mean 'violation of any syntax rule', which this is not;
if anything it would be a constraint violation -- also a required
diagnostic, but a separate category. And I say not even that.
printf ( "%s", msgptr12 [i] );


Undefined behaviour - %s expects a pointer to char, but you gave it a
pointer to array. You were (un)lucky that you chose a variadic function,
if you tried:
puts(msgptr12[i])
you would get a compiler warning.

Agree here. In fact gcc will even unrequiredly warn on the printf.
If you want msgptr12's members to point to complete arrays, then the
arrays all have to be the same size and you have to specify that, eg:

const char (* const msgptr12[]) [40] = { &message1, &message2 };
printf("%s", *msgptr12[1]);

Or, as others have suggested, you could simply use an array of
pointers to char (but this was a useful exercise in understanding
pointers to arrays).


Yes.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #7
Dave Thompson <da*************@worldnet.att.net> wrote:
ol*****@inspire.net.nz (Old Wolf) wrote:
Jacob Schmidt <Ja***********@aol.com> wrote:

const char message1[] = <snip> [message 2 similar]

const char (* const msgptr12[]) [] = { &message1, &message2 };

msgptr12[0] has type 'pointer to array (of unknown size) of const char'.
This is an incomplete type, so you cannot actually dereference that
pointer, so you can't access the chars in it safely.


To be clear, 'array of unknown size (of anything)' is the incomplete
type; 'pointer to X' is complete even if X is incomplete. But I think
you _can_ dereference it: this is a corner case, where it is allowed
to have a pointer to incomplete and dereference it to form an lvalue
of incomplete type; you explicitly musn't fetch (convert to rvalue) or
store (assign) such, but AFAICS if as here it is an array you can let
it decay to pointer to element and legally use that, within the bound
of the array object it points "to" (rather, to the base element of).


Thanks for the clarifications.
Nov 14 '05 #8

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

Similar topics

83
6418
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
3
20574
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward...
2
3322
by: Legendary Pansy | last post by:
Hello, I'm trying to accomplish the impossible by trying to do something equivalent of this example found here http://www.c-sharpcorner.com/Code/2003/Dec/DialogTutorial.as Starting with "Listing...
3
1760
by: Wild Wind | last post by:
Hello, I made a post relating to this issue a while back, but I haven't received any answer, so here I am again. I am writing a mixed C++ dll which uses the following declaration: typedef...
1
5691
by: HappyHippy | last post by:
Hi, I have a question about forward declaration of classes. First of all, a simple working example, which compiles and works with no problems. ///file class_a.h:/ #ifndef __CLASS_A_H__...
6
8600
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
14
13024
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
4
2024
by: subramanian100in | last post by:
Consider the following: Class Test { public: Test(Test_int arg) : val(arg) { } typedef int Test_int; private:
6
3147
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
6
14706
by: samsneelam | last post by:
Hi.. This is samuel, while doing a program, i encountered this problem.. Let me give you clarity regarding my prob.. I am having two files .. one is mpcplib.h it contains the follwing...
0
7018
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...
0
7182
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,...
1
6906
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
7397
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...
1
4923
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...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3110
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...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.