473,699 Members | 2,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

int (*daytab)[13] vs int *daytab[13]

Hi,
I am studying K&R book. On the multidimensiona l Arrays chapter they
say
"int (*daytab)[13]
is a pointer to an array of 13 integers. The parenthesis are necessary
since brackets [] have higher precedence than *. Without parenthesis,
the declaration
int *daytab[13]
is an array of 13 pointers to integers."

Here is how I get confused: Think about "int *daytab[13]". Since []
have higher precedence than *, I should parenthesize it as "int
*(daytab[])". Now that should mean "a pointer to an array of
integers". Or st. like that... Men, I am already lost.

also int (*daytab)[13] seems to me "an 13 element array of pointers"
if I read according to parenthesis.

totally wrong.

I think I dont know how to interpret/read [] in the first place. int
daytab[13] is an array of 13 integers. That is ok. Better throw out
yourself from window instead of trying to understand when there are
some *s around though.

Can sb. help me about this?
Thanks in advance.
Nov 13 '05 #1
5 2310
j

"Kemal Ozan" <km****@hotpop. com> wrote in message
news:1f******** *************** ***@posting.goo gle.com...
Hi,
I am studying K&R book. On the multidimensiona l Arrays chapter they
say
"int (*daytab)[13]
is a pointer to an array of 13 integers. The parenthesis are necessary
since brackets [] have higher precedence than *. Without parenthesis,
the declaration
int *daytab[13]
is an array of 13 pointers to integers."

Here is how I get confused: Think about "int *daytab[13]". Since []
have higher precedence than *, I should parenthesize it as "int
*(daytab[])". Now that should mean "a pointer to an array of
integers". Or st. like that... Men, I am already lost.

also int (*daytab)[13] seems to me "an 13 element array of pointers"
if I read according to parenthesis.

totally wrong.

I think I dont know how to interpret/read [] in the first place. int
daytab[13] is an array of 13 integers. That is ok. Better throw out
yourself from window instead of trying to understand when there are
some *s around though.

Can sb. help me about this?
Thanks in advance.

[] = denotes "array of"
* = denotes "pointer to"

[] has higher precedence over *
and parens() have higher precedence over [] and *
So when you encounter () in a declaration, you first read
everything enclosed within the parens starting with the most
inner parens.

Also, we always start with an identifier first. e.g., char hello; hello is
the identifier.
When we encounter parens, *, or [] -- we state what they represent over any
listed types.
That is, if we have ``char bar[10];'' We first state the identifier ``bar''
We then proceed to state what ``[]'' means, which is ``array-of'', followed
by what is
enclosed within the subscript([]) and laslty the type, which is ``char''

We get: bar is array-of 10 char

So let's do this again..
Given the example declaration:
int foo[10];
We first start with the identifier. The identifier here is ``foo''
So we say ``foo''

We then see that ``foo'' is followed by the subscript operator
and that the subscript operator is not empty, so we also
state what is enclosed within the subscript operator
We then get: ``foo is array-of 10''

Lastly, we see the type(int) listed on the left.
We finish this up by stating that type with what we already have:
``foo is array-of 10 int''

So if you follow these same rules in reading C declarations, you should be
able to understand
the declarations which you listed.

int (*daytab)[13];

We see it includes parens, so we start within the parens.
Which gives us: ``* daytab''

``daytab'' is our identifier. So we say the identifiers name..
``daytab''; we now see that the indirection operator is also included within
the parens.
This means that we state what the indirection operator represents
Which is ``pointer to''
We now have: ``daytab is a pointer to''
We are now finished reading what is contained within the parens, so we
discard what
was in those parens and now concentrate on what is outside those parens.
We see the following: [13] and int
Looking back at our rules, we know to say what the subscript operator
represents over
the listed type.
Which now gives us: ``array-of 13'' and then followed by the type of ``int''
Once we construct it all together the declaration now reads: ``daytab is a
pointer to array of 13 int''

