c-strings

Understanding the dereference, address-of, and array subscript operators in C

我的未来我决定 提交于 2019-12-11 05:38:56
问题 I have argv[] defined as a char *. Using the following printf statements: printf("%s\n",argv[1]); // prints out the entire string printf("%p\n",&argv[1]); // & -> gets the address printf("%c\n",argv[1][0]);// prints out the first char of second var printf("%c\n",*argv[1]); // It's this last one I don't understand. What does it mean to print *argv[1] ? why isn't that the same as *argv[1][0] and how come you can't print out printf("%s\n",*argv[1]); . Also, why is &*argv[1] a different address

C++ comparing c string troubles

廉价感情. 提交于 2019-12-11 02:02:20
问题 I have written the following code which will does not work but the second snippet will when I change it. int main( int argc, char *argv[] ) { if( argv[ 1 ] == "-i" ) //This is what does not work //Do Something } But if I write the code like so this will work. int main( int argc, char *argv[] ) { string opti = "-i"; if( argv[ 1 ] == opti ) //This is what does work //Do Something } Is it because the string class has == as an overloaded member and hence can perform this action? Thanks in advance

Use strlen with scanf(%ms)

泄露秘密 提交于 2019-12-11 01:42:36
问题 Is it possible to use strlen() over a dynamically allocated string? FOR EXAMPLE : #include <stdio.h> #include <string.h> int main () { char *input=NULL; printf ("Enter a sentence: "); scanf("%ms", &input); //Is this legit? printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(input)); return 0; } 回答1: You can use strlen() on any sequence of char s ended by a '\0' , the null-character aka NUL *1 , which in fact equals 0 . It does not matter how the memory has been allocated.

reading input parameters from a text file with C

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 01:37:02
问题 I am trying to write a general function that will read in parameters from formatted text file. I want it to be flexible enough that the parameter list can vary. What is the best way to accomplish this in C? I've been struggling with this for a few days. The strings that I'm able to extract from the file are not what I expected. The sample text file I'm using to debug is simple: Nx : 1600; Ny : 400; dx : .524584; dy : .25; dt : 1; My program is below. #include <stdio.h> #include <string.h>

Interfacing C++ with Rust - returning CString panics

天大地大妈咪最大 提交于 2019-12-10 23:10:15
问题 I am trying to call some functions written in Rust from C++. So far I've been quite successful but I still have one little problem with a CString -related panic during runtime. The function hello is supposed to take an input string, concatenate it with some other string and return the product. Here's my fun.rs : use std::ffi::CString; #[no_mangle] pub extern "C" fn add(a: i32, b: i32) -> i32 { a + b } #[no_mangle] pub extern "C" fn hello(cs: CString) -> CString { let slice = cs.to_str()

sgetn Doesn't Null Terminate String

你。 提交于 2019-12-10 22:39:23
问题 sgetn Takes a char* for it's first argument and writes characters to it. It does not write a trailing '\0' to the char* . This behavior seems to be inconsistent with every other time that I can find a char* written to. However, it is consistent across Clang, gcc, and Visual Studio, so I can't believe it's a bug that all the compilers have. Is there a reason that the standard doesn't require the trailing '\0' to the char* ? [Live Example] 回答1: Because it can be used to read arbitrary data, not

Dynamically prompt for string without knowing string size

断了今生、忘了曾经 提交于 2019-12-10 17:05:56
问题 In C, what is the best way of prompting and storing a string without wasted space if we cannot prompt for the string length. For example, normally I would do something like the following... char fname[30]; char lname[30]; printf("Type first name:\n"); scanf("%s", fname); printf("Type last name:\n"); scanf("%s", lname); printf("Your name is: %s %s\n", fname, lname); However, I'm annoyed with the fact that I have to use more space than needed so I do not want to use char fname[30] , but instead

Default advice for using C-style string literals vs. constructing unnamed std::string objects?

北慕城南 提交于 2019-12-10 12:35:35
问题 So C++ 14 introduced a number of user-defined literals to use, one of which is the "s" literal suffix, for creating std::string objects. According to the documentation, its behavior is exactly the same as constructing an std::string object, like so: auto str = "Hello World!"s; // RHS is equivalent to: std::string{ "Hello World!" } Of course constructing an unnamed std::string object could be done prior to C++ 14, but because the C++ 14 way is so much simpler, I think way more people will

Difference Between const char[] and const char*

主宰稳场 提交于 2019-12-10 11:37:37
问题 So this article is discussing the use of declaring a string literal like const char* foo = "foo" it ends with the claim: const char *foo = "foo"; is almost never what you want. Instead, you want to use one of the following forms: For a string meant to be exported: const char foo[] = "foo"; For a string meant to be used in the same source file: static const char foo[] = "foo"; For a string meant to be used across several source files for the same library: __attribute__((visibility("hidden")))

Should I pass a mutable reference or transfer ownership of a variable in the context of FFI?

被刻印的时光 ゝ 提交于 2019-12-08 18:52:30
I have a program that utilizes a Windows API via a C FFI (via winapi-rs). One of the functions expects a pointer to a pointer to a string as an output parameter. The function will store its result into this string. I'm using a variable of type WideCString for this string. Can I "just" pass in a mutable ref to a ref to a string into this function (inside an unsafe block) or should I rather use a functionality like .into_raw() and .from_raw() that also moves the ownership of the variable to the C function? Both versions compile and work but I'm wondering whether I'm buying any disadvantages with