473,883 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parse "Card * const wDeck"

I'm trying to learn C on my own and, apparently, my brain went on
vacation somewhere. I just can't figure out how to parse the following
function call:

"void fillDeck( Card * const wDeck, const char * wFace[], const char *
wSuit[])"

Card is aliased before that call with:

"typedef struct card Card"

the card structure is define before that with:

"struct card {
const char *face;
const char *suit;
};"

and the paramter passed via wDeck is defined in main as:

"Card deck[ 52 ]" /* an array of 52 card structures */

Looking around the web, this seems to be a fairly standard example
program. In that call to fillDeck, the first parameter seems to parse
as sending a pointer to an instance of type Card (in this case, the
address to the first Card in the 52 element array of Cards) but making
sure the receiving function can't modify it (with the "const"
modifier). I've got to be reading that wrong, because the sole purpose
of fillDeck is to modify wDeck by filling it using a for loop:

"for ( i = 0; i <= 51; i++ ) {
wDeck[ i ].face = wFace[i % 13 ];
...."

Can anyone shed some light on how I'm misreading that function call?
Probably related to it: can anyone tell me why the dereferencing
operator * is separated from its variable? IOW, why

"void fillDeck( Card * const wDeck,..."

instead of

"void fillDeck( Card *const wDeck,..." (*const instead of * const)

or, heck,

"void fillDeck( Card const *wDeck,..." (const *wDeck instead of * const
wDeck)?

Nov 14 '05 #1
18 2247

"DaveLessna u" <dl******@mail. com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
I'm trying to learn C on my own and, apparently, my brain went on
vacation somewhere. I just can't figure out how to parse the following
function call:

"void fillDeck( Card * const wDeck, const char * wFace[], const char *
wSuit[])"
That's not a function call, but a declaration (which is
missing the terminating semicolon. It declares:

'fillDeck' is a function which does not return a value, and
takes three arguments:

1. A constant pointer ('constant' meaning that its value is not
allowed to be changed) to an object of type 'wDeck'.

2. A pointer to a constant object of type 'char'.

3. A pointer to a constant object of type 'char'.


Card is aliased before that call with:

"typedef struct card Card"

the card structure is define before that with:

"struct card {
const char *face;
const char *suit;
};"

and the paramter passed via wDeck is defined in main as:

"Card deck[ 52 ]" /* an array of 52 card structures */
The name of an array, used in a value context (one of which
is passing it as a function argument), automatically converts
to a pointer to its first element. So the call:

fillDeck(deck, /* other args */);

causes the address of 'deck[0]' (the first array element)
to be passed as the first argument.

Looking around the web, this seems to be a fairly standard example
program. In that call to fillDeck, the first parameter seems to parse
as sending a pointer to an instance of type Card (in this case, the
address to the first Card in the 52 element array of Cards) but making
sure the receiving function can't modify it (with the "const"
modifier).
It must not modify the pointer value, modifying what
it points to is fine.
I've got to be reading that wrong,
Perhaps.
because the sole purpose
of fillDeck is to modify wDeck by filling it using a for loop:
Yes, the elements of the array are modified, but the pointer
containing its address is not.

"for ( i = 0; i <= 51; i++ ) {
wDeck[ i ].face = wFace[i % 13 ];
..."

Can anyone shed some light on how I'm misreading that function call?
See above.
Probably related to it: can anyone tell me why the dereferencing
operator * is separated from its variable? IOW, why

"void fillDeck( Card * const wDeck,..."

instead of

"void fillDeck( Card *const wDeck,..." (*const instead of * const)
That's a syntactical issue. The whitespace is not germane. Equally
valid, and having the same meaning:

Card*const wDeck

Card * const wDeck
Card
*
const
wDeck

/* etc */

or, heck,

"void fillDeck( Card const *wDeck,..." (const *wDeck instead of * const
wDeck)?


Now you've changed the syntax. The placement of the asterisk
with respect to the const modifier affects the meaning.

Using type 'int' for simplicity's sake:

int *p; /* non-const pointer to non-constant int */
const int *p; /* non-const pointer to constant int */
int const *p; /* non-const pointer to constant int */
int * const p; /* constant pointer to non-constant int */
const int * const p; /* constant pointer to constant int */
int const * const p; /* constant pointer to constant int */

