?
Solved

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

Posted on 2003-03-15
34
Medium Priority
?
210 Views
Last Modified: 2010-04-15
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
Comment
Question by:tapasmondal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 22
  • 12
34 Comments
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142014
is data pointing to a double array?
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142023
no this is a single array
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142026
is data pointing to a double array?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:tapasmondal
ID: 8142029
no this is a single array
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142030
I mean to say r u passing the name of an array of type double as an argument to this function?
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142033
no this is a single array
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142037
yes u r right
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142042
yes u r right
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142044
yes u r right
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142046
pls try to assign like this

data[i] = (double)i;
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142047
yes u r right
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142053
actually i did this.
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142055
when i debugging this it is ok. when i print this it is wrong.
tapas
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142058
when i debugging this it is ok. when i print this it is wrong.
tapas
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142059
so there is no problem in ur code. pls send me some more line of ur code.
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142060
when i debugging this it is ok. when i print this it is wrong.
tapas
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142063
when i debugging this it is ok. when i print this it is wrong.
tapas
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142069
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
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142070
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
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142077
not this one pls send me how you decalre array, how u call the function..

0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142078
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
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142084
main()
{
   double *data;
   data=new double[N];
   func(data);
}
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142085
There is no error in ur code, it may happen while printing only.
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142089
main()
{
   double *data;
   data=new double[N];
   func(data);
}
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142092
main()
{
   double *data;
   data=new double[N];
   func(data);
}
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142094
pls change ur code

data=new double[N];

and try this

data = (double *) malloc(N*8);
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142096
Exactly but how can i solve it. this hamper my result
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142104
or try int or long array inseted of double, as u have int values to store
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142107
i have suggested malloc insted of new operator because compare to new, malloc performs batter
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142111
" data = (double *) malloc(N*8); " i used this but same problem arise.
0
 
LVL 1

Accepted Solution

by:
umangjoshi earned 200 total points
ID: 8142123
Pls try int or long, i have no more idea
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142139
" data = (double *) malloc(N*8); " i used this but same problem arise.
0
 
LVL 1

Author Comment

by:tapasmondal
ID: 8142145
thanks i solve this problem using int
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8142151
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question