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 8852305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:21:57+00:00 2026-06-14T13:21:57+00:00

So I’ve thoroughly gone through the topics by searching this site but I couldn’t

  • 0

So I’ve thoroughly gone through the topics by searching this site but I couldn’t find something which can help me.

I’m making a task for my friend and let me tell you that it’s been a year or more since I’ve used C++.

So the problem I’m facing is with pointers and returning struct through pointer.

Here’s the code below:

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

    struct student
   {
    int SUB[3];
   };

   struct AVGPCT
   {
    float avg;
      float pct;
   };


AVGPCT* CALC(student *A)
{
   int x,y;
    AVGPCT D[5]={0.0,0.0,0.0,0.0,0.0};

   for(x=0;x<5;x++)
   {
    for(y=0;y<3;y++)
      {
        D[x].avg+=A[x].SUB[y];
         D[x].pct+=A[x].SUB[y];
      }
   }

   for(x=0;x<5;x++)
   {
    D[x].avg/=2;
      D[x].pct/=300;
   }
   return (D);
}

void main(void)
{
   int i,j;
   student S[5];
   AVGPCT *V;

   for(i=0;i<5;i++)
   {
    for(j=0;j<3;j++)
      {
        cout << "Enter Marks Of Subject[" << (j+1) << "] Of Student [" << (i+1) <<"] [Max=100]:";
        cin >> S[i].SUB[j];
      }
   }

   V = CALC(S);
   cout << "Average Number Of Student [1]: " << V->avg;
   cout << (*V).pct;

   getch();
}

Now the problem is, it is giving me a hard time to retrieve correct values from the location.

I’m trying to increment the pointer with V++. The funny thing is it is printing the values correctly if I use:

cout << V->avg;
cout << V->pct;
V++;
cout << V->avg;
cout << V->pct;

And so on. But if I use loop or write even a single line or use endl or \n, the values jumble up and gibberish values appear.

So what could be going wrong here?

It is important that I use struct and also please guide according to the code.

  • 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-06-14T13:21:58+00:00Added an answer on June 14, 2026 at 1:21 pm

    Looks like this line

    AVGPCT D[5]={0.0,0.0,0.0,0.0,0.0};
    

    declares the array D locally to the function CALC()

    You pass a pointer to that array back to the main function, but the array is marked as free game for overwriting when the function ends. The more stuff you do in the program, the more chance there is of this location in memory being overwritten, though it’s hard to say when that will happen. Hence, sometimes you get away with it, other times you don’t.

    At the risk of giving you advice, it’s probably better to declare D somewhere in main and pass a pointer to it into the function or use a new or malloc inside the function and pass the resultant pointer back.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.