Now an additional note, for being able to understand pointers to functions:
parens which designate types of parameters denote functions.
(they could even be empty, but that is considered obsolescent)
e.g.,
int (*foo)(int);

You might be confused here by which to read first, since we have two things
listed within two separate parens. But if you recall, I said start with the
identifier first.
This means, we can just focus on the following of that declaration:
(*foo) and set ``int and (int)'' aside for later.
Given (*foo), this means that ``foo'' is a ``pointer to'', but a pointer to
what?
If we look at the stuff we set aside a moment ago, we can see we have:
int and (int)
What the latter describes is the type of a parameter for a function
We state this as: ``function'' and alternatively we can list the types
enclosed within those parens.

We are now up to ``foo is pointer to function'', and what does this function
return? Well, what else did we set aside from earlier? Oh, the type. Which
was ``int'' (which is the type which is returned)
Finally we can read it as: ``foo is pointer to function returning int''
Or alternatively, if we wish to state the types listed in the function
parens:
``foo is pointer to function (accepting an int) returning int''
Nov 13 '05 #2
In practice, you'll almost NEVER see "int (*daytab)[13]". There's just no
need for the extra level of indirection, especially when you can just use
'daytab' as a pointer to the first array element. Other than understanding
the language, I wouldn't worry too much if the "int (*daytab)[13]" doesn't
completely make sense to you.

"j" <ja**********@b ellsouth.net> wrote in message
news:eL******** ********@bignew s4.bellsouth.ne t...

"Kemal Ozan" <km****@hotpop. com> wrote in message
news:1f******** *************** ***@posting.goo gle.com...
Hi,
I am studying K&R book. On the multidimensiona l Arrays chapter they
say
"int (*daytab)[13]
is a pointer to an array of 13 integers. The parenthesis are necessary
since brackets [] have higher precedence than *. Without parenthesis,
the declaration
int *daytab[13]
is an array of 13 pointers to integers."

Here is how I get confused: Think about "int *daytab[13]". Since []
have higher precedence than *, I should parenthesize it as "int
*(daytab[])". Now that should mean "a pointer to an array of
integers". Or st. like that... Men, I am already lost.

also int (*daytab)[13] seems to me "an 13 element array of pointers"
if I read according to parenthesis.

totally wrong.

I think I dont know how to interpret/read [] in the first place. int
daytab[13] is an array of 13 integers. That is ok. Better throw out
yourself from window instead of trying to understand when there are
some *s around though.

Can sb. help me about this?
Thanks in advance.

[] = denotes "array of"
* = denotes "pointer to"

[] has higher precedence over *
and parens() have higher precedence over [] and *
So when you encounter () in a declaration, you first read
everything enclosed within the parens starting with the most
inner parens.

Also, we always start with an identifier first. e.g., char hello; hello is
the identifier.
When we encounter parens, *, or [] -- we state what they represent over

any listed types.
That is, if we have ``char bar[10];'' We first state the identifier ``bar'' We then proceed to state what ``[]'' means, which is ``array-of'', followed by what is
enclosed within the subscript([]) and laslty the type, which is ``char''

We get: bar is array-of 10 char

So let's do this again..
Given the example declaration:
int foo[10];
We first start with the identifier. The identifier here is ``foo''
So we say ``foo''

We then see that ``foo'' is followed by the subscript operator
and that the subscript operator is not empty, so we also
state what is enclosed within the subscript operator
We then get: ``foo is array-of 10''

Lastly, we see the type(int) listed on the left.
We finish this up by stating that type with what we already have:
``foo is array-of 10 int''

So if you follow these same rules in reading C declarations, you should be
able to understand
the declarations which you listed.

int (*daytab)[13];

We see it includes parens, so we start within the parens.
Which gives us: ``* daytab''

