Results 1 to 6 of 6

Thread: Assigning an array to a non-array variable

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Assigning an array to a non-array variable

    If I have an array:

    int array[4];

    and a non array:

    int nonarray;

    and I see this:

    nonarray = array;

    do I assume that it is assigning the [0] element of array to nonarray?
    Last edited by jmsrickland; Dec 13th, 2014 at 05:53 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    679

    Re: Assigning an array to a non-array variable

    array refers to the address of memory where the data of the array is stored. Thus nonarray is assigned the memory address of array - not the contents of the first element. A simple c test program shows this
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    int array[4] = {0, 1, 2, 3};
    int nonarray;
    
    	nonarray = array;
    
    	printf("value of nonarray: 0x%08x\n", nonarray);
    	return 0;
    }
    on my system this shows
    Code:
    value of nonarray: 0x0022fa44
    which is the memory address of array, not its first element.

    My compiler also shows a warning message that "'int' differs in levels of indirection from 'int *" as nonarray should be defined as
    Code:
    int *nonarray;
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2019 (16.7.3)

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Assigning an array to a non-array variable

    I understand.

    What I posted was just a simple example in order to get the idea of what was taking place. Actually the real code is like this:

    Code:
      '
      '
    #define csteMAX	256
    
    typedef struct tagSTE 
    {
      int coUs;
      //
      // more members
      //	
    } 
    STE, *PSTE;
    
    typedef struct tagCON
    {
      STE	argste[csteMAX];
      //
      // more members
      //	    
    } 
    CON, *PCON;
      '
      '
      '
    BOOL FInitCon(PCON pcon, char const *szFen)
    {
      PSTE pste = pcon->argste;
       '
       '
      pste->coUS = 1;
       '
       '
       '
    }
    So, since argste is an array of struct tagSTE in struct tagCON and pste is a pointer to this array then it has to point to the [0] element of the array, am I correct?

    When I see the line, pste->coUS = 1;, that tells me it is assigning the value of 1 to the member coUs in the array like this:

    argste:
    Code:
                 0          1                  255
            +----------+----------+       +----------+
    pste--> | coUs = 1 | coUs = 0 |       | coUs = 0 |
            |  '       |  '       |.......|  '       |
            |  '       |  '       |       |  '       |  
            +----------+----------+       +----------+
    Since I see no increment of pste then that tells me that only the members of element [0] of the array are being assigned values and no other element of the array are getting any values assigned to them. Is this correct?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,110

    Re: Assigning an array to a non-array variable

    I would say pste points to the first element, and is setting the first elements coUs field to the color the computer is playing, coUs I'm assuming is "color, Us" vs "color, them".

    As for the other entries being set, I believe they are being set in the VFixSte function, which is called near the end of the FInitCon function.
    The comments alone suggest that:
    // Once the first STE record is set up properly, this cleans up a few
    // details in the record, and sets up the rest of the array properly.

    At the end of that function they have the loop
    Code:
    	for (i = 1; i < csteMAX; i++)
    		pcon->argste[i].coUs = pste->coUs ^ (i & 1);
    That loop is taking the color (0 = white or 1 = black) in the first entry, pste->coUs, and exclusive ORing that value with (i And 1).
    Since i is incrementing by 1 in the loop, (i & 1) will toggle between 0 and 1, all the odd values of i (e.g. 1,3,5,...) Anded with 1 will return 1, and all the even values (2,4,6...) will returm 0.
    One way to look at Exclusive OR boolean operation:
    If you Exclusive OR a bit with 1, it returns the opposite value, i.e. toggles the bit ( 0 ^ 1 returns 1, 1 ^ 1 returns 0).
    If you Exclusive OR a bit with 0, it returns the bit unchanged ( 0 ^ 0 returns 0, 1 ^ 0 returns 1).

    The end result of that loop is, whatever color the computer is playing which is stored in pste->coUs, aka pcon->argste[0].coUs, then all the even indexes in the array will be set to the same value, and all the odd indexes will be set to the opposite.
    That array is tracking moves, and each side takes turns, so the entries are preset to alternating values of 0 and 1 (or 1 and 0) depending on what letter "w" or "b" was found in the initialization string for what color the engine is playing.
    Last edited by passel; Dec 15th, 2014 at 12:29 AM.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Assigning an array to a non-array variable

    The line in VFixSte,

    pcon->argste[i].coUs = pste->coUs ^ (i & 1);

    seems a bit odd to me as pcon->argste[i].coUs and pste->coUs are both pointing to the same memory location so why not just do

    pcon->argste[i].coUs = pcon->argste[i].coUs ^ (i & 1); or pste->coUs = pste->coUs ^ (i & 1);


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,110

    Re: Assigning an array to a non-array variable

    They are never pointing to the same memory location (i is never 0).
    pste->coUs is pointing to pcon->argste[0].coUs , i.e. it is a direct pointer to the first (0) element of the array, without the overhead of recalculating the first records offset over and over.
    pcon->argste[i].coUs is pointing to the [i] element of pcon->argste, i.e. it is stepping through the array, so you are modifying the i'th element based on what the 0 element is set to.
    The variable i in the for loop starts at 1, so they never point to the same location, not even the first time through the loop.

    The code could have been
    pcon->argste[i].coUs = pcon->argste[0].coUs ^ (i & 1);

    or even
    pcon->argste[i].coUs = pcon->argste[i-1].coUs ^ 1;

    since each element should be the opposite of the one in front (lower index) of it in the array.
    In either of those two cases, you're having to recalculate offsets into the array twice (although the compiler might possibly optimize the generated code if smart), whereas the code as given only has to calculate the iterative offset, not the 0 offset each time through the loop.
    The code could have also just used a local variable for the start color at this point as well.
    int startCo = pste->coUs;
    then the line could be
    pcon->argste[i].coUs = startCo ^ (i & 1);
    Last edited by passel; Dec 15th, 2014 at 08:40 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width