Note that in the case where 'const' appears to the left of
the asterisk, placing it before or after the 'int' does
not change the meaning.
-Mike
Nov 14 '05 #2
Thanks. Amongs my key misunderstandin gs was "Yes, the elements of the
array are modified, but the pointer containing its address is not." I
was mixing up the addresses and the values. Also, your table at the
end showing the differences between constant/non-constant pointers to
constant/non-constant values helped tremendously.

Thanks again.

Nov 14 '05 #3
Groovy hepcat Mike Wahler was jivin' on Mon, 10 Jan 2005 23:30:23 GMT
in comp.lang.c.
Re: Parse "Card * const wDeck"'s a cool scene! Dig it!
"DaveLessnau " <dl******@mail. com> wrote in message
news:11******* *************** @c13g2000cwb.go oglegroups.com. ..

"void fillDeck( Card * const wDeck, const char * wFace[], const char *
wSuit[])"


'fillDeck' is a function which does not return a value, and
takes three arguments:

1. A constant pointer ('constant' meaning that its value is not
allowed to be changed) to an object of type 'wDeck'.


That's a const pointer to type Card. wDeck is the name of the
parameter.

[Snip.]
Probably related to it: can anyone tell me why the dereferencing
operator * is separated from its variable? IOW, why

"void fillDeck( Card * const wDeck,..."

instead of

"void fillDeck( Card *const wDeck,..." (*const instead of * const)


That's a syntactical issue. The whitespace is not germane. Equally
valid, and having the same meaning:


It's a style issue, rather than a syntactical one. The syntax allows
either. As you say, the white space is not germane.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #4
DaveLessnau wrote:
I'm trying to learn C on my own
[but] apparently, my brain went on vacation somewhere.
I just can't figure out how to parse the following function call:

void fillDeck(Card*c onst wDeck,
const char* wFace[], const char* wSuit[]);

Card is aliased before that call with:

typedef struct Card Card;

the card structure is define before that with:

struct Card {
const char *face;
const char *suit;
};

and the paramter passed via wDeck is defined in main as:

Card deck[ 52 ]; // an array of 52 card structures Looking around the web,
this seems to be a fairly standard example program.
Unfortunately, it is a very poor design as well as a very common design.
In that call to fillDeck, the first parameter seems to parse
as sending a pointer to an instance of type Card (in this case,
the address to the first Card in the 52 element array of Cards)
but making sure the receiving function can't modify it
(with the "const" modifier). I've got to be reading that wrong,
because the sole purpose of fillDeck
is to modify wDeck by filling it using a for loop: for (size_t i = 0; i < 52; ++i) {
wDeck[i].face = wFace[i%13];
// ...

Can anyone shed some light on how I'm misreading that function call?
Probably related to it: can anyone tell me why the dereferencing
operator * is separated from its variable? IOW, why

void fillDeck(Card*c onst wDeck, ...

instead of

void fillDeck(Card *const wDeck, ... // (*const instead of * const)

or, heck,

void fillDeck(Card const *wDeck, ... // (const *wDeck instead of * const
wDeck)?


A better design would be

typedef struct Deck {
Card deck[52];
} Deck;

Deck Deck_create(con st char* wFace[], const char*wSuit[]) {
Deck deck;
for (size_t i = 0; i < 52; ++i) {
deck.deck[i].face = wFace[i%13];
// ...
}
return deck;
}

Which you could call like this:

Deck wDeck = Deck_create(wFa ce, wSuit);

instead of like this:

Deck wDeck;
fillDeck(wDeck. deck, wFace, wSuit);
Nov 14 '05 #5

"Peter "Shaggy" Haywood" <ph******@alpha link.com.au.NO. SPAM> wrote in
message news:41******** ******@news.alp halink.com.au.. .
Groovy hepcat Mike Wahler was jivin' on Mon, 10 Jan 2005 23:30:23 GMT
in comp.lang.c.
Re: Parse "Card * const wDeck"'s a cool scene! Dig it!
"DaveLessnau " <dl******@mail. com> wrote in message
news:11******* *************** @c13g2000cwb.go oglegroups.com. ..

"void fillDeck( Card * const wDeck, const char * wFace[], const char *
wSuit[])"
'fillDeck' is a function which does not return a value, and
takes three arguments:

1. A constant pointer ('constant' meaning that its value is not
allowed to be changed) to an object of type 'wDeck'.


That's a const pointer to type Card. wDeck is the name of the
parameter.


Oops. :-)

[Snip.]
Probably related to it: can anyone tell me why the dereferencing
operator * is separated from its variable? IOW, why

"void fillDeck( Card * const wDeck,..."

instead of

"void fillDeck( Card *const wDeck,..." (*const instead of * const)
That's a syntactical issue. The whitespace is not germane. Equally
valid, and having the same meaning:


It's a style issue, rather than a syntactical one.


Style of syntax, OK? :-)
The syntax allows
either. As you say, the white space is not germane.