``daytab'' is our identifier. So we say the identifiers name..
``daytab''; we now see that the indirection operator is also included within the parens.
This means that we state what the indirection operator represents
Which is ``pointer to''
We now have: ``daytab is a pointer to''
We are now finished reading what is contained within the parens, so we
discard what
was in those parens and now concentrate on what is outside those parens.
We see the following: [13] and int
Looking back at our rules, we know to say what the subscript operator
represents over
the listed type.
Which now gives us: ``array-of 13'' and then followed by the type of ``int'' Once we construct it all together the declaration now reads: ``daytab is a
pointer to array of 13 int''

Now an additional note, for being able to understand pointers to functions: parens which designate types of parameters denote functions.
(they could even be empty, but that is considered obsolescent)
e.g.,
int (*foo)(int);

You might be confused here by which to read first, since we have two things listed within two separate parens. But if you recall, I said start with the identifier first.
This means, we can just focus on the following of that declaration:
(*foo) and set ``int and (int)'' aside for later.
Given (*foo), this means that ``foo'' is a ``pointer to'', but a pointer to what?
If we look at the stuff we set aside a moment ago, we can see we have:
int and (int)
What the latter describes is the type of a parameter for a function
We state this as: ``function'' and alternatively we can list the types
enclosed within those parens.

We are now up to ``foo is pointer to function'', and what does this function return? Well, what else did we set aside from earlier? Oh, the type. Which
was ``int'' (which is the type which is returned)
Finally we can read it as: ``foo is pointer to function returning int''
Or alternatively, if we wish to state the types listed in the function
parens:
``foo is pointer to function (accepting an int) returning int''

Nov 13 '05 #3
"Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> wrote:
In practice, you'll almost NEVER see "int (*daytab)[13]". There's just no
need for the extra level of indirection, especially when you can just use
'daytab' as a pointer to the first array element. Other than understanding
the language, I wouldn't worry too much if the "int (*daytab)[13]" doesn't
completely make sense to you.


You make a 'pointer to array' every time you use a multi-dimensional array in C.

int foo(int array[2][3])
{
return array[1][2];
}

This function actually takes an int(*)[3] and the prototype of this function
can be written as:
int foo(int (*array)[3]);

--
Simon.
Nov 13 '05 #4
j wrote:
"Kemal Ozan" <km****@hotpop. com> wrote in message
I am studying K&R book. On the multidimensiona l Arrays chapter
they say
"int (*daytab)[13]
is a pointer to an array of 13 integers. The parenthesis are
necessary since brackets [] have higher precedence than *.
Without parenthesis, the declaration
int *daytab[13]
is an array of 13 pointers to integers."

Here is how I get confused: Think about "int *daytab[13]".
Since [] have higher precedence than *, I should parenthesize
it as "int *(daytab[])". Now that should mean "a pointer to an
array of integers". Or st. like that... Men, I am already lost.
.... snip ...
[] = denotes "array of"
* = denotes "pointer to"

[] has higher precedence over *
and parens() have higher precedence over [] and *
So when you encounter () in a declaration, you first read
everything enclosed within the parens starting with the most
inner parens.

Also, we always start with an identifier first. e.g., char hello;
hello is the identifier.


.... snip excellent tutorial ...

Very nice job.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #5
Thanks alot... Miraculously, I seem to be decoding them...
I even am able to word these:
char (*(*x())[])() : x is a function-returning pointer-to array-of
pointers-to functions-returning char

char (*(*x[3])())[5] : x is an array-of 3 pointers-to
functions-returning pointer-to array-of 5 chars.

"j" <ja**********@b ellsouth.net> wrote in message news:<eL******* *********@bigne ws4.bellsouth.n et>...
"Kemal Ozan" <km****@hotpop. com> wrote in message
news:1f******** *************** ***@posting.goo gle.com...
Hi,
I am studying K&R book. On the multidimensiona l Arrays chapter they
say
"int (*daytab)[13]
is a pointer to an array of 13 integers. The parenthesis are necessary
since brackets [] have higher precedence than *. Without parenthesis,
the declaration
int *daytab[13]
is an array of 13 pointers to integers."

Here is how I get confused: Think about "int *daytab[13]". Since []
have higher precedence than *, I should parenthesize it as "int
*(daytab[])". Now that should mean "a pointer to an array of
integers". Or st. like that... Men, I am already lost.

