Struct/Array/Referencing Question

This is a discussion on Struct/Array/Referencing Question within the C Programming forums, part of the General Programming Boards category; I have a fairly basic question regarding Structs, Arrays, and I guess Field Referencing. I have an array of structs. ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    9

    Question Struct/Array/Referencing Question

    I have a fairly basic question regarding Structs, Arrays, and I guess Field Referencing.

    I have an array of structs. Each of those structs have 6 variables in them. I am trying to figure out how to store a struct in a temporary variable that I can change the values of without accessing the array each time.

    For example, my struct has a double value named hours_in_week.

    I have come to understand the following statement gives me a temporary copy of the struct, where I can access the variable values but not change them:
    Code:
    employee tempEmployee = employeeArray[s];
    I have also come to understand that this statement:
    Code:
    employeeArray[s].hours_in_week += totalHours;
    allows me to edit the value of the struct in the array, but this statement:
    Code:
    tempEmployee.hours_in_week += totalHours;
    doesn't allow me to change the value of the struct it represents.

    I would assume this is a matter of setting up pointers or something. A little guidance for future reference would be appreciated.


    **I apologize if my C lingo isn't the best, I am naturally a JAVA programmer**

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    When you do "tempEmployee.hours_in_week += totalHours" you're modifying the copy of the struct, not the original in your array. What you're looking for is something like:
    Code:
    employee *employee = &employeeArray[s];
    employee->hours_in_week += totalHours;
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    it should be changed.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by aussiemcgr View Post
    I have also come to understand that this statement:
    Code:
    employeeArray[s].hours_in_week += totalHours;
    allows me to edit the value of the struct in the array, but this statement:
    Code:
    tempEmployee.hours_in_week += totalHours;
    doesn't allow me to change the value of the struct it represents.
    First off, why would you want to work with a temporary structure? At some point you're just going to have to copy it back anyway, might as well just do it directly... Unless you're trying to implement an "undo" function, there's no reason you need a temporary struct.

    That said, the data in tempEmployee.hours_in_week should be changed. The only reason I can see for it not being changed is if you are using "employee tempEmployee = employeeArray[s];" multiple times... that would amount to making a new variable that clobbers the existing one, possibly causing memory leaks and stack issues in the process.

    Code:
    // at top of proggy
    employee tempEmployee;
    
    // intervening code
    
    tempEmployee = employeeArray[s];
    tempEmployee.hours_in_week += totalHours;
    Instantiate once, assign as often as you like.
    Last edited by CommonTater; 11-29-2010 at 10:55 PM.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    @CommonTater: Sometimes I think you completely skip over my posts, because so many times you ask questions I've already answered.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itsme86 View Post
    @CommonTater: Sometimes I think you completely skip over my posts, because so many times you ask questions I've already answered.
    I don't know what to say... I went back up to your post, read it, read mine, read yours, read mine.... I just don't see that.

    You showed him how to point to the struct in the array...
    I asked why he needed to copy it in the first place...

    How is that the same thing?

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    He doesn't need to copy it:
    Quote Originally Posted by aussiemcgr
    allow me to change the value of the struct it represents.

    I would assume this is a matter of setting up pointers or something.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21