c-strings

How to append characters to a string array in C

走远了吗. 提交于 2020-01-25 06:48:06
问题 I'm very new to C and I'm trying to write a program that checks if a string contains any uppercase letters, and if it does, prints them out. I'm using https://www.onlinegdb.com/online_c_compiler# as my compiler (cause I don't have access to my personal computer right now) and after a test run, the results are (p.s. I know gets isn't safe): main.c:16:5: warning: ‘gets’ is deprecated [-Wdeprecated-declarations] /usr/include/stdio.h:638:14: note: declared here main.c:(.text+0x26): warning: the

Selecting only the first few characters in a string C++

耗尽温柔 提交于 2020-01-23 05:53:08
问题 I want to select the first 8 characters of a string using C++. Right now I create a temporary string which is 8 characters long, and fill it with the first 8 characters of another string. However, if the other string is not 8 characters long, I am left with unwanted whitespace. string message = " "; const char * word = holder.c_str(); for(int i = 0; i<message.length(); i++) message[i] = word[i]; If word is "123456789abc" , this code works correctly and message contains "12345678" . However,

C-String array initialization - is this mutable? [duplicate]

安稳与你 提交于 2020-01-22 18:32:11
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Modifying C string constants? Pointer to const char vs char array vs std::string I know I'm probably beating the dead horse with this question, but I'm a little confused and I haven't managed to find an exact answer on SO or google (that I'm confident is right - there's just too much information on C-strings to sift through). Also, I've tagged it C++ because that's what I'm interested in, even though we're

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

假装没事ソ 提交于 2020-01-14 05:17:28
问题 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

How to assign string of one variable to other variable?

瘦欲@ 提交于 2020-01-11 14:05:16
问题 This is my first question on this site. How do i assign string of one variable to other variable. What am i doing wrong here? #include<stdio.h> #include<string.h> main(){ char a[30],b[30]; scanf("%s",a); b[30]=a[30]; printf("%s",b); } 回答1: Use the standard C function strcpy declared in the header <string.h> . For example strcpy( b, a ); Arrays do not have the assignment operator. As for your statement b[30]=a[30]; then b[30] and a[30] are undefined objects of the type char that are beyond the

strncpy() fails on second call for same source

爷,独闯天下 提交于 2020-01-11 13:22:47
问题 I'm new with c and want to separate string in two parts. Here is my code: #include <stdio.h> #include <string.h> #include <stdlib.h> void test(char** a, char** b) { const char * c = "abcdef"; *a = (char *)malloc(4* sizeof(char)); *b = (char *)malloc(4* sizeof(char)); strncpy(*a, c, 3); *a[3] = '\0'; fprintf(stderr, "a -> %s\n", *a); strncpy(*b, c+3, 3); *b[3] = '\0'; fprintf(stderr, "b -> %s\n", *b); } int main() { setvbuf (stderr, NULL, _IONBF, 0); char *a = NULL; char *b = NULL; test(&a, &b

reversing only certain words of a string

笑着哭i 提交于 2020-01-11 13:11:09
问题 I have a string with the name "Mustang Sally Bob" After i run my code i want the string output to be like this: gnatsuM yllaS boB My approach is to count the words until the space and save the index of where the space is located in the string. then Then I want to print the characters starting from the space backwards. #include <stdio.h> int main() { char* test="Mustang Sally Bob"; int length; //string length int x; for(length=0;test[length] !=0&&test[length];length++); //get string length int

Understanding C-strings & string literals in C++

限于喜欢 提交于 2020-01-04 04:34:08
问题 I have a few questions I would like to ask about string literals and C-strings. So if I have something like this: char cstr[] = "c-string"; As I understand it, the string literal is created in memory with a terminating null byte, say for example starting at address 0xA0 and ending at 0xA9, and from there the address is returned and/or casted to type char [ ] which then points to the address. It is then legal to perform this: for (int i = 0; i < (sizeof(array)/sizeof(char)); ++i) cstr[i] = 97

Format string with multiple percent signs

痴心易碎 提交于 2020-01-03 13:59:29
问题 I know %% is used to escape actual % signs in a string, so %%%ds will end up with %10s in the following format string, but I don't know why I need %%5s in this string? After all, there are only two additional arguments (BUFFSIZE / 10). #define BUFFSIZE 100 char buf[100]={0} sprintf(buf, "%%5s %%%ds %%%ds", BUFFSIZE / 10, BUFFSIZE / 10); After running the code above, the buf will contain the string, %10s %10s 回答1: The purpose is to get a format string to use it in another function that needs a

scanf() does not read input string when first string of earlier defined array of strings in null

▼魔方 西西 提交于 2020-01-01 19:37:09
问题 I defined an array for strings. It works fine if I define it in such a way the first element is not an empty string. When its an empty string, the next scanf() for the other string stops reading the input string and program stops execution. Now I don't understand how can defining the array of strings affect reading of input by scanf() . char *str_arr[] = {"","abc","","","b","c","","",""}; // if first element is "abc" instead of "" then works fine int size = sizeof(str_arr)/sizeof(str_arr[0]);