-Mike
Nov 14 '05 #6
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:cr******** **@nntp1.jpl.na sa.gov...
A better design would be

typedef struct Deck {
Card deck[52];
} Deck;


IMO even better would be to dump the array and use a container.

-Mike
Nov 14 '05 #7
"Mike Wahler" <mk******@mkwah ler.net> writes:
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:cr******** **@nntp1.jpl.na sa.gov...
A better design would be

typedef struct Deck {
Card deck[52];
} Deck;


IMO even better would be to dump the array and use a container.


What do you mean by "container" ? Are you thinking of C++?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
Keith Thompson wrote:
Mike Wahler writes:
E. Robert Tisdale wrote:
A better design would be

typedef struct Deck {
Card deck[52];
} Deck;
IMO even better would be to dump the array and use a container.


What do you mean by "container" ?


A container is an object that contains objects of some type.
An array is an example of a container.
A list is a container.
A queue is a container.

I believe that Mike is correct.
A container for a Deck of Card's would be even better
but I didn't want to confuse the issue
which I believe was the return value.
Are you thinking of C++?

Nov 14 '05 #9
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > writes:
Keith Thompson wrote:
Mike Wahler writes:
E. Robert Tisdale wrote:

A better design would be

typedef struct Deck {
Card deck[52];
} Deck;

IMO even better would be to dump the array and use a container.

What do you mean by "container" ?


A container is an object that contains objects of some type.
An array is an example of a container.
A list is a container.
A queue is a container.


Thanks, but I was asking Mike Wahler what *he* meant by "container" .

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

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

Similar topics

2
6410
by: Joshua Beall | last post by:
Hi All, What does the "const" reserved word in PHP4 do? I know it is a reserved word in PHP4, but the only references I can find to it are for PHP5. Here's what I want to do: Rather than using define(), I'd like to declare a variable as a constant. This way, I can use it in strings. Specifically, in heredoc strings - constants don't get interpreted in between <<<EOT and EOT;
1
2509
by: bucher | last post by:
Hi, I want to push a const auto_ptr into a vector, but the compile reports errors. Below is the code. class Folder; class Result; class Results { public: int size(){return _Items.size();}
3
2487
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going wrong? I am using g++ (GCC) 3.3.5 on a Debian Sarge system. The compiler complains: //**************************** //**************************** Compiler output starts *********** cd /home/red/tmp/testprogs/
3
1888
by: Alexander Farber | last post by:
Hi, does anyone have an idea, why do I get the following error (I have to use g++296 on RedHat Linux as compiler): In file included from r_dir.cpp:9: r_obey.h:262: declaration of `const AreaSet &CObeyFile::AreaSet () const' r_areaset.h:197: changes meaning of `AreaSet' from `class AreaSet'
4
6108
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple definition errors if the header file is #include'd in more than one code file. The answer was that constants have internal linkage unless declared extern, so it's OK. So, you can put something like...
9
1269
by: Gary | last post by:
Hi all! I've taken some time on learning the difference between "pointers to const variables" and "const pointer variables". The question is: in the following code, can we change the contents of the const pointer (i.e. t)? I got a segmentation fault in the last for loop. I wrote the code in c++, but the language is not the point, right? :) Thanks in advance! #include <iostream>
26
11791
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
20
3583
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
4
2652
by: adam.timberlake | last post by:
Me again! I am trying to create a currency converter function for my eCommerce that still pet treats. I am a little short on money recently and so cannot afford a programmer. Can somebody tell me why my script tells me that I have a syntax error on the "CONST" line. I am trying to follow the article below. Article: http://www.talkphp.com/showthread.php?t=1503 The error I get is:
0
9948
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
11164
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
10767
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
10864
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
10422
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
5807
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
6008
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4623
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
4230
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.