Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;

typedef struct math
{
   string a;
   string b;
   int c;

   math()
   {
      c = 1000;
      printf("c = %d\n", c);
   }

}m3;

void print(struct math m1)
{
   cout<<"a : "<<m1.a<<endl;
   cout<<"b : "<<m1.b<<endl;
   cout<<"c : "<<m1.c<<endl;
}

void my_func(struct math &m1)
{
   m1.a.clear();
   m1.a = '\0';
   m1.b.clear();
   m1.b = '\0';
   m1.c = 0;
}

int main()
{
   math m1;
   m1.a = "OPEN";
   m1.b = "close";
   m1.c = 20;

   print(m1);

   my_func(m1);

   print(m1);

}


like same portion am using in application but am getting junk value after the
my_func
calls. how to restrict this type of case or any other method helps me????

What I have tried:

analysis are done but no use of it. help me if u can.
Posted
Updated 7-Mar-23 0:39am
Comments
Richard MacCutchan 7-Mar-23 6:35am    
I just tried your code and it works fine. Which compiler are you using?
Richard MacCutchan 7-Mar-23 6:41am    
You do not need a null character in a STL string type, as it controls the content by keeping a count of the contents. Placing the null character there just makes it a string with a length of 1 character.

1 solution

Your code produces the expected output (no junk) on my linux box:
c = 1000
a : OPEN
b : close
c : 20
a : 
b : 
c : 0

Note, I think you need neither
Quote:
m1.a = '\0';
nor
Quote:
m1.b = '\0';

In any case, you should use the debugger in order to see what happens on your system.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900