|
Hello,
I would like to get the size of un array of strings that I pass as paramater to a function but I am not comfortable with pointers.
could you please tell me what I did wrong with this code?
extern const char* test[] = { "Data", "Data","Data","Data", "Data","Data", "Data", "Data"};
int MyFunc(const char* test[])
{
int size = sizeof(list) / sizeof(list[0]);
return size;
}
int sz = MyFunc(test) // return 1 instead of 8
|
|
|
|
|
C arrays are not arrays they are pointers, therefore there is no way to determine size.
led mike
|
|
|
|
|
Hi,
thanks for reply.
I'm afraid the problem is elsewhere.
If I bring const char* test[] into the function (no extern declaration) I get the right size.
I think that i do something wrong with the function's parameter.
Any idea?
int MyFunc(const char* test[])
{
const char* test[] = { "Data", "Data","Data","Data", "Data","Data", "Data", "Data"};
int size = sizeof(list) / sizeof(list[0]);
return size;
}
int sz = MyFunc(test) // It works , return 8
|
|
|
|
|
Arris7 wrote: int size = sizeof(list) / sizeof(list[0]);
Unless list is defined elsewhere, how are you not getting a compiler error here?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Sorry it is a wrong copy and paste. It is test instead of list.
|
|
|
|
|
the problem is that after you've 'passed' the array to a function, the function only sees a pointer. the function has no way to know it's an array of anything.
you have to pass the size along as a separate parameter.
|
|
|
|
|
hi,
Thanks for your help.
Actually I have several const arrays of strings with different sizes. So I would like to calculate the size at compilation time or run time.
I think to use enum but I have no clue about knowing how many items it contains at compilation time or run time.
Any other idea?
|
|
|
|
|
Arris7 wrote: Any other idea?
every time you pass a pointer to an array, pass the number of elements in the array, too.
or, use a vector of std::strings or a CStringArray of CStrings (or a vector of CStrings). that way, the vector/CStringArray can keep track of how many elements it has, and you can ask it, when you want to know.
|
|
|
|
|
What is sometimes done is to place a NULL as the last "string" in the array. That way, you can walk the array until you find an element with a value of NULL , and that indicates the end of the array.
Example:
LPCSTR g_cpaStrings[] = { "One", "Two", "Three", "Four", <code>NULL</code> };
DWORD dwIndex = 0;
while( g_cpaStrings[ dwIndex ] )
{
TRACE( "String: %s\n", g_cpaStrings[ dwIndex++ ] );
} Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
how does that work if you're passing g_cpaStrings into a function?
or, are you assuming g_cpaStrings is a global variable ?
|
|
|
|
|
It works the same, regardless of g_cpaStrings being global or even if it was cpaStrings as a local, or if it is used locally or passed to a function.
Passing it to a function causes it to decay from array to pointer in either case. But that only changes how the variable appears, not the data behind it.
LPCSTR g_cpaStrings[] = { "One", "Two", "Three", "Four", NULL };
void TestFunc1( LPCTSTR cpaStrings[] )
{
DWORD dwIndex = 0;
while( cpaStrings[ dwIndex ] )
{
TRACE( _T( "String1: %s\n" ), cpaStrings[ dwIndex++ ] );
}
return;
}
void TestFunc2( LPCTSTR *paStrings )
{
DWORD dwIndex = 0;
while( paStrings[ dwIndex ] )
{
TRACE( _T( "String2: %s\n" ), paStrings[ dwIndex++ ] );
}
return;
}
int main( int iArgC, char paArgV[] )
{
LPCSTR cpaStrings[] = { "Five", "Six", "Seven", "Eight", NULL };
DWORD dwIndex = 0;
while( g_cpaStrings[ dwIndex ] )
{
TRACE( _T( "String: %s\n" ), g_cpaStrings[ dwIndex++ ] );
}
dwIndex = 0;
while( cpaStrings[ dwIndex ] )
{
TRACE( _T( "String: %s\n" ), cpaStrings[ dwIndex++ ] );
}
TestFunc1( g_cpaStrings );
TestFunc1( cpaStrings );
TestFunc2( g_cpaStrings );
TestFunc2( cpaStrings );
return( 0 );
} Note that all things being equal, you solution of passing a count of valid data with all pointers is a better solution, and the one I usually suggest first. I am simply offering this solution as an alternative and to better allow the OP to understand how things work.
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
hmmm.
i asked because when i first tried your code with a function, i couldn't get it compile (char *[5] vs char*[] errors). but your code compiles fine.
i must've mistyped something somewhere and given up too easily.
|
|
|
|
|
No problem!
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Very clear and good explanation.
Thank you so much.
|
|
|
|
|
Could anyone tell me in FreeRTOS
I have two tasks.
TASK1
TASK2
I want to put TASK1 for sleep of 1000ms and in the sleeping time of TASK1 I want to run TASK2 when the time expired TASK1 again awakeup and so on.
-thx
|
|
|
|
|
ikbahrian wrote: Could anyone tell me in FreeRTOS
You do realize this is a forum for Windows development, don't you? Not that I mind you asking such a question, but just don't be surprised if you fail to get any help.
ikbahrian wrote: I want to put TASK1 for sleep of 1000ms...
Try the Sleep() function.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Sorry David.I was actually stuck with this prb so i post it.
|
|
|
|
|
Well, I don't know anything about FreeRTOS, but many RTOSs have the concept of blue and red threads. Red threads always preempts blue threads and usually you can set the priority between the red threads.
If FreeRTOS has this concept you could schedule TASK2 as a blue thread and TASK1 as a red thread that runs every 1000ms.
Just a shot in the dark, but sometimes something gets hit. :->
--
Roger
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
|
|
|
|
|
I want to put TASK1 for sleep of 1000ms and in the sleeping time of TASK1 I want to run TASK2 when the time expired TASK1 again awakeup and so on.
use WaitForSingleObject with wait time of 1000!, as using it handle you can break it before it completion...
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and you
|
|
|
|
|
I have an OO library that encapsulates the Win32 API. All the files are located in a shared folder for all other projects to use. One of the main files includes another file called "ClassType.h". This file defines a class name for your particular project that the OO library needs. The problem is that the ClassType.h is shared accross all other projects, and every time I build a project I need to edit ClassType.h to include the name of the class for a that particular project.
#ifndef CLASSTYPE_H
#define CLASSTYPE_H
#define ClassType CFoo // this name has to be edited every time I switch projects because the file is shared
#endif // CLASSTYPE_H
ClassType.h is in turn included by a shared file Called CApp.h
#include "ClassType.h"
Is there a way I can make Capp.h dynamically "detect" the current project directory so I can have a different ClassType.h file for every project?
As in:
#include "($Current_project_dir)\Source\ClassType.h" // something like that
|
|
|
|
|
Couldn't you have that define in each projects' stdafx.h?
|
|
|
|
|
I think that using quotes with #include instead of angle brackets causes the same directory and the file #include ing the file to be searched first. So if you just put the localized copy of that file in the same directory as the project-specific source files, it might just work for you.
There is a particular search order that #include uses depending on how the path and/or filename is specified.
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
I haven't tried this, by how about...
1) In your project folder, create a file named appname.h, and inside that, put #include "myapp.h"
2) In your shared folder includes, add #include "appname.h"
3) In your project settings "other include directories", make sure you have "." as your first entry, so it would look something like this:
.,../some_folder,../../some_other_folder
If it works, all you have to do is create the file in step one in all your projects...
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Hi all,
I have Derived a class CMyWnd from CWnd . Now I need to display a web page in
CMyWnd . How it possible?? . I dont want to use CHtmlView.
Thanks
George
|
|
|
|
|
You can host the Internet Explorer control to render your HTML, or use any of the lite HTML controls available (some available from this site, try searching for HTML Control ).
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|