[comp.lang.c] array side-effect

<PRD3@psuvm.psu.edu> (06/14/91)

I have found a real stumper.
Partial listing follows:
...
int CODE_MATRIX[26][25];         //global
char INMOD1[50][3];              //global
...
int mod_loop=0;                  //local
...
mod_loop++;
INMOD1[mod_loop][0]="9";        //mod_loop is 1


This last statement changes the value of CODE_MATRIX[26][1]
from -9 to some large random number. Rows 1-25 are unchanged.
CODE_MATRIX[26][0] and CODE_MATRIX[26][2] may also be affected
but I forgot to check.  I have tried '9' instead of "9"
and I have used strcpy to replace the whole line.  The problem persists.

I am using Borland Turbo C++ on a 386Clone running DOS.

c60b-4fd@web-4f.berkeley.edu (Dave Zoss) (06/15/91)

In article <91165.095327PRD3@psuvm.psu.edu> PRD3@psuvm.psu.edu writes:
>I have found a real stumper.
>Partial listing follows:
>...
>int CODE_MATRIX[26][25];         //global

(code deleted)

>This last statement changes the value of CODE_MATRIX[26][1]

When declaring arrays in C or C++, the number in brackets specifies the SIZE
of the array.  The indices, therefore, range from 0 to SIZE-1.

The cell CODE_MATRIX[26][1] does not exist.

Dave Zoss		zoss@ocf.berkeley.edu		zoss@icf.llnl.gov

KQS@psuvm.psu.edu (Kevin Sullivan) (06/15/91)

Two problems here:

In article <91165.095327PRD3@psuvm.psu.edu>, <PRD3@psuvm.psu.edu> says:
>...
>int CODE_MATRIX[26][25];         //global
>char INMOD1[50][3];              //global
>...
>int mod_loop=0;                  //local
>...
>mod_loop++;
>INMOD1[mod_loop][0]="9";        //mod_loop is 1

First: an array declared as a[SIZE] goes from a[0] to a[SIZE-1]; a[SIZE]
is undefined (and in thic case points to the first element of the next
array).

second:  "9" evaluates to a (const char *), not a char.  But perhaps
you meant '9', which is a char (actually an int).


     Kevin Sullivan                       BITNET: KQS@PSUVM
     Student Consultant - CAC           INTERNET: kqs@vm.psu.edu
     Penn State University
                                        Old programmers don't die...they
I don't speak for Penn State...         just branch to a new address.
Penn State doesn't speak for me.

mouse@thunder.mcrcim.mcgill.edu (der Mouse) (06/18/91)

In article <91165.095327PRD3@psuvm.psu.edu>, PRD3@psuvm.psu.edu writes:

> I have found a real stumper.  Partial listing follows:

> int CODE_MATRIX[26][25];         //global
> char INMOD1[50][3];              //global

> int mod_loop=0;                  //local

> mod_loop++;
> INMOD1[mod_loop][0]="9";        //mod_loop is 1

> This last statement changes the value of CODE_MATRIX[26][1] from -9
> to some large random number.

It should provoke a compile-time complaint about assigning a pointer to
an integer ("9" is a pointer[%], but INMOD1[mod_loop][0] is an
integer[$]).

[%] Actually, an array, but in this context it immediately decays to a
    pointer.
[$] I said integer, not int; "char" is one of the integer datatypes.

That aside, CODE_MATRIX[26][1] doesn't exist; the first subscript
ranges only 0..25.  So when you access CODE_MATRIX[26][1] you could get
anything from a coredump to the current time of day in Uganda to (most
likely, in this case) two of the elements of INMOD1 packed together.

> I am using Borland Turbo C++ on a 386Clone running DOS.

Thank you for mentioning this; *nobody* seems to think to say what
machine and OS are in use anymore....

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

bkottmann@falcon.aamrl.wpafb.af.mil (Brett Kottmann) (06/20/91)

In article <1991Jun14.184344.6066@agate.berkeley.edu>, c60b-4fd@web-4f.berkeley.edu (Dave Zoss) writes:
> In article <91165.095327PRD3@psuvm.psu.edu> PRD3@psuvm.psu.edu writes:
>>I have found a real stumper.
>>Partial listing follows:
>>...
>>int CODE_MATRIX[26][25];         //global
> 
> (code deleted)
> 
>>This last statement changes the value of CODE_MATRIX[26][1]
> 
> When declaring arrays in C or C++, the number in brackets specifies the SIZE
> of the array.  The indices, therefore, range from 0 to SIZE-1.
> 
> The cell CODE_MATRIX[26][1] does not exist.
> 

	What a great exam question (or employee quiz)!!!!

;)
Brett
=============================OFFICIAL=DISCLAIMER================================
The opinions and views expressed here are strictly my own and do not 
necessarily reflect the official position of either the U.S. Air Force 
or its contractors.
=====================DO=NOT=REMOVE=TAG=UNDER=PENALTY=OF=LAW===:)================