Skip Some Arguments in a C++ Function

Ignoring parameters in C functions

The ulong parameters are the sizes of the buffer arguments. This is so the function knows how big those buffers are so it doesn't overflow them.

If you need the fileName argument, you must supply a correct fileNameBufferSize.

As for whether you actually can pass in a NULL pointer to the pointer arguments, only the documentation(or source code) for this function can tell you. Or if the documentation doesn't tell you, you'll have to do some basic science experiments on the function and see how it behaves.

Assuming the function accepts NULL pointers for parameters you don't want filled in, you'd likely pass 0 as the value for the ulong parameters.

You'll have to do:

unz_file_info pfile_info;
char *szFileName = malloc(1024);
uLong fileNameBufferSize = 1024;
if( szFileName == NULL) {
//handle error
return;
}

do {
int ret = unzGetCurrentFileInfo(zipFile, &pfile_info, szFileName, fileNameBufferSize, NULL, 0, NULL, 0);
NSLog(@"get info ret %i filename size %lu, filename %s", ret, pfile_info.size_filename, szFileName);
// do other stuff here with the filename
}
while (unzGoToNextFile(zipFile) == UNZ_OK);
free(szFileName);

You should also investigate the meaning of the return value of unzGetCurrentFileInfo. If it fails, it's unlikely you can use szFileName or any of the other arguments to the function - so don't call NSLog with those variables if the function fails

In this case, malloc seems unneccesary. Just use a local array, and drop the free() call.

char szFileName[1024];
uLong fileNameBufferSize = sizeof szFileName;

How to skip certain optional arguments in a library function

Those are nullable parameters, not optional. If you want to skip some of them, pass null as value.

client.ListSMSMessages("to", null, date, null, null);

For more details:

  • Nullable types in C#.
  • Named and optional args in C#.

If you want optional args, then create your own function over the one in the lib (building on Robert's answer:) ):

public SmsMessageResult MyListSmsMessages(
string to = null, string from = null, DateTime? dateSent = null, int? pageNumber = null, int? count = null)
{
return client.ListSMSMessages(to, from, date, pageNumber, count);
}

How to pass optional arguments to a method in C++?

Here is an example of passing mode as optional parameter

void myfunc(int blah, int mode = 0)
{
if (mode == 0)
do_something();
else
do_something_else();
}

you can call myfunc in both ways and both are valid

myfunc(10);     // Mode will be set to default 0
myfunc(10, 1); // Mode will be set to 1

Skip the number of words, located in a String, indicated by one parameter

First you need the return type to be a char pointer, then you need the array's len unless it is a null terminated string and then you can call strlen() to get the string's length,
to count the words simply count spaces that are followed by a none space char(begining of word)
BTW, i assume that by "word" you mean sequence of characters excluding spaces.. :

char* skipWords(char s[], int slen, int words)// OR (char* s, int words)
{
int i = 0;

if(slen > 0 && isalpha(s[0]))
words--;

for(i=0; i < slen - 1;i++)
{
if(words == 0)
{
return s + i;
}

if(isspace(s[i]) && isalpha(s[i+1]))
{
words--;
}
}

return NULL;
}

How can I force the use of a default parameter value in C# (skip optional arguments)?

Foo("Hello World", b: 6, d: 1.9f);


Related Topics



Leave a reply



Submit