![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
*operater and arrays
Ok the following code is being used to store an array of strings (being stored with memcpy). What is the * operater doing in the array declaration? I can't figure it out for the live of me! Is it just being used to declare 6000 elements?
char G_cMsgList[120*50]; this is the routine used to store the Msg! I understand common things but certain thing like "cTemp + 120" is beyond me! Why would memcpy be used to store the msg's array (G_cMsgList) in cTemp them use memcpy again to store the new msg in cTemp and then take cTemp and put it in G_cMsgList again! void PutLogList(char * cMsg)
{
char cTemp[120*50];
G_cMsgUpdated = TRUE;
ZeroMemory(cTemp, sizeof(cTemp));
memcpy((cTemp + 120), G_cMsgList, 120*49);
memcpy(cTemp, cMsg, strlen(cMsg));
memcpy(G_cMsgList, cTemp, 120*50);
}This is used to output the msg's (WM_PAINT). The main thing i don't understand is why the the declaration for a string is char * cMsg;. void OnPaint()
{
HDC hdc;
PAINTSTRUCT ps;
register short i;
char * cMsg;
hdc = BeginPaint(G_hWnd, &ps);
SetBkMode(hdc,TRANSPARENT);
for (i = 0; i < 35; i++) {
cMsg = (char *)(G_cMsgList + i*120);
TextOut(hdc, 5, 5 + 600 - i*16, cMsg, strlen(cMsg));
}
EndPaint(G_hWnd, &ps);The reason for all of this is because im writting a text based rpg (still) using Win32 and I want to paint text (ofcourse). 1 first msg (line) being at the bottom of the window and each time i want to output a new msg (line) it is placed at the bottom and everything else shifts up a line. The code you see performs this action.. but i would never rip it. I want to completely understand how it was done before i attempt it (re-written). Thanks
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 3
![]() |
>Is it just being used to declare 6000 elements?
Yes. And char * cMsg; is just a pointer to the text contained in the G_cMsgList array. [EDIT] memcpy((cTemp + 120), G_cMsgList, 120*49); memcpy(cTemp, cMsg, strlen(cMsg)); memcpy(G_cMsgList, cTemp, 120*50);
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me... Last edited by bivhitscar; Jun 22nd, 2006 at 10:19 PM. |
|
|
|
|
|
#3 |
|
Expert Programmer
|
but why char * cMsg why not char* cMsg or char *cMsg??? Or are they all the same?
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 3
![]() |
Yes, they are all the same.
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me... |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Btw it's written 'operator' not 'operater'.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#6 |
|
Expert Programmer
|
thanks nnxion i wrote these post last night after drinking a 6-pack. Anyone else have any insight about all of my other questions?
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#7 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
It isn't a matter of insight. Which question did you ask that you didn't see an answer for?
Quote:
memcpy((cTemp + 120), G_cMsgList, 120*49); memcpy(cTemp, cMsg, strlen(cMsg)); memcpy(G_cMsgList, cTemp, 120*50); Quote:
Okay?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
||
|
|
|
|
|
#8 |
|
Hobbyist
Join Date: Sep 2005
Posts: 259
Rep Power: 3
![]() |
Perhaps part of the question is why use memcpy when C++ provide several abstractions for such operations. My anwser would be: I wouldn't. The code posted is probably written in C.
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Oct 2005
Location: Melbourne, Australia
Posts: 126
Rep Power: 3
![]() |
>Anyone else have any insight about all of my other questions?
I hate repeating DaWei, but what did you ask that I didn't answer? ![]()
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me... |
|
|
|
|
|
#10 |
|
Expert Programmer
|
I guess they were anwsered! lol sorry I had more then i didn't write... so i should be able to perform the same output using a (string) vector correct?
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|