Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How can we test the end of an array of unsigned char ? With a string char it's easy, I can just make a test like
while (str[i] != '\0')
or just
while (str[i])
But with an array of unsigned char that doesn't work and I don't really understand why. That's an example where I need help
BYTE* a = getB();
int i=0;
while(a[i]!=0)
{
printf("%C", a[i]);
i++;
}
Thanks
It is completely up to you.
That is, you define what the end of an array means.
Conventionally, sure, the end of a character array is a "null byte", signifiying the terminating position. There's no reason you can't do that with an array of unsigned char.
But, just like with char, this need not necessarily be the case. Maybe you signify the end of the array by counting its elements (specifying a size), or by declaring that some other character is the final one.
We cannot tell you what that is. Only the person who creates the data (as far as we're concerned here, that's you) can do so.
So, either consult the documentation for the library that gave you the array, or reach into your memory. :)
#eugenesh is right. You can't do do it. Either you need to have extra space at the end and add some kind of delimiter or keep a count of charactera
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I've got a recursive function that is supposed to check if a given word is a palindrome, which works by comparing the first and last value, then strips them off and passes them recursively into checkPalindrome, but whenever I try to assign word[size-1] to '\0', I get a Bus Error.
Here is my code:
bool checkPalindrome(char word[]){
int size = std::strlen(word);
//Parts removed...
word[size-1]='\0'; //This is the line causing the issue
return checkPalindrome(word+1);
}
}
Here is an example of a call to this function:
checkPalindrome("racecar");
Thank you!
There are two ways I can think of to solve this problem:
The obvious one would be to use a std::string instead of a string literal and just .pop_back() (C++11) to remove the last character.
The second would be to pass the length of the string to the function as a parameter, instead of computing it. Then, just decrease the length by 1 so now you have a new "fake end point" to the string. Since you're using this number to check the last character, you don't really need to actually modify the string, just change where the "last" character is.
But since you're using C++, I don't see why not use std::string and avoid overcomplicating the problem.
So as stated in the comments you can't modify a string literal.
So before passing it to the function, make a copy of it.
std::size_t len = std::strlen(word);
char* copy = new char[len + 1];
std::strncpy ( copy, word, len + 1 );
isPalindromeR(copy);
delete[] copy;
other solution would be to not use recursion, thus not needing to modify the string.
anyway outside of assignments use a std::string
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How can we know whether the given input is int or char using unions in c++?
Please keep the program simple.
How can I actually know when the given input is char or int so that I perform specific operations?
I basically got to know that in a char array ,when I enter a double digits number,the array takes only the first digit,is there any correct method or I need to use only unions?
Unions are not able to distinguish what type you put in. When it is important (what it is in most cases) and you don't want to the union as cast, then you need to store what type of data you put in.
E.g.,
typedef union
{
int a;
char b[4];
} myUn;
now you can do
myUn mu;
mu.a=42;
char c=mu.b[0];
and your compiler wont complain. So you need to store in a further location what type you have put in (if important).
e.g.,
typedef struct
{
myUn mu;
int type;
}
and encode in type what you have put in. But that is just a very basic solution and you should ask why you want to use a union here.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to store characters from user's input in array, BUT not one by one. User will input them as one line like this;
....
I need to save each dot in array, but I can't do this:
scanf("%s%s%s%s", &s[0], &s[1], &s[2], &s[3])
because user can enter N number of dots. So it must be dynamic, I guess.
scanf() is a C runtime function. In C++, you should be using std::cin instead. For instance, with std::getline(). You can treat the returned std::string like an array of characters.
User will input them as one line like this; .... I need to save each dot in array,
C solution:
Define upper sane bound like 1000 and use a scanset "%[]".
// Read up to 1000 `.`
char dot[1000 + 1];
if (scanf(" %1000[.]") == 1) {
// Success
puts(dot);
}
Additional code needed if other non-., non -white-space characters need to be handled.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm trying to write a simple hangman game in c++ by randomly selecting a word from a list, checking the string length, and writing that many *s into a new string to serve as placeholders in the yet un-guessed word. The max length is 9 letters. I have the game working almost flawlessly -- the problem is that whenever my word has 8 or 9 letters, the program prints the correct number of *s followed by one or two � characters. Research tells me these are unprintable characters, but I've tried for a while now and I'm not sure why they're here, why they only show up with a word length>7, or how to get rid of them. Below is relevant code. Any suggestions?
Generating *s:
char word[80];
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word[i]='*';
}
You forgot to add the \0 terminator at the end of the string. After the for loop, add:
word[i] = '\0';
Or, best, use std::string instead of a C string.
Try using std::string instead.
std::string word;
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word+='*';
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to be able to iterate through all of the utf-8 characters in a string.
Imagine that all code points had a index, the first code point had index 0 and the last code point had index N. I want the iterator to be able to jump X indexes forward and also tell me the index of a code point.
I want to do something similar to http://www.nubaria.com/en/blog/?p=371, but I am not sure how to iterate over the bytes so that the iterators always refer to the start of a legal utf-8 code points.
Warning: this method only works if you already know that your bytes contain clean UTF-8. It will not work properly in the presence of malformed or invalid characters.
The second through last bytes of a UTF-8 encoded codepoint will always have a bit sequence of 10xxxxxx. Skip over those and you'll be at the start of the next codepoint.
for (int i=0; i<X && *p!=0; ++i)
{
++p;
while ((*p & 0xc0) == 0x80) ++p;
}
The *p!=0 is there to make sure you don't run past the end of the string.