Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6383531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:39:40+00:00 2026-05-25T02:39:40+00:00

#include<iostream> using namespace std; struct data { int x; data *ptr; }; int main()

  • 0
#include<iostream>
using namespace std;
struct data {
  int x;
  data *ptr;
};

int main() {
 int i = 0;
  while( i >=3 ) {
    data *pointer = new data;  // pointer points to the address of data
    pointer->ptr = pointer;    // ptr contains the address of pointer
    i++;
  }
 system("pause");
}

Let us assume after iterating 3 times :

 ptr had address = 100 after first loop
 ptr had address = 200 after second loop
 ptr had address = 300 after third loop

Now the questions are :

  1. Do all the three addresses that were being assigned to ptr exist in the memory after the program gets out of the loop ?
  2. If yes , what is the method to access these addresses after i get out of the loop ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:39:41+00:00Added an answer on May 25, 2026 at 2:39 am

    For starters, there will be no memory allocated for any ptr with the code you have.

    int i = 0;
    while( i >= 3)
    

    This will not enter the while loop at all.

    However, if you are looking to access the ptr contained inside the struct then you can try this. I am not sure what you are trying to achieve by assigning the ptr with its own struct object address. The program below will print the value of x and the address assigned to ptr.

    #include<iostream>
    using namespace std;
    struct data {
      int x;
      data *ptr;
    };
    
    int main() {
     int i = 0;
     data pointer[4];
      while( i <=3 ) {
        pointer[i].x = i;
        pointer[i].ptr = &pointer[i];  
        i++;
      }
    
    for( int i = 0; i <= 3; i++ )
    {
       cout<< pointer[i].x << endl;
       cout<< pointer[i].ptr << endl;
    }
    
    }
    

    OUTPUT:

    0
    0xbf834e98
    1
    0xbf834ea0
    2
    0xbf834ea8
    3
    0xbf834eb0
    

    Personally, when I know the number of iterations I want to do, I choose for loops and I use while only when I am looking to iterate unknown number of times before a logical expression is satisfied.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<iostream> using namespace std; struct sample { int data[3][2]; }; struct sample* function() {
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include <iostream> using namespace std; int main() { double u = 0; double w
#include <iostream> using namespace std; int main() { cout << !!!Hello World!!! << endl;
Example: #include <iostream> using namespace std; int main() { wchar_t en[] = LHello; wchar_t
#include <iostream> using namespace std; struct Node { int item; // storage for the
#include <iostream> using namespace std; struct Node { int item; // storage for the
#include <iostream> using namespace std; struct InvRec { int PartID; float Price; int Warehouse;
#include <iostream> using namespace std; struct node { int v; node* next; node (int
#include <iostream> #include <math.h> using namespace std; int main() { int arraylength; int lastbig

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.