3,290 questions
2
votes
1
answer
143
views
Trying to learn how getchar works
I am encountering a problem inside a while loop that is embedded in a for loop iterating through a struct of arrays. The issues I am coming across is before the array iterates again, I want to have ...
3
votes
2
answers
273
views
Why does "\0" give integer in c
So, I am learning C language and was trying to make a function that returns the length of a string. In the if statement I mistakenly used "" instead of ''. So, instead of comparing to char '\...
2
votes
2
answers
133
views
Why does my function replace my letters with weird characters? [closed]
My goal is to recreate the strcat() function without libraries:
#include <stdio.h>
char *ft_strcat(char *dest, char *src)
{
int i;
int j;
i = 0;
j = 0;
while (dest[i] !...
1
vote
3
answers
150
views
Inputting to a cstring of undefined size without wasting memory [closed]
I know beginner level C++. In C++ when I want to, for example. input the user's name, all I need to dos is:
getline(cin,str);
but I am trying to learn C.( I want to play with kernels and embedded ...
3
votes
2
answers
166
views
Is it safe to call free on std::string_view::data?
Is it safe to call free in the example below:
size_t len = 10;
char* buffer = static_cast<char*>(malloc(len));
std::string_view sview(buffer, len);
free(sview.data())
Why do I need this?
I have ...
0
votes
3
answers
206
views
What is a null operation?
I have a question related to null operation in C. From my understanding and the thing i read here, a null operation or differently called a null byte, is used to tell the compilers the end of the ...
0
votes
0
answers
45
views
Issues With Pointers and Memory While Making C-String Functions From Scratch in C++
I need help looking for the source of an error that's plaguing me in my coding assignment (I'm doing the class online over the summer.). The professor is tasking the class with several cstring ...
0
votes
3
answers
147
views
Line 17: Char 20: runtime error: left shift of 1 by 31 places cannot be represented in type 'int' [solution.c] [closed]
bool isSubsequence(char* s, char* t) {
int len = 0;
bool res = false;
int k = 0;
len = strlen(t);
char str[len+1];
uint64_t z;
for (uint64_t i = 0; i < pow(2, len)...
2
votes
3
answers
163
views
Can someone help me understand this unexplained behaviour in C char arrays?
I have a function called escape that copies over a character array into a new one while replacing the \t and \n characters with raw characters \t and \n i.e single character '\n' becomes 2 characters '...
4
votes
2
answers
132
views
how to change color in a string in c?
My problem is that I have a string which is colored gray, the string is almost 1000 characters long. 2 of them must be in another color (red), but I don’t know how to change the color in the string.
...
3
votes
3
answers
173
views
remove a character by using conditional statement
recently just practiced the problem 67 in Leetcode This problem is like you would be given two binary string, and you have to calculate the sum of these two binary string and return the result.For ...
10
votes
2
answers
1k
views
Are the literals "" (empty string) and "\0" (null string) identical? [duplicate]
Is there any situation where the literal const char*s "" and "\0" differ, or are they identical?
Edit: As requested that an edit should be added, the answer to this question was ...
-1
votes
1
answer
181
views
How to reduce memory used by an array buffer? [closed]
I am learning C++. I have a code which is working, but now I am wondering if it can be optimized in terms of memory usage?
I need to get and keep a few strings in a character array, but the number of ...
4
votes
3
answers
462
views
If I am using UTF-8 strings is it risky to use standard string handling that assumes null termination?
From what I understand it is very rare for UTF-8 strings to have embedded NULLs, however there is the case that a person can put a NULL into a Unicode string explicitly with "X\0Y" or ...
3
votes
1
answer
291
views
How to convert cString into String in Swift6?
The following code produces a warning in Swift6:
'init(cString:)' is deprecated: Use String(decoding: array, as:
UTF8.self) instead, after truncating the null termination.
var size = 0
...
3
votes
4
answers
190
views
Change pointer adress inside function in C
I'm trying to make a program that deletes extra white spaces from a string. It should keep one space but delete any extra.
I wrote this code, which works up until the point I have to change the passed ...
1
vote
2
answers
66
views
Line extraction in a char
Hi i'm trying to write a recursive function to extract each line of a string. I don't know why it doesn't work i spent 2 hours trying to find the issue.
#include <string.h>
#include <stdlib.h&...
1
vote
2
answers
105
views
How to debug this substring matching code?
I am a beginner in C programming, and this is a string matching code I wrote. It aims to find the positions where the substring t appears in the string s and print them. The use of pointers is ...
1
vote
3
answers
94
views
c++ manual concatenation of c-strings function that is called twice and adds a space
I have an assignment to write a function that takes two C-string parameters and appends the second to the first just like the strcat function but without using said function. Then, write a main driver ...
0
votes
1
answer
57
views
During the second call of strtok(), the code raises the following error: Invalid read of size 1
I am new to C++ and I do not understand why the code doesn't continute tokenizing the text array, and instead raises the error.
I suspect that the problme might be because of passing the token ptr mid-...
0
votes
0
answers
32
views
Does cstring variables needs to be static to work with ESP8266?
Despite 5 years of development with ESP8266 (mini D1) I'l still confused:
My IDE version is 1.8.19
My sketch is 3300 lines but I made a test case:
This is the sketch:
// Globals
int i=1;
void setup() ...
4
votes
2
answers
223
views
Why does fgets() require a maximum size of user input? Is it because it does not have the "restrict to first space" property of scanf()?
This is fgets()'s official prototype:
char *fgets(char *str, int n, FILE *stream);
Why specify the size (n) ? Is it to prevent buffer overflow of some sort? Or is it just a design flaw?
I would think ...
1
vote
3
answers
153
views
How to store a dynamic C string
For an assignment in my college data structures class, we have to define functions for a palindrome class that takes a word, checks if it is a palindrome, stores it as half the word, and can access ...
1
vote
1
answer
87
views
How to compare an array with a char using if in C?
I ask for your help. I need to compare an array of tokens with a char using the if statement. But in the array I also have a string, the code is this:
char bho(char** Token){
char* token = (char*)...
2
votes
3
answers
217
views
Why is the strlen here equal to 25?
Just so I can confirm if what I think is going on is really going on. The following code prints out 25 when I give it the (26 letter) alphabet as an input, is it because fgets always automatically ...
0
votes
0
answers
616
views
Convert string_view to char* and walk away with it
In this post, How you convert a std::string_view to a const char*?, the question was raised how to convert std::string_view into char* and the answer is basically:
make a temporary string from the ...
0
votes
1
answer
113
views
Adding elements into a char array
I've been trying to make a function in C that takes a string as the input and then prints out a version of that string that only contains alpha characters.
#include <string.h>
#include <ctype....
0
votes
2
answers
88
views
C++ logical error: strstr function not finding an available entry
I wrote the code below to try the strstr() function to search for a name in a list, but it is not finding it.
Can someone please explain why this code fails to find the name I am trying to look for?
#...
1
vote
1
answer
88
views
C pointers: Function returns a pointer defined in its body [duplicate]
I'm learning C using the book C Programming: A Modern
Approach and I have some doubts about the use of pointers
and referencing an out-of-scope variables. I've put together three examples to
...
1
vote
3
answers
121
views
Does s conversion specifier specify direction / order in which characters are written?
Does s conversion specifier specify direction / order in which characters are written?
N3220 working draft, 7.23.6p8 (emphasis added):
s If no l length modifier is present, the argument shall be a ...
-1
votes
2
answers
92
views
char *array[200] sorting program doesn't read words
The program is expected to sort and display a list of words from a array whose elements are pointers to char. I use malloc to alocate space to each element and use a read_line function to store in the ...
1
vote
2
answers
8k
views
Unexpected behaviour while swapping strings in C
I am a beginner to programming and today I was trying to swap two strings with somewhat of a raw , brute force logic . But I stumbled upon this behaviour :
My code :
#include <stdio.h>
#include ...
0
votes
0
answers
138
views
Microsoft's newer (templated) CString does not handle null characters the way it did before
// this is the old MFC CString (not actually dependant on CObject) vs the new ATL version that is implemented as a template
char array[] = { 0x45, 0x46, 0x0, 0x45 };
CString a(array,sizeof array);
...
1
vote
1
answer
149
views
Finding possible permutations of a word
again. I've written a code on finding all possible combinations on a word and my code works but very limited times (i.e) it prints the string with "a" being only swapped (my I/P is abcd) but ...
2
votes
4
answers
123
views
Number index to array element
Task
Convert number to Chinese Pinyin. Minus sign is equal to "fu".
key-value
# num: pinyin
0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu
Code
#include <stdio.h>
...
0
votes
1
answer
66
views
LSD Radix for a fixed count array
I would like to create a radix LSD to sort strings. I tried to adapt to a count[27] instead of count[256], because the exercise requires it. I must use A[I][d] - 'a' + 1 for [a-z] and 0 for spaces, ...
0
votes
1
answer
115
views
Segmentation fault occur in function when passing char* instead of char[] during equation. How can I get access to char* literal? (avoiding read-only)
Here's function code:
void TrimRight(char* s)
{
if(!*s) return;
char* end = 0;
for(char c; c = *s; s++)
{
if(c == ' ') { if(end == 0) end = s; }
else end = 0;
}
...
1
vote
1
answer
176
views
Bun FFi function fails when I return a null terminated slice from ziglang
I'm implementing a ziglang function to generate a ulid. So far it works when called via a main function. However, when using it with Bun's FFI it fails. (console.log) is an empty string.
Zig
const std ...
0
votes
1
answer
76
views
I need to create a malloc array of strings and print those strings out
important: I don't have a deep understanding of malloc so try to put things as simple as possible
Greetings, I want to create a dynamic array of strings (using malloc) and then print those strings out ...
1
vote
1
answer
106
views
Is there a worked example of using CStrBufT with a CString?
I stumbled on this class when browsing the internet this morning: CStrBufT.
I understand that it is meant to be used instead of:
CString::GetBuffer
CString::GetBufferSetLength
CString::ReleaseBuffer
...
1
vote
1
answer
126
views
Function is returning null instead of array in C [duplicate]
So I am solving the "Longest Common Prefix" problem on Leetcode in C and have almost got it working. My Code:
char * longestCommonPrefix(char ** strs, int strsSize){
int minlen = ...
0
votes
1
answer
61
views
Nested strtok() calls to tokenize given string does not work as expected [duplicate]
I want to tokenize a provided input string using strtok(). In the first step, I want to tokenize the given string by "|" symbol. Then, I tokenize each substring by ";". Finally, I ...
1
vote
2
answers
457
views
Looping through an array which contains a string with spaces
I am trying to loop through an array that contains a string with is several words separated by spaces. Ultimately I want to make each word within the string a new variable so I can compare it to other ...
1
vote
1
answer
116
views
Word Count in C
I have a word count program in C that excludes special characters and number digits:
int main(){
char str[100];
int i, j = 0;
int len;
char choice;
do {
printf("...
0
votes
1
answer
59
views
Last character index of inverted string in C being the whole uninverted original string
I just started learning C and i'm trying to write a program that inverts the string so that furthermore i can check if the string is a palindrome. I did it by adding a for loop limited by the size of ...
0
votes
0
answers
321
views
sprintf blocks stm32 program
I'm programming an STM32 microcontroller. In the code I'm just calling sprintf the way I did before and it worked then, but now it just freezes the entire program. What's the problem here?
char str[...
2
votes
3
answers
307
views
Calling SHGetKnownFolderPath from Python?
I've written this minimal reproducible example to calculate the Desktop folder on Windows "the hard way" (using SHGetKnownFolderPath), but I seem to end up with a Success error code while ...
0
votes
0
answers
36
views
Invalid Initialization of Non-Const Reference Error in C++ [duplicate]
I am re-creating the string class in C++. I developed both the constructor and the copy constructor as well as an operator+ method to concatenate two strings.
The issue I am facing is that the copy ...
1
vote
3
answers
465
views
Is this a legal C strdup function?
I tried to make my own strdup function to practice my C skills, but I'm not sure if it's legal or not.
the function is down below:
char*
strdup(char* str) {
char* dup_str;
int len;
for (...
1
vote
4
answers
229
views
How to pass a string to a function and return the same string changed in C?
I have a function that converts the content of strings from hexadecimal symbols to binary symbols.
In my simple example I have used only two hex symbols a and b, and the same string is converted two ...