pointers again

This is a discussion on pointers again within the C++ Programming forums, part of the General Programming Boards category; can you store like variables in pointers?...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    pointers again

    can you store like variables in pointers?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    What do you mean like variables. Pointers are basically variables that hold memory addresses.
    Woop?

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    i mean like could you hold a variable like
    int variable;
    int variable2;
    int variable 3;
    inside of 1 pointer?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    No.
    You could hold all of them in one pointer one at a time but not at the same time.
    Woop?

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    well could i put the variables into structures and use pointers to call the structure?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    int array[3];
    array[0] = variable;
    array[1] = variable2;
    array[2] = variable3;
    If you passed array to a function they would share a common pointer. Dynamic arrays also share a pointer.

    On the other hand
    int variable, variable2, variable3 = 42; // or something else
    int *pointer = variable;

    Right now the pointer points to variable, but it can always be reassigned.

    > well could i put the variables into structures and use pointers to call the structure?
    I'm going to say yes you can, but the way you phrased that is a little weird.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    ya so it can?

    how would i?
    int *pointer = struct variable
    ?

    im asking because my code is getting to repetitive and annoying

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Code:
    #include <stdlib.h>
    
    struct bucket {
       int a;
       int b;
       int c;
    };
    
    int main(void) {
       struct bucket *on_heap = malloc(sizeof(struct bucket));
       on_heap->a = 42; /* for example, here we are initializing */
       on_heap->b = 27;
       on_heap->c = 4;
    
       on_heap->a = 0; /* but they can be changed at any time */
    
       /* at some point free it */
       free(on_heap);
       return 0;
    };

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    > im asking because my code is getting to repetitive and annoying
    Very descriptive

    What exactly do you "like" want to do?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    for my text rpg after each turn it allows you to view your stats ,so i have keep writing it down all like 8 lines after the 80th time it gets to repetitive...so if i could just put the imformation in a struct and use the pointer to the the struct it makes 8 lines just 1

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    It's a pretty tall order to make it into one line. This is more of a design problem, which the structure thing can help you in. If you are clever you can make functions to automate common things that will happen to your characters:

    For example if I took my bucket:
    void decrease_values(struct bucket ** me);
    void increase_values(struct bucket ** me);

    Where in those functions, you do what took you eight lines. But since we're in the C++ forum, make characters a class, with methods and everything.

    God. I had wished I knew what we were doing before.
    Last edited by whiteflags; 07-05-2006 at 09:41 PM. Reason: Wow. I'm in the C++ forum now.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,578
    A structure would not "like" help you decreasing much the number of lines. You would still have to output each of the members.

    Use functions and the struct, like citizen suggested because it organizes your code. Don't do it because it "like" makes you write less "lines".
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    i swear to god if one more person does the "like"thing,i only did it once or twice

    and how would i limit down my code then

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    > and how would i limit down my code then
    You need to learn the 80/20 rule. One application is, "If your program does anything useful 80 lines of code accounts for 20% of what it does." You can not apply this literally, but as we've been saying you can not expect every application or game to work in <100 lines of code. In fact, most don't.

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,578
    It was answered already. You use functions for those tasks that you repeat often.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 11:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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