"strnset" is standard-c, (visual c++) and should work in objective-c.
But it doesn't recognize it.
What library am I missing? I have tried: stdio.h and string.h... both don't do it.
what would work in its place where I am given a char array and I want to create: n * characters to be placed in it?
example: _strnset(data, '8', 12); will yield -> data = "888888888888"
Use memset() instead. strnset is not part of the standard "C" library.
What property does strset have over memset that you are interested in?
NAME
memset -- fill a byte string with a byte value
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <string.h>
void *
memset(void *b, int c, size_t len);
DESCRIPTION
The memset() function writes len bytes of value c (converted to an
unsigned char) to the byte string b.
RETURN VALUES
The memset() function returns its first argument.
strnset is actually not a standard C function. It's not present in the GCC headers.
Related
Does
if(strncmp(buf, buf2, 7) == 0)
do the same thing as
if(memcmp(buf, buf2, 7) == 0)
buf and buf2 are char* arrays or similar.
I was going to append this to another question but then decided perhaps it was better to post it separately. Presumably the answer is either a trivial "yes" or if not then what is the difference?
(I found these functions from online documentation, but wasn't sure about strncmp because the documentation was slightly unclear.)
Like strcmp(), strncmp() is for comparing strings, therefore it stops comparing when it finds a string terminator in at least one argument. Any differences past that point have no effect on the result. strncmp() differs in that it will also stop comparing after the specified number of bytes if it does not encounter a terminator before then.
memcmp(), on the other hand, is for comparing blocks of random memory. It compares up to the specified number of bytes from each block until it finds a difference, regardless of the values of the bytes. That is, it does not stop at string terminators.
In C and C++ the end of a string is indicated by a byte with value 0.
The function memcmp does not care about the end of a strig but will in any case compare exactly the number of bytes specified.
In contrast to that, the function strncmp will stop at a byte with value 0 even though the passed number of bytes to compare is not yet reached.
The main difference between strncmp() and memcmp() is that the first is sensible to (stops at) '\0' where the latest is not. If the first 7 bytes of memory from buf and buf2 do not contain a '\0' in it, then the behaviour is the same.
Consider the following example:
#include <stdio.h>
#include <string.h>
int main(void) {
char buf[] = "123\0 12";
char buf2[] = "123\0 34";
printf("strncmp(): %d\n", strncmp(buf, buf2, 7));
printf("memcmp(): %d\n", memcmp(buf, buf2, 7));
return 0;
}
It will output:
strncmp(): 0
memcmp(): -2
Because strncmp() will stop at buf[3], where it'll find a '\0', where memcmp() will continue until all 7 bytes are compared.
When I include the header file <string.h> in MS Visual Studio 2010 it says nothing , but when I declare a string variable in the main function or in any function , it gives an intellisense error that says that string is an undeclared identifier
What's the solution for that issue?
C doesn't actually have strings, at least not really. Unlike C++, strings are actually represented as character arrays, which MUST be null terminated '\0'. What the string header actually does is define functions to help you compare, copy, and modify strings.
Valid string definitions would include:
char string_one[ 80 ];
char *string_two = "this is a character string";
char *string_three = malloc( 80 * sizeof( char ) );
strcpy(string_three, "I have a string now");
There are a TON of resources for string usage in C, heres a few to get you started:
http://www.cs.nyu.edu/courses/spring05/V22.0201-001/c_tutorial/classes/String.html
http://www.eskimo.com/~scs/cclass/notes/sx8.html
http://en.wikipedia.org/wiki/C_string_handling
I use OpenCL on an ATI card with the printf extension enabled. I've written a function to print out variables:
void printVar(constant char* name, float var)
{
printf("%s: %f\r\n", name, var);
}
This code works as expected when compiled as plain C, but if i invoke it in OpenCL with
printVar("foo", 0.123);
the result is always some random char followed by 0.123 instead of "foo: 0.123". I guess the compiler has problems with recognizing the char* string, is there a workaround or a fix so i can get the function working?
As I mentioned in my comment I also get the same behavior, however I can suggest a simple workaround for the use case you showed, I.e. when the string is known at compile time we could just use a define statement instead:
#define PRINTVAR(N,X) (printf(N ": %f\r\n", X))
What are the arguments passed into the main method of a command-line program:
int main(int argc, const char * argv[])
what is the first int mean?
And what is the 2nd parameter, is that an array of chars?
How would one use these?
Also, what practical use is a command-line project type, other than using it to learn obj-c i.e. to practise.
argc means "argument count". It signifies how many arguments are being passed into the executable.
argv means "argument values". It is a pointer to an array of characters. Or to think about it in another way, it is an array of C strings (since C strings are just arrays of characters).
So if you have a program "foo" and execute it like this:
foo -bar baz -theAnswer 42
Then in your main() function, argc will be 5, and argv will be:
argv[0] = "/full/path/to/foo";
argv[1] = "-bar";
argv[2] = "baz";
argv[3] = "-theAnswer";
argv[4] = "42";
The parameters to main() are a unix convention for accessing the arguments given on the command line when your program is executed. In a Cocoa app, you can access them the plain old C way, or you can use NSProcessInfo's -arguments method to get them in an NSArray of NSString objects, or use NSUserDefaults to get them as values in a dictionary.
Just to add to the other answers - Objective-C targets both OS X and iOS. And while there is not much value in iOS command line applications, the shell on OS X is still widely used and there are lot of people writing command line tools.
That main is from C and not specific to objective-c. Argc gives you the number of command line arguments passed to your C program. Argv is an array of C strings and contains the command line arguments.
You would use them and the command-line project any time you wanted to write a command line tool or a program you interact with from the command line.
As wikipedia (and any other source says):
int main(void)
int main(int argc, char *argv[])
The parameters argc, argument count, and argv, argument vector, respectively give the number and value of the program's command-line arguments. The names of argc and argv may be any valid identifier in C, but it is common convention to use these names. In C++, the names are to be taken literally, and the "void" in the parameter list is to be omitted, if strict conformance is desired. Other platform-dependent formats are also allowed by the C and C++ standards, except that in C++ the return type must stay int; for example, Unix (though not POSIX.1) and Microsoft Windows have a third argument giving the program's environment, otherwise accessible through getenv in stdlib.h:
int main(int argc, char **argv, char **envp)
Also, what practical use is a command-line project type, other than using it to learn obj-c i.e. to practise.
The practical use is creating a command-line tool using code from a Framework or Application that you have written. Helpers, utilities, launch agents and daemons, all of these background processes are typically implemented as command-line tools.
Is it possible to emit and read(parse) binary data(image, file etc)?
Like this is shown here:
http://yaml.org/type/binary.html
How can I do this in yaml-cpp?
As of revision 425, yes! (for emitting)
YAML::Emitter emitter;
emitter << YAML::Binary("Hello, World!", 13);
std::cout << emitter.c_str();
outputs
--- !!binary "SGVsbG8sIFdvcmxkIQ=="
The syntax is
YAML::Binary(const char *bytes, std::size_t size);
I wasn't sure how to pass the byte array: char isn't necessarily one byte, so I'm not sure how portable the algorithm is. What format is your byte array typically in?
(The problem is that uint8_t isn't standard C++ yet, so I'm a little worried about using it.)
As for parsing, yaml-cpp will certainly parse the data as a string, but there's no decoding algorithm yet.
Here it is answered how to read/parse binary data from a yaml file with the yaml-cpp library.
This answer assumes that you are able to load a YAML::Node node object from a yaml file - explained in the yaml-cpp tutorials: https://github.com/jbeder/yaml-cpp/wiki/Tutorial).
The code to parse binary data from a yaml node is:
YAML::Binary binary = node.as<YAML::Binary>();
const unsigned char * data = binary.data();
std::size_t size = binary.size();
Then you have an array of bytes "data" with a known size "size".