returning pointers to structures

This is a discussion on returning pointers to structures within the C++ Programming forums, part of the General Programming Boards category; i realise many will think this is a stupid question but i having problems :S getting my head round pointers. ...

  1. #1
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33

    returning pointers to structures

    i realise many will think this is a stupid question but i having problems :S getting my head round pointers.

    In the simplest sense I am trying to return a pointer to a structure back from a function in which i initiate the structure as well as enter certain variables into its fields.

    A general example of the code involved in this, would be greatly appreciated.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    whipped up some code... there are probably typos and stuff but you'll get the general idea of simple pointer manipution, how to create new objects and setting struct attributes

    Code:
    #include<iostream>
    using namespace std;
    
    struct astruct
    {
         int attribute;
         char another_attribute;
         astruct *next;
    };
    
    //Function prototype:  function will accept an astruct pointer and will return an astruct pointer
    astruct* set_attributes(astruct*);
    
    int main()
    {
         //Create a variable that will hold the memory address of a "astruct" object
         astruct *my_struct;
    
         //Create a new object in memory, and assign its location into a pointer variable
         my_struct = new astruct;
    
         //Who knows what kind of garbage data these variables might contain     
         cout << "\n\nHere is the unititialized struct: " << endl;
         cout << my_struct->attribute << endl;
         cout << my_struct->another_attribute << endl;
         cout << my_struct->next << endl;
          
         //Pass in the my_struct pointer and have my_struct accept the function's return pointer
         my_struct = set_attributes(my_struct);
    
         cout << "\n\nHere is the struct after being intialized: " << endl;
         cout << my_struct->attribute << endl;
         cout << my_struct->another_attribute << endl;
         cout << my_struct->next << endl;
    
         return 0;
    }
    
    
    
    //Function Definition(s)
    
    astruct* set_attributes(astruct* temp_struct)
    {
         temp_struct->attribute = 42;
    
         temp_struct->another_attribute = 'K';
    
         temp_struct->next = new astruct;
    
         return temp_struct;
    }
    Last edited by The Brain; 06-21-2005 at 05:07 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    Thanks for the quick reply Brain, got it working now and 69.3% understood too

    cheers,
    Matt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  2. Noob trouble: returning strings (pointers to char)
    By SgtMuffles in forum C Programming
    Replies: 4
    Last Post: 02-28-2005, 08:48 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM
  5. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM

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