Thread: Simple Question

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    82

    Simple Question

    Hi All, simple question i hope,

    Just wanted some clarification on some of the arrays i've being fiddlng with, i won't get into to much detail but essentially a 2D array comprising two integers X and Y.

    i.e: Grid[X][Y];

    With X and Y set to 5 for example.

    Now if i want to look at Grid[4][5], i can simply do Grid[--X][Y] and this allows me to look at those 'coordinates' and also sets the integer X to value 4.

    Now what i wanted to ask is whether looking at Grid[3][5] by simply doing Grid[X-2][Y] ALSO changes the value of the integer X by -2 to the value 3 or whether it only allows me to look at the 'coordinates' but not change the integer?

    Hope that was all clear, any help is appreciated

    Regards Wolfe

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    GRID[X-1][Y] will get you value at "X less one", Y, in your example 4,5. GRID[X-2][Y] does "x less two", Y, and in your example 3, 5. Neither case will change the value of X.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Thanks, by the way [--X] will change the value of X, well looks like i've got some changes to do

    Thanks Wolfe

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Cdrwolfe View Post
    Thanks, by the way [--X] will change the value of X, well looks like i've got some changes to do

    Thanks Wolfe
    Yes, --X or X-- will change the value of X. As will X += 2, X -=2 and X++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Cdrwolfe View Post
    Thanks, by the way [--X] will change the value of X, well looks like i've got some changes to do

    Thanks Wolfe
    You could just change --X to X-1...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    82
    Quote Originally Posted by QuantumPete View Post
    You could just change --X to X-1...

    QuantumPete
    Sorry, but i needed the actual value of X to change as well after it had been 'scanned' so to speak. saves me having to add in a line X = X - 1; later one even though i still have to do it for X - 2 .

    Regards Wolfe

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Cdrwolfe View Post
    Sorry, but i needed the actual value of X to change as well after it had been 'scanned' so to speak. saves me having to add in a line X = X - 1; later one even though i still have to do it for X - 2 .

    Regards Wolfe
    Well, that's what I mean, if you change --X to X-1, you can then treat it the same way as X - 2, which will save you special conditions depending on whether you're subtracting one or two. Implicit code is always better than explicit code.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    if you did --X once for getting the 4 value so to speak, and you did it again it would get the 3 value, without having to do X - 2.

    Code:
    grid[x][y]; //say its at 5,5 to start
    
    grid[--x][y]; //this is now 4,5
    
    grid[--x][y]; //now its 3,5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM