www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/18/13:49:27

From: kagel AT quasar DOT bloomberg DOT com
Date: Mon, 18 Nov 1996 13:05:17 -0500
Message-Id: <9611181805.AA01924@quasar.bloomberg.com >
To: e DOT oti AT stud DOT warande DOT ruu DOT nl
Cc: djgpp AT delorie DOT com
In-Reply-To: <328DD331.26E4@stud.warande.ruu.nl> (message from Elliott Oti on Sat, 16 Nov 1996 06:44:01 -0800)
Subject: Re: Q: Typedef riddle
Reply-To: kagel AT dg1 DOT bloomberg DOT com

   Errors-To: postmaster AT bloomberg DOT com
   From: Elliott Oti <e DOT oti AT stud DOT warande DOT ruu DOT nl>
   Newsgroups: comp.os.msdos.djgpp
   Date: Sat, 16 Nov 1996 06:44:01 -0800
   Organization: Academic Computer Centre Utrecht, (ACCU)
   Lines: 40
   Nntp-Posting-Host: warande1078.warande.ruu.nl
   Mime-Version: 1.0
   Content-Transfer-Encoding: 7bit
   X-Mailer: Mozilla 2.02 (Win16; I)
   Dj-Gateway: from newsgroup comp.os.msdos.djgpp
   Content-Type: text/plain; charset=us-ascii
   Content-Length: 800

   Hi,
   I've been programming in C for a couple of years now and I thought
   I knew at least the basics pretty well, but this has me stumped.

   Why does the following short little program output

	10 <rubbish> <rubbish>
	It's also 10 10 10

   instead of  

	10 10 10
	It's also 10 10 10 

   It's not a gcc bug because Borland gives a similar output,
   but -Wall gives absolutely no warnings. So what's wrong?

Because you declare V in Dump to be pointer to thing.  In this case V[1]
is the second array of three integers and *V[1] is the first element of 
this array.  Both V[1] and V[2] are out of range of the array V declared \
in main().  Change the code as shown

   --------------------- SNIP ------------------------------------------
   #include <stdio.h>

   typedef int thing[3];

/*   void Dump(thing *V) */
   void Dump(thing V) 
   {
   printf("\n%i %i %i\n",V[0],V[1],V[2]);
   }

   int main(void)
   {
   thing V;
   V[0] = V[1] = V[2] = 10;
/*   Dump(&V); */   /* Actually V, &V, and &V[0] are all the same address so 
   Dump(V);         /* this is really OK also, but just V is better. */
   printf("\nIt's also %i %i %i\n",V[0],V[1],V[2]);
   return 0;
   }

   -------------------------------------------------------------------

- Raw text -


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