|
Hello all... how do I create array of char* . At the moment I have hardcoded the number of instances (large enough to hold tokens) but this should be generic. Example
int temp_array[2] = {0};
char* token1;
char* token2; As you can see, I might not know how many tokens I need at run time. I want something like this
char* [] tokens; How could I fix this? Thanks
modified 12-May-17 6:00am.
|
|
|
|
|
Create a variable for the array that is initially empty and a variable holding the size:
char **tokens = NULL;
unsigned tokenSize = 0;
Once you know the required number of items, allocate the array. How to do that depends if you are using C or C++:
tokenSize = items_to_store_in_array;
tokens = new char *[tokenSize];
tokens = (char **)malloc(sizeof(char*) * tokenSize);
If you need to change the size use realloc with C.
With C++ you should then avoid arrays and use a container like vector - C++ Reference[^] instead.
|
|
|
|
|
Jochen Arndt wrote: How to do that depends if you are using C or C++:
Well I should have mentioned that earlier..... I am using C. Can you please edit your question (or append the code for C)
|
|
|
|
|
The C code is there (the malloc call).
Use something like this (for two tokens):
tokenSize = 2;
tokens = (char **)malloc(sizeof(char*) * tokenSize);
tokens[0] = token1;
tokens[1] = token2;
free(tokens);
tokens = NULL;
tokenSize = 0;
|
|
|
|
|
Only addition to Jochens answer is to remove confusion to you typedef the char* pointer
ptrCount = 2;
typedef char* CHARPTR;
CHARPTR* charPtrArray = (CHARPTR*)malloc(sizeof(CHARPTR)*ptrcount);
Now it looks like any other array
The confusion exists because char** can be a couple of things in C you can't distinguish them without looking at the initialization code
1.) A pointer to a char* (always true)
2.) An array of char* (if initialized as such)
3.) A multidimensional char array (if initialized as such)
Number 3 in particular usually causes mass confusion with students learning C.
The typedef doesn't stop you assuming the wrong thing but it does give you language cues to what it is and is easier to read.
In vino veritas
modified 14-May-17 22:51pm.
|
|
|
|
|
std::vactor<char*> m_myArrayOfString ;
m_myArrayOfString.push_back(new char[17]);
...
...
in the end loop through vector and delete them one by one
for(auto ptr : m_myArrayOfString)
delete [] ptr ;
m_myArrayOfString.clear();
huzifa
|
|
|
|
|
Think of an array of chars as a 2d map.
So along the bottom the x axis can be considdered as an array of char pointers, each one pointing to a vertical array of chars going up in the y direction.
So point 0,0 is a char**
You can of course extend this to a 3 d map, with char***
Any point on the 2d map can be referenced by data[x][y]
SO first you alloc how many you need in the x direction: char** data = alloc(xMax)
Now each vertical collum can be alloced as data[0] = alloc(y1Max) data[1] = alloc(y2Max)
And so on.
|
|
|
|
|
the following are the related code,could someone can help me,
void CXXXStatic::PreSubclassWindow()
{
DWORD dwStyle = GetStyle();
SetWindowLong(GetSafeHwnd(),GWL_STYLE,dwStyle | SS_OWNERDRAW);
CStatic::PreSubclassWindow();
}
void CXXXStatic::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
}
CXXXStatic m_Static;
m_Static.Create("hello", WS_CHILD|WS_VISIBLE|BS_OWNERDRAW, CRect(100, 100, 200, 200), this,0x0301);
|
|
|
|
|
That is because SS_OWNERDRAW is not a bit value that can be O-red to the style but a masked value.
Use
SetWindowLong(GetSafeHwnd(),GWL_STYLE,( dwStyle & ~SS_TYPEMASK) | SS_OWNERDRAW); instead.
Or better:
ModifyStyle(SS_TYPEMASK, SS_OWNERDRAW));
|
|
|
|
|
Hello all. I am trying to re-use a file pointer at the start of the program and then close this when program is shutting down, after some work with it. So far I have come up with this
globals.h
=========
extern FILE* ptrLogFile;
file1.c
=======
#include "globals.h"
FILE* ptrLogFile = fopen("RequestsLog.log", "a+");
file2.c
========
#include "globals.h"
FILE* ptrLogFile = fopen("RequestsLog.log", "a+");
file3.c
========
#include "globals.h"
fclose(ptrLogFile);
But this is not what I am looking for. I want to initialize, using fopen() , the ptrLogFile just once (may be in globals.h ?) and then re-use it in file1.c and file2.c as long as I want. Finally close it in file3.c when the program is shutting down. How do I do this? Thanks
modified 9-May-17 9:17am.
|
|
|
|
|
You have to define and initialise the global variable within one source file:
FILE* ptrLogFile = NULL;
Then use it anywhere this way:
#include "globals.h"
if (NULL == ptrLogFile)
ptrLogFile = fopen("RequestsLog.log", "a+");
#include "globals.h"
if (NULL == ptrLogFile)
ptrLogFile = fopen("RequestsLog.log", "a+");
#include "globals.h"
if (NULL != ptrLogFile)
{
fclose(ptrLogFile);
ptrLogFile = NULL;
}
|
|
|
|
|
Thanks. I got it working. But the log file is not getting updated until I close my program. How do I update the log file even when the program is running?
|
|
|
|
|
Call fflush[^] after writing to the stream.
|
|
|
|
|
Don't do it that way. Create a function or library module that does all the work and then just call the actual logging functions anywhere in your code. Call the initialiser, to open the file, at the beginning of your main module, and the closing operation just before your main program terminates.
|
|
|
|
|
Here's how I deal with global variables. I have one header file, I call Globals.h, with these definitions :
#ifdef DEFINE_GLOBALS
#define Global
#define GlobalVar(Type,Variable,Value) Type Variable=Value
#else
#define Global extern
#define GlobalVar(Type,Variable,Value) extern Type Variable
#endif
Then, in one and ONLY one file do this :
#define DEFINE_GLOBALS
#include "include "Globals.h"
#endif
before I include headers that define variables.
Here are some sample definitions :
typedef FILE * PFILE;
GlobalVar( PFILE, PtrLogFile, NULL );
GlobalVar( size_t, LineCount, 0 );
|
|
|
|
|
Hi
basically my problem is how to get rid of flickering when i use customdraw.
I know about double bufering, bckgnderase and any other solutions, but all of them do not work in my case, because my customdraw is called for each item (when resizing window(mfc behavior)) and for some of them when multiselection happens. CustomDraw is called from OnNotify(need that because i am extending normal tree with columns like in listView - so it look smth like Grid with expandable rows).
Cause of all that behavior that i need, got no idea how to apply double buffering into this, cause there's none notification when to bitblt offscreen bitmap into screen.
Would appreciate any ideas.
|
|
|
|
|
|
Richard MacCutchan wrote: See WM_SETREDRAW message (Windows)[^].
Same problem like with bitblt there's no proper place to call that one. MFC calls NM_CUSTOMDRAW for each element. So i've got no control over this "message loop".
|
|
|
|
|
|
|
Actually using this one as base. Anyway thx for reply.
|
|
|
|
|
There double buffering technique that is fairly trivial to implement and gives reasonable results for any complex drawing
1.) Your draw routines only draw on a memory context, don't draw to the screen at all in those item draw calls.
2.) You use a standard windows timer which will timeout after a set time and you use that to update the screen.
3.) Anytime you draw to the memory context in step 1 you kill the running any timer and restart it
4.) The timer running out posts off a message and if you use that message to transfer the memory DC to screen, you then dispose of the timer.
You should be able to get what happens that each item simply draws itself to the memory DC and will restart the timer. When the last item has been drawn the timer will finally hit it's wait timeout and post off the message which will draw all the items together as one big bitblt draw and no flickering.
The requirements 1 memory context (created when window is created), 1 timer which is created and disposed as per above.
In vino veritas
|
|
|
|
|
Very interesting idea, will check it now and tell if it work's for me. Thanks.
|
|
|
|
|
So i've found answer to my problem. I think that i got best working solution that's : Fast, easy and makes ALL flickering dissapear.
So the trick is to set Ctrl's window Exstyle WS_EX_COMPOSITED and the same time handle ON_WM_ERASEBKGND() (just return true or false doesn't metter from what i've observed) and that do the trick.
|
|
|
|
|
You should return true (being a non zero value) otherwise window will remain marked for erasing which somewhere down the track may come back to bite you. Remember the API underworkings aren't fixed you may find an old version of Windows or a new update which decides to use the flag.
In vino veritas
|
|
|
|