Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   *operater and arrays (http://www.programmingforums.org/showthread.php?t=10496)

Kilo Jun 22nd, 2006 9:43 PM

*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

bivhitscar Jun 22nd, 2006 10:05 PM

>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);

Copies the first 5880 elements in G_cMsgList into the cTemp array - starting from cTemp[120]. Then it copies the cMsg (current message?) into the start of cTemp. Now cTemp contains the new message, followed by the original first 49 entries in G_cMsgList. Then we copy that data back into the G_cMsgList array. So, basically, it just shifts the oldest message out and the newest message into the beginning of the array.

Kilo Jun 22nd, 2006 10:09 PM

but why char * cMsg why not char* cMsg or char *cMsg??? Or are they all the same?

bivhitscar Jun 22nd, 2006 10:16 PM

Yes, they are all the same.

nnxion Jun 23rd, 2006 8:19 AM

Btw it's written 'operator' not 'operater'.

Kilo Jun 23rd, 2006 10:18 AM

thanks nnxion i wrote these post last night after drinking a 6-pack. Anyone else have any insight about all of my other questions?

DaWei Jun 23rd, 2006 10:32 AM

It isn't a matter of insight. Which question did you ask that you didn't see an answer for?
Quote:

What is the * operater doing in the array declaration?
It's probably to remind the coder that the total number of elements declared represents a partitioning of m x n.
:

        memcpy((cTemp + 120), G_cMsgList, 120*49);
        memcpy(cTemp, cMsg, strlen(cMsg));
        memcpy(G_cMsgList, cTemp, 120*50);

The first statement moves the message list into the temp area 120 bytes down from the beginning. The second statement moves cMessage to the beginning of the temp area (before the old message list). The third statement replaces the old message list with the new arrangement. This puts new messages at the front of the list, not at the rear. Capish?
Quote:

i don't understand is why the the declaration for a string is char * cMsg;.
As mentioned above, char *thangy, char* thangy, and char * thangy are equivalent.

Okay?

Cache Jun 23rd, 2006 12:06 PM

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.

bivhitscar Jun 24th, 2006 3:57 AM

>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? :confused:

Kilo Jun 24th, 2006 10:32 PM

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?


All times are GMT -5. The time now is 12:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC