Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Value of a array element has been changed!!!!!!!!

Hi Friends,

i am facing a problem regarding pointer. i am using a function with one one argument. like this:

void func(double *data)
{
      for(i=0;i<N;i++)
          data[i]=i;
      for(i=0;i<N;i++)
          printf("\n%lf",data[i]);
}

when i am debuging i have seen that the value of a particular location is right but when i print this value the value has been changed automatically.

pls help me.


tapas
0
tapasmondal
Asked:
tapasmondal
  • 22
  • 12
1 Solution
 
umangjoshiCommented:
is data pointing to a double array?
0
 
tapasmondalAuthor Commented:
no this is a single array
0
 
umangjoshiCommented:
is data pointing to a double array?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
tapasmondalAuthor Commented:
no this is a single array
0
 
umangjoshiCommented:
I mean to say r u passing the name of an array of type double as an argument to this function?
0
 
tapasmondalAuthor Commented:
no this is a single array
0
 
tapasmondalAuthor Commented:
yes u r right
0
 
tapasmondalAuthor Commented:
yes u r right
0
 
tapasmondalAuthor Commented:
yes u r right
0
 
umangjoshiCommented:
pls try to assign like this

data[i] = (double)i;
0
 
tapasmondalAuthor Commented:
yes u r right
0
 
tapasmondalAuthor Commented:
actually i did this.
0
 
tapasmondalAuthor Commented:
when i debugging this it is ok. when i print this it is wrong.
tapas
0
 
tapasmondalAuthor Commented:
when i debugging this it is ok. when i print this it is wrong.
tapas
0
 
umangjoshiCommented:
so there is no problem in ur code. pls send me some more line of ur code.
0
 
tapasmondalAuthor Commented:
when i debugging this it is ok. when i print this it is wrong.
tapas
0
 
tapasmondalAuthor Commented:
when i debugging this it is ok. when i print this it is wrong.
tapas
0
 
tapasmondalAuthor Commented:
void func(double *data)
{

     for(i=0;i<N;i++)
     {
 
         data[i]=i;
     for(i=0;i<N;i++)
         printf("\n%lf",data[i]);
}
 


0
 
tapasmondalAuthor Commented:
void func(double *data)
{

     for(i=0;i<N;i++)
     {
 
         data[i]=i;
     for(i=0;i<N;i++)
         printf("\n%lf",data[i]);
}
 


0
 
umangjoshiCommented:
not this one pls send me how you decalre array, how u call the function..

0
 
tapasmondalAuthor Commented:
void func(double *data)
{
  unsigned char bit,x;
    for(i=0;i<N;i++)
    {
        bit=0,bitwcnt=0;
        while(bit.wcnt!=4)
        {
            x=Bit_Manipulation();
           bit=bit<<1;
           bit=x|bit;
           bitwcnt++;
         }
         
          Data[i]=bit;
     }
    for(i=0;i<N;i++)
        printf("\n%lf",data[i]);
}

0
 
tapasmondalAuthor Commented:
main()
{
   double *data;
   data=new double[N];
   func(data);
}
0
 
umangjoshiCommented:
There is no error in ur code, it may happen while printing only.
0
 
tapasmondalAuthor Commented:
main()
{
   double *data;
   data=new double[N];
   func(data);
}
0
 
tapasmondalAuthor Commented:
main()
{
   double *data;
   data=new double[N];
   func(data);
}
0
 
umangjoshiCommented:
pls change ur code

data=new double[N];

and try this

data = (double *) malloc(N*8);
0
 
tapasmondalAuthor Commented:
Exactly but how can i solve it. this hamper my result
0
 
umangjoshiCommented:
or try int or long array inseted of double, as u have int values to store
0
 
umangjoshiCommented:
i have suggested malloc insted of new operator because compare to new, malloc performs batter
0
 
tapasmondalAuthor Commented:
" data = (double *) malloc(N*8); " i used this but same problem arise.
0
 
umangjoshiCommented:
Pls try int or long, i have no more idea
0
 
tapasmondalAuthor Commented:
" data = (double *) malloc(N*8); " i used this but same problem arise.
0
 
tapasmondalAuthor Commented:
thanks i solve this problem using int
0
 
umangjoshiCommented:
In case of int or long or unsigned there is a fixed formula is used to store a number in 2 or 4 bytes, but in case of double or float it uses natural log (something like taht) to store a number so it might have some problem. it can't store the exact number.

you can test is the above theory...

double d = 2.0;

if (d == 2.0)
{
  printf("same");
}
else
{
  printf("not same");
}

----------
the above may return the correct result
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 22
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now