-3

What I want is, If data in array buff inside the thread changes, the global variable global_buff data must also change

#include <process.h>
.........

char global_buff_1[50];
char global_buff_2[50];

void thread (int x)
{
   char buff[50] = {0};
   if (x == 0)
      buff = global_buff_1;      //this is what i need, how can i equal two array correctly. i want to if buff array data changing the global_buff also changing.
   else
      buff = global_buff_2;
    .............
    //do some thing 
    .............
}

int main(int argc, char* argv [])
{
...................
int y = 0;
_beginthread((void(*)(void*))thread, 0, (void*)y);
.....................
}

any helping!

abdo.eng 2006210
  • 507
  • 2
  • 6
  • 12
  • 6
    I am really finding it hard to Understand what is your problem here. Can you frame your words in such a way, that it is understandable to us? –  Jul 18 '13 at 11:46
  • C or C++, not both. `buff = global_buff_2` How do you expect assigning a `char` to an array to work? – Roddy Jul 18 '13 at 11:46

2 Answers2

1
void thread (int x)
{
   char* buff = 0; // change here to a pointer 
   if (x == 0)
      buff = global_buff_1; // new these assignments work. And when changing buff
   else                     // global_buff_1 and global_buff_2 will change depending on
      buff = global_buff_2; // the assignment done here
    .............
    //do some thing 
    .............
}
RedX
  • 14,749
  • 1
  • 53
  • 76
0

You want to,

  • assign global_buff_1[] to buff[]
  • Reflect changes to buff[] array in global_buff_1[]

buff is a char array and array name can act as pointers too. global_buff_1 is a char array too. So you can't simply assign an array to another array, since they are addresses. If you need to copy the value from global_buff_1 to buff you need to do something like,

strcpy(buff,global_buff_1);

But this will create two separate arrays, with same values and won't satisfy your second requirement.

You can make use of pointers here as such,

char * buff;
buff = global_buff_1;

But still you have a single array accessible by two names.

Community
  • 1
  • 1
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59