pointer question (should be easy to ans)

OK background: I have a struct called client, and I have an array of them and a single instance of it separate from the array. So I have code like:

CLIENT clientList[MAX_CLIENTS]; //GLOBAL VARIABLE

1
2
3
4
5
6
CLIENT myClient;
myClient.field = var;
login(*myClient);
if(myClient.logged){
  printf("myClient is logged in");
}


1
2
3
4
login(CLIENT *thisClient){
  clientList[p].logged = true;
  clientList[p].field = &thisClient.field;
  thisClient = clientList[p];


So in words what I'm trying to do is copy some of the info from the passed client to info in the list, then make the client that was passed in equal to the client in the list. There are two problems, when setting the field from the passed in client to the client in the list I'm not doing that right (damn pointers). Then I try to set the one that was passed in to point to the one in the list so that back in the main function I'm no longer accessing the one that was passed in but the one in the list.

Does that make sense? Any ideas?


ok when trying to get at thisClient.field (DUH) use thisClient->field

Still can't figure out how to set the passed in pointer to point at the new client in the list....
ok i've changed it around some so that I have:
1
2
3
4
5
6
CLIENT myClient;
CLIENT *cpoint;
login(*cpoint);
  if(myClient.logged){
    printf("myClient is logged in");
  }

1
2
3
4
5
login(CLIENT *thisCP){
  clientList[p].logged = true;
  clientlist[p].field = thisCP->otherField;
  thisCP = &clientList[p];
}


but it's not working. What I mean is, after that returns, the if statement is not ran (logged in variable is still false).
1
2
3
4
5
6
CLIENT myClient;
myClient.field = var;
login(*myClient);
if(myClient.logged){
  printf("myClient is logged in");
}

should be
1
2
3
4
5
6
CLIENT myClient;
myClient.field = var;
login(&myClient);
if(myClient.logged){
  printf("myClient is logged in");
}
Last edited on
also you can change it like this
1
2
3
4
5
6
CLIENT myClient;
CLIENT *cpoint;
login(cpoint);
  if(myClient.logged){
    printf("myClient is logged in");
  }

1
2
3
4
login(CLIENT *thisCP){
  clientList[p].logged = true;
  clientlist[p].field = thisCP->otherField;
  thisCP = &clientList[p];
if it can't work well, tell me.
Last edited on
You would need to instantiate the cpoint object.

1
2
3
CLIENT *cpoint = new CLIENT();
thisCP->otherfield = "something";
login(cpoint);


1
2
3
4
5
6
login(CLIENT *thisCP) {
clientList[p].logged = true;
  clientlist[p].field = thisCP->otherField;
  delete thisCP; // Free memory from your new() !
  thisCP = &clientList[p];
}


And for the record. You should use a vector instead of an array when storing classes. A Vector or Pointers would be ideal in this situation.
Last edited on
And you should probably change your structure name from CLIENT to Client, so it's easier to understand :)
enixi0s: That really comes down to the naming convention he/she is using.

I use Hungarian so my struct would start with an S (e.g SClient). CClient for a class etc.
OK well I had this working for awhile, now it doesn't work and I don't even know why... At any rate, I added the delete cp; and it didn't work. debug assertion fails and it crashes. Dunno bout dat.

OK in trying to do some debugging, I did this:
1
2
3
4
5
6
7
8
Server (Server): Server has most same functi
client 5963104 sent send all hello
client 5963104 sent login mike mike
c 3748576 p 4876184
c 4876184 p 4876184
Server (all):  mike logged in successfully!
client 5963104 sent send all hello


That's the output on the server. whenever anything is sent it prints c with:
printf("client %d sent %s\n",c,message);
and from within login function before and after it's changed:
printf("c %d p %d\n",c,&clientList[p]);

So you can see that it isn't changed for the function that called login, while it works fine for the actual log in function.
I hate to be one of 'those' posters but im running into a deadline. (midnight tonight actually).

i don't understand how this is giving me trouble now when i had it working previously, but i'm uber frustrated. :/

By the way, I meant to thank all of you who already posted. I love the forums here, they are very helpful and people posting usually know what's up :) Thanks again everyone.
Hmm are you using MSVC++ 2003-2008? If so, you don't need the delete. I believe the GC will handle memory deallocation for you.
closed account (z05DSL3A)
Zaita Wrote:
> Hmm are you using MSVC++ 2003-2008? If so, you don't need the delete. I
> believe the GC will handle memory deallocation for you.

GC is .net so unless the OP is doing an CLR project with C++/CLI or is mixing managed and unmanaged code then GC is not present. GC is relevant to managed code objects.
Grey: yea. 2003 is the start of the .NET era. If Universities over there are anything like the ones here then it will be .NET based that they are trying to use. Fortunately, I have stayed away from C++ in .NET because I am not a fan of Microsoft's compiler.

I was addressing why the "delete pClient;" would be causing a crash. It seems this persons deadline for their assignment has passed though. So whatever answer we come up with is going to be useless :P
Topic archived. No new replies allowed.