delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/04/10:02:20

From: chrhenz AT aol DOT com (ChrHenz)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Pointer problem
Date: 4 Jan 1998 14:54:21 GMT
Lines: 54
Message-ID: <19980104145401.JAA15892@ladder02.news.aol.com>
NNTP-Posting-Host: ladder02.news.aol.com
References: <01bd182a$edde3de0$4d3e64c2 AT default>
Organization: AOL Bertelsmann Online GmbH & Co. KG http://www.germany.aol.com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Im Artikel <01bd182a$edde3de0$4d3e64c2 AT default>, "laine" <laine AT ks DOT iwn DOT fi>
schreibt:

>Hi!
>
>How can I make a pointer to an array?
>
>I'm not sure if it's called an array, but it's like this:
>	 array[5][1] (for example)
>
>Thanks
>

This one is from taken the FAQ of comp.lang.c :

2.10:	My compiler complained when I passed a two-dimensional array to
	a routine expecting a pointer to a pointer.

A:	The rule by which arrays decay into pointers is not applied
	recursively.  An array of arrays (i.e. a two-dimensional array
	in C) decays into a pointer to an array, not a pointer to a
	pointer.  Pointers to arrays can be confusing, and must be
	treated carefully.  (The confusion is heightened by the
	existence of incorrect compilers, including some versions of pcc
	and pcc-derived lint's, which improperly accept assignments of
	multi-dimensional arrays to multi-level pointers.)  If you are
	passing a two-dimensional array to a function:

		int array[NROWS][NCOLUMNS];
		f(array);

	the function's declaration should match:

		f(int a[][NCOLUMNS]) {...}
	or
		f(int (*ap)[NCOLUMNS]) {...}   /* ap is a pointer to an array */

	In the first declaration, the compiler performs the usual
	implicit parameter rewriting of "array of array" to "pointer to
	array;" in the second form the pointer declaration is explicit.
	Since the called function does not allocate space for the array,
	it does not need to know the overall size, so the number of
	"rows," NROWS, can be omitted.  The "shape" of the array is
	still important, so the "column" dimension NCOLUMNS (and, for 3-
	or more dimensional arrays, the intervening ones) must be
	included.

	If a function is already declared as accepting a pointer to a
	pointer, it is probably incorrect to pass a two-dimensional
	array directly to it.

	References: K&R I Sec. 5.10 p. 110; K&R II Sec. 5.9 p. 113.

Hope that helps... Christian.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019