pointer to a structure?

This is a discussion on pointer to a structure? within the C++ Programming forums, part of the General Programming Boards category; I need a poniter to each variable for the structure but it seems because i have made a loop around ...

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

    pointer to a structure?

    I need a poniter to each variable for the structure but it seems because i have made a loop around the stuct it won't let me pass any variables outside of the loop. ( i hope i have made sence), is there a way to pass variables outside of the loop using pointers? or is there another way?.





    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    
    struct product
    {
    int item;
    char des[256];
    int qty;
    };
    
    
    int main(int itemN, int qtyN, char ans, int *b)
    {
    
    
    do {
    
    char items[256];
    
    
    
    
    cout<<"\nPlease enter the item description > ";
    cin.getline(items,256);
    cout<<"Please enter the item number >";
    cin>> itemN;
    cout<<"please enter the quanity counted >";
    cin>> qtyN;
    
    product descrip;
    product *ptr;
    
    descrip.item=itemN;
    descrip.qty=qtyN;
    descrip.des=items;
    ptr=&descrip;
    
    cout<<"\nThis is the data you have entered\n";
    cout<<"\nItem description > "<< descrip.des;
    cout<<"\nItem > "<< descrip.item;
    cout<<"\nQuantity > "<< descrip.qty <<"\n";
    cout<<"\nIs this correct (Y/N) > ";
    cin>> ans;
    
    cin.get();
    
    cout<< ptr;
    
    } while ( ans=='n' );
    
    
    ofstream a_file;
    a_file.open("stores.dat",ios::app);
    
    
     cin.get();
      return 0;
    
    }
    WhAtHA hell Is GoInG ON

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    A variable you declare inside of a loop exists only inside the loop.

    If you want every product object to exist after the loop is done you'll need to store them somewhere -- possibly an array or vector or map of products. Make sure you declare the container before the loop, and then insert each entry into the container inside the loop.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by Cat
    A variable you declare inside of a loop exists only inside the loop.

    If you want every product object to exist after the loop is done you'll need to store them somewhere -- possibly an array or vector or map of products. Make sure you declare the container before the loop, and then insert each entry into the container inside the loop.
    Ok, well how do i store a memory address in an array?
    WhAtHA hell Is GoInG ON

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Define your variables outside of the loop. Because they're defined in the loop, they only exist in the scope of the loop. When you leave the scope, the variables don't exist anymore. This is one of the reasons you're taught to define your variables at the top of a function.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by SlyMaelstrom
    Define your variables outside of the loop. Because they're defined in the loop, they only exist in the scope of the loop. When you leave the scope, the variables don't exist anymore. This is one of the reasons you're taught to define your variables at the top of a function.
    ok, but if i declare my variables before the loop how do i go back to change them if they are wrong?
    WhAtHA hell Is GoInG ON

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    You can still assign to the variables in the loop all you want. That has nothing to do with memory, as far as your concerned. Unless you're talking about resizing a dynamically allocated array, in which case you'd declare the pointer outside of the loop, and inside you'd both delete and reallocate it all you want.

    Off topic, since I didn't really look at your code the first time around:
    Code:
    int main(int itemN, int qtyN, char ans, int *b)
    main is not a function you can pass arguments to like that... the only time it accepts arguments are if they're through the command line, which these aren't. Like the other variables we've named, you'll want to define those at the beginning of the main function.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    142
    Quote Originally Posted by SlyMaelstrom
    You can still assign to the variables in the loop all you want. That has nothing to do with memory, as far as your concerned. Unless you're talking about resizing a dynamically allocated array, in which case you'd declare the pointer outside of the loop, and inside you'd both delete and reallocate it all you want.

    Off topic, since I didn't really look at your code the first time around:
    Code:
    int main(int itemN, int qtyN, char ans, int *b)
    main is not a function you can pass arguments to like that... the only time it accepts arguments are if they're through the command line, which these aren't. Like the other variables we've named, you'll want to define those at the beginning of the main function.
    yea ok, i understand the first part

    (after) oh, now i do see what you are saying, aswell as moving those variable (i hope my terminology is right)
    Last edited by wart101; 12-01-2006 at 05:04 AM.
    WhAtHA hell Is GoInG ON

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. method returning a pointer to a structure
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2009, 10:01 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 07:59 AM
  4. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 05:35 PM
  5. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 06:54 AM

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