Thread: 2 dimensional pointer array to a struct

  1. #1
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56

    2 dimensional pointer array to a struct

    I compiled a program I worked on for a few weeks, and (without any suprises), it had over a hundred errors. I can fix them in time, but one major problem is that my program pretty much rests on the ability of a certain command to work. I have a class set up, with a two dimensional pointer array and several additional functions. The functions run through the array, setting the location that each slot points to to a new struct, and then modifying the variables in each struct (each struct is different). I do this like this:

    *environment[temp][temp2].type_value="cell" (as an example).

    I know now that the syntax of this is incorrect. (temp and temp2, by the way, are variables in double for loop). Does anyone know an alternative to this? Preferably one that might work

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
    //this is the way most people derference their pointers, since the -> is a dead giveaway
    environment[temp1][temp2]->type_value = "cell; 
    
    //this is also valid.  * has lower precedence than  .
    (*environment[temp1][temp2]).type_value = "cell";

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I compiled a program I worked on for a few weeks, and (without any suprises), it had over a hundred errors.
    *boggle!* - Weeks coding without compiling? - man, that's pathetic.

    You should compile often, at LEAST once an hour (for a pro), and you don't add any more code until it clean compiles once more. If you're at all unsure about what you've just coded, then you should be compiling like every 5 minutes or so.

    It takes just a few seconds to compile a single source file (less time than it takes to think about the next step), so there's no reason against frequent compiling IMO.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56
    *boggle!* - Weeks coding without compiling? - man, that's pathetic.
    OK i get it. I'm not professional, ok?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I'm saying that no sane person waits a week before pressing compile for the first time!

    You have a tool which is vastly superior at spotting mistakes than you are, and you refuse to use it on a regular basis.

    > Does anyone know an alternative to this?
    Yeah, by compiling early and often, you find out quickly, and it takes minutes to fix.

    Or leave it for a week, horribly embed your mistake all through the code and then find you've wasted a week because the only effective way out is to rewrite the thing.

    The choice is yours!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    4
    i agree, i may not be much of a C++ programmer yet, but i've been scripting with HTML and PHP for years and i never go more than 30 mins to an hour without testing the code i've written, sometimes i'll go so far as to check it line by line or every 5 minutes if i'm unsure about the code i'm writing.

    nobody can write an entire program, even a small program, without constant tests

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of struct pointers
    By simo_mon in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:34 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Replies: 3
    Last Post: 06-11-2002, 12:57 PM