Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

char (*name)[3]

30 views
Skip to first unread message

David Michael Schwartz

unread,
May 4, 1994, 12:33:49 AM5/4/94
to
What is the meaning and use of the following declaration,

char (*name)[3]

I think it is an array of pointers to char yet I cannot
get any of the pointers to accept a string.

For example,

*name[0] = " hello ";

does not work.

I've got K&R opened to p.109 and the FAQ opened to 2.12.

woe. For any response, I will be most grateful.

Kurt Watzka

unread,
May 4, 1994, 7:14:57 AM5/4/94
to
sch...@alum01.its.rpi.edu (David Michael Schwartz) writes:

>What is the meaning and use of the following declaration,

>char (*name)[3]

>I think it is an array of pointers to char yet I cannot
>get any of the pointers to accept a string.

It might be a pointer to arrays of three characters?
*After* allocating memory, assign characters to the
elements of name. Treat name as a two-dimensional array
of char.

name = malloc(SIZE*3*sizeof(char));
name[i][2] = 'a';


Kurt Watzka
wat...@statistik.uni-muenchen.d400.de
ua3...@sunmail.lrz-muenchen.de
-------------------------------------

Ray Reid

unread,
May 4, 1994, 10:16:50 AM5/4/94
to
David Michael Schwartz (sch...@alum01.its.rpi.edu) wrote:
: What is the meaning and use of the following declaration,

: char (*name)[3]

: For example,

: *name[0] = " hello ";

: does not work.


Depending on your compiler, declare name as following....

char *name[3];

if you want to assign a pointer use,

name[0] = "hello";

Your code was trying to stuff a strings worth of gear into a char.

Kevin D. Quitt

unread,
May 4, 1994, 5:44:30 PM5/4/94
to
Thus wrote sch...@alum01.its.rpi.edu (David Michael Schwartz)
>What is the meaning and use of the following declaration,
>char (*name)[3]

Get the program cdecl:

cdecl> explain char (*name)[3]
declare name as pointer to array 3 of char
cdecl> declare name as array 3 of pointer to char
char *name[3]
cdecl>

--
#include <standard.disclaimer>
_
Kevin D Quitt 91351-4454 96.37% of all statistics are made up

Jonas Schlein

unread,
May 5, 1994, 2:14:21 AM5/5/94
to
|> What is the meaning and use of the following declaration,
|>
|> char (*name)[3]
|>
|> I think it is an array of pointers to char yet I cannot
|> get any of the pointers to accept a string.

That's because it's a pointer to an array of three characters. You would
have to allocate space, but after you do that you can dereference it and
use it just like an array of 3 characters.
--
Jonas Schlein General E-Mail : sch...@umbc.edu
Math Council of Majors President Math Council E-Mail: sch...@math.umbc.edu

Geek code: GCS/M -p+ c++(+++) l u+(++) e+(*)@ m@ s+/++ n+ f? g- t+(+++)@ r+ y?

Vincenzo Romano

unread,
May 6, 1994, 4:50:12 AM5/6/94
to
sch...@alum01.its.rpi.edu (David Michael Schwartz) writes:

>What is the meaning and use of the following declaration,

>char (*name)[3]

>I think it is an array of pointers to char yet I cannot
>get any of the pointers to accept a string.

You're right!

>For example,

>*name[0] = " hello ";
>does not work.

... bacause you made a mistake! If name is a vector of pointers to chars,
name[0] is a pointer to char and *name[0] is the first char pointed by it!
Try:
name[0] = " hello ";

>I've got K&R opened to p.109 and the FAQ opened to 2.12.

>woe. For any response, I will be most grateful.

You're welcome!
--
Vincenzo "Enzo" Romano .--------------------------------------------------.
CNUCE/C.N.R. Pisa | Maybe Computers will never become as intelligent |
mu...@MILES.cnuce.cnr.IT | as Humans. Surely they won't become so stupid. |
vro...@NYX.cs.du.edu `--------------------------------------[ Enzo ]----'
--
Vincenzo "Enzo" Romano .--------------------------------------------------.
CNUCE/C.N.R. Pisa | Maybe Computers will never become as intelligent |
mu...@MILES.cnuce.cnr.IT | as Humans. Surely they won't become so stupid. |
vro...@NYX.cs.du.edu `--------------------------------------[ Enzo ]----'

Arjan Kenter

unread,
May 6, 1994, 7:13:18 AM5/6/94
to
In article <CpA7M...@westminster.ac.uk>, ob...@westminster.ac.uk (Ray Reid) writes:
[about what ``char (*name)[3];'' means and why *name[0]="string" doesn't work]

|> Depending on your compiler, declare name as following....

Why should that depend on your compiler? Any compiler that does not
accept it, ought to be thrown out the window rightaway.

|> char *name[3];
|>
|> if you want to assign a pointer use,
|>
|> name[0] = "hello";
|>
|> Your code was trying to stuff a strings worth of gear into a char.

Still, this does not answer the other question: what does char (*name)[3] mean?
C declarations are read "inside out" so that their declaration matches their
use as close as possible. So you first see

*name

which tells you that `name' is a pointer to something. To what then? Well:

char ... [3]

`name' is a pointer to an array of three characters.

--
^^
ir. H.J.H.N. Kenter oo ) ken...@cs.utwente.nl
University of Twente =x= \ tel. +31 53 893747
Tele-Informatics & Open Systems | \ tfx. +31 53 333815
P.O. Box 217 7500 AE Enschede /|__ \
The Netherlands (____)_/

``Look up the words in a dictionary. -- Ian Stewart - Does God Play Dice?
Mega: big (Referring to the use of high-performance
Flop: failure'' computers for weather prediction.)

David Anderson

unread,
May 6, 1994, 5:33:31 PM5/6/94
to
David Michael Schwartz (sch...@alum01.its.rpi.edu) wrote:
: What is the meaning and use of the following declaration,

: char (*name)[3]

: For example,

: *name[0] = " hello ";

: does not work.

Quick Answer: 'name' is a pointer to an array of 3 characters.

Long Answer: These types of questions come up from time to time and I have
passed on (via email) my article on using the "clockwise rule". I thought that
I would post it again since it has been a long time since I posted this to this
newsgroup.

The clockwise rule is a method which I use when teaching C to help people learn
how to parse these complicated declarations *in their head*. It is very easy to
use this technique, and I'll guarantee you will never have further problems
understanding C declarations once you learn this technique.

So without further ado... the "clockwise rule":

*********************************************************************************
The "Clockwise/Spiral Rule"
===========================
By David Anderson
-----------------

There is a technique known as the "Clockwise/Spiral Rule" which enables
any C programmer to parse in their head any C declaration!

There are three simple steps to follow:

1. Starting with the unknown element, move in a spiral/clockwise direction;
when ecountering the following elements replace them with the corresponding
english statements:

[X] or [] => Array X size of... or Array undefined size of...
(type1, type2) => function passing type1 and type2 returning...
* => pointer(s) to...

2. Keep doing this in a spiral/clockwise direction until all tokens have been
covered.

3. Always resolve anything in parenthesis first!


Example #1: Simple declaration

+-------+
| +-+ |
| ^ | |
char *str[10];
^ ^ | |
| +---+ |
+-----------+

Question we ask ourselves: What is str?

"str is an...

o We move in a spiral clockwise direction starting with 'str' and the first
character we see is a '[' so, that means we have an array, so...

"str is an array 10 of...

o Continue in a spiral clockwise direction, and the next thing we encounter is
the '*' so, that means we have pointers, so...

"str is an array 10 of pointers to...

o Continue in a spiral direction and we see the end of the line (the ';'), so
keep going and we get to the type 'char', so...

"str is an array 10 of pointers to char"

o We have now "visited" every token; therefore we are done!


Example #2: Pointer to Function declaration

+--------------------+
| +---+ |
| |+-+| |
| |^ || |
char *(*fp)( int, float *);
^ ^ ^ || |
| | +--+| |
| +-----+ |
+------------------------+

Question we ask ourselves: What is fp?

"fp is a...

o Moving in a spiral clockwise direction, the first thing we see is a ')';
therefore, fp is inside parenthesis, so we continue the spiral inside the
parenthesis and the next character seen is the '*', so...

"fp is a pointer to...

o We are now out of the parenthesis and continuing in a spiral clockwise
direction, we see the '('; therefore, we have a function, so...

"fp is a pointer to a function passing an int and a pointer to float
returning...

o Continuing in a spiral fashion, we then see the '*' character, so...

"fp is a pointer to a function passing an int and a pointer to float
returning a pointer to...

o Continuing in a spiral fashion we see the ';', but we haven't visited all
tokens, so we continue and finally get to the type 'char', so...

"fp is a pointer to a function passing an int and a pointer to float
returning a pointer to a char"


Example #3: The "Ultimate"

+-----------------------------+
| +---+ |
| +---+ |+-+| |
| ^ | |^ || |
void (*signal(int, void (*fp)(int)))(int);
^ ^ | ^ ^ || |
| +------+ | +--+| |
| +--------+ |
+----------------------------------+

Question we ask ourselves: What is 'signal'?

Notice that signal is *inside* parenthesis, so we must resolve this first!

o Moving in a *clockwise* direction we see '(' so we have...

"signal is a function passing an int and a...

o Hmmm, we can use this same rule on 'fp', so... What is fp? fp is also inside
parenthesis so continuing we see an '*', so...

"fp is a pointer to...

o Continue in a spiral clockwise direction and we get to '(', so...

"fp is a pointer to a function passing int returning..."

o Now we continue out of the function parenthesis and we see void, so...

"fp is a pointer to a function passing int returning nothing (void)"

o We have finished with fp so let's catch up with 'signal', we now have...

"signal is a function passing an int and a pointer to a function passing an
int returning nothing (void) returning...

o We are still inside parenthesis so the next character seen is a '*', so...

"signal is a function passing an int and a pointer to a function passing an
int returning nothing (void) returning a pointer to...

o We have now resolved the items within parenthesis, so continuing clockwise,
we then see another '(', so...

"signal is a function passing an int and a pointer to a function passing an
int returning nothing (void) returning a pointer to a function passing an
int returning...

o *Finally* we continue and the only thing left is the word 'void', so the
final complete definition for signal is:

"signal is a function passing an int and a pointer to a function passing an
int returning nothing (void) returning a pointer to a function passing an
int returning nothing (void)"

The same rule is applied for const and volatile. For Example:

const char *chptr;

o Now, what is chptr??

"chptr is a pointer to a char constant"

How about this one:

char * const chptr;

o Now, what is chptr??

"chptr is a constant pointer to char"

Finally:

volatile char * const chptr;

o Now, what is chptr??

"chptr is a constant pointer to a char volatile.

Practice this rule with the examples found in K&R II on page 122.

************************************************************************
Copyright (C) 1993,1994 David Anderson

This article may be freely distributed as long as the author's name and
this notice are retained.
************************************************************************

--
David AR Anderson
email: David_A...@Mitel.COM
Mitel Corporation Disclaimer: My opinions are my own!

Klaus Guhr

unread,
May 9, 1994, 4:33:45 AM5/9/94
to
test

Bye,
Klaus

--
Klaus...@Gnugate.Rhein.DE

0 new messages