also int (*daytab)[13] seems to me "an 13 element array of pointers"
if I read according to parenthesis.

totally wrong.

I think I dont know how to interpret/read [] in the first place. int
daytab[13] is an array of 13 integers. That is ok. Better throw out
yourself from window instead of trying to understand when there are
some *s around though.

Can sb. help me about this?
Thanks in advance.

[] = denotes "array of"
* = denotes "pointer to"

[] has higher precedence over *
and parens() have higher precedence over [] and *
So when you encounter () in a declaration, you first read
everything enclosed within the parens starting with the most
inner parens.

Also, we always start with an identifier first. e.g., char hello; hello is
the identifier.
When we encounter parens, *, or [] -- we state what they represent over any
listed types.
That is, if we have ``char bar[10];'' We first state the identifier ``bar''
We then proceed to state what ``[]'' means, which is ``array-of'', followed
by what is
enclosed within the subscript([]) and laslty the type, which is ``char''

We get: bar is array-of 10 char

So let's do this again..
Given the example declaration:
int foo[10];
We first start with the identifier. The identifier here is ``foo''
So we say ``foo''

We then see that ``foo'' is followed by the subscript operator
and that the subscript operator is not empty, so we also
state what is enclosed within the subscript operator
We then get: ``foo is array-of 10''

Lastly, we see the type(int) listed on the left.
We finish this up by stating that type with what we already have:
``foo is array-of 10 int''

So if you follow these same rules in reading C declarations, you should be
able to understand
the declarations which you listed.

int (*daytab)[13];

We see it includes parens, so we start within the parens.
Which gives us: ``* daytab''

``daytab'' is our identifier. So we say the identifiers name..
``daytab''; we now see that the indirection operator is also included within
the parens.
This means that we state what the indirection operator represents
Which is ``pointer to''
We now have: ``daytab is a pointer to''
We are now finished reading what is contained within the parens, so we
discard what
was in those parens and now concentrate on what is outside those parens.
We see the following: [13] and int
Looking back at our rules, we know to say what the subscript operator
represents over
the listed type.
Which now gives us: ``array-of 13'' and then followed by the type of ``int''
Once we construct it all together the declaration now reads: ``daytab is a
pointer to array of 13 int''

Now an additional note, for being able to understand pointers to functions:
parens which designate types of parameters denote functions.
(they could even be empty, but that is considered obsolescent)
e.g.,
int (*foo)(int);

You might be confused here by which to read first, since we have two things
listed within two separate parens. But if you recall, I said start with the
identifier first.
This means, we can just focus on the following of that declaration:
(*foo) and set ``int and (int)'' aside for later.
Given (*foo), this means that ``foo'' is a ``pointer to'', but a pointer to
what?
If we look at the stuff we set aside a moment ago, we can see we have:
int and (int)
What the latter describes is the type of a parameter for a function
We state this as: ``function'' and alternatively we can list the types
enclosed within those parens.

We are now up to ``foo is pointer to function'', and what does this function
return? Well, what else did we set aside from earlier? Oh, the type. Which
was ``int'' (which is the type which is returned)
Finally we can read it as: ``foo is pointer to function returning int''
Or alternatively, if we wish to state the types listed in the function
parens:
``foo is pointer to function (accepting an int) returning int''

Nov 13 '05 #6

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

Similar topics

12
2528
by: Tor Rustad | last post by:
Did a fun project some years ago.. a cdecl, but never really tested it. Here are some test cases I just tried: cdecl> char **argv; cdecl> argv is pointer to pointer to char Looks ok cdecl> int (*daytab); cdecl> daytab is pointer to array of int
10
1491
by: mdh | last post by:
Could someone help clear up some confusion for me. I am trying to understand how pointers and multidimensional arrays inter-relate. Given: static char daytab = { {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} };
0
8706
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
8631
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
9055
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
8902
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
7787
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
6550
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
5889
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4392
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...
2
2366
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.