char array initialization and some char string and c difference [closed] - c++

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 8 years ago.
Improve this question
I see some code written like this:
char str[256] = {0};
or
char str[256] = {1};
when the former case, I use
printf("%s", str);
gives nothing,
Does it means give all the str[0] to str[256] all value 1?
when latterthe stdio give a ASCII smile char.
Furthermore, what is the difference between
char s[256] = {0};
printf("%c", s[1]);
It gives nothing in stdout
char s[256] = {0};
printf("%s", s[1]);
it give a (null)
I do not understand because I am a beginner of c char array and c pointers.

This initializes all 256 chars to 0:
char str[256] = {0};
This one initializes the first one to 1, and all the rest to 0:
char str[256] = {1};
Concerning the behaviour of printf, "%c" expects a single char. You pass it 0, which is NUL (the character string termination) so it prints nothing. "%s" expects a char* with the first character in a nul-terminated character string. You pass it a single char with value 0, which it interprets as a null pointer.

Related

C++: strcpy non-ascii characters problem is possible? [closed]

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 2 years ago.
Improve this question
I am trying to create a specific case of a problem in my following code;
std::string id = "123456789123456789" // it can be different sized string too.
std::string my_id = "TEST_" + id;
char* temp_my_id = (char *) my_id.c_str();
strcpy(temp_my_id, my_id.c_str());
PRINT("my_id value : ", temp_my_id);
It is a legacy code in my project and I suspect the temp_my_id returns non-ascii character. Is this possible?
char* temp_my_id = (char *) my_id.c_str();
strcpy(temp_my_id, my_id.c_str());
source and destination can not have the same memory location in strcpy because both arguments are marked as restrict:
Bold test is mine:
restrict is a keyword (in C, not in C++) that can be used in pointer declarations. By
adding this type qualifier, a programmer hints to the compiler that
for the lifetime of the pointer, only the pointer itself or a value
directly derived from it (such as pointer + 1) will be used to access
the object to which it points.
you can try
char a[] = "123";
char *b = a;
strcpy(b, b);
returns:
warning: ‘strcpy’ source argument is the same as destination [-Wrestrict]
again in C, in C++ it is also broken because source and destination can not overlap.
strcpy does not really care about the encoding, it copies until it finds a NULL byte. So you just have to be careful that
the source ends with a zero byte and contains none in the middle
the source is not longer than the destination
source and destination should not overlap
see strcpy vs. memcpy and
http://www.cplusplus.com/reference/cstring/strcpy/

character and array [closed]

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 8 years ago.
Improve this question
I came across something unfamiliar in C++
char name[5] = "best";
I only know that char gives us a size of 1 character only.
So does the above line of code i.e. using arrays with char helps to increase the size of char data type ?
I hope now my question is clear.
char name;
This gives you a single char object.
char name[5];
This gives you 5 char objects, one after the other - this is called an array of 5 chars. You can index them with name[0], name[1]... until name[4].
"best"
This is a string literal. It represents an array of 5 chars in read-only memory, containing the characters b, e, s, t, and \0.
char name[5] = "best";
This declares an array of 5 chars, as before, and initialises each of the elements of that array with the elements of the string literal. The name array will now also contain the characters b, e, s, t, and \0.

Difference between char , char[] , char * [closed]

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 8 years ago.
Improve this question
I tried to set simple char with empty value without success. The main goal was to compare between std::string single char and pre-defined char
std::string str ="fcvfr";
char c = '' //trying to set empty char here .... but it gives me error
if(c == str[0])
{
//do something
}
This leads me to the question when should I use each of the following types:
char * , char , char[]
char represents a character (allocated on the stack). If you want to set it to empty, use char c = '\0' or char c = (char) 0.
char* cPtr is a pointer to a character. You can allocate an empty character on the heap with char* c = new char('\0').
char c[n] is a character array of size n.
Edit
As people have correctly pointed out below, a char is never empty, in the same sense as a container such as std::vector or std::string can be empty. A char is not fundamentally different to, say, an int, it's just shorter (1 byte as opposed to 2 or 4 or 8). Can an int be empty? Not as such; it can be zero, meaning that all its bits are set to zero in memory, and the same goes for a char. char c = '\0' will be represented as "00000000" on the stack.
A pointer to a char (char* cPtr), on the other hand can be 'empty' in the sense that it can point nowhere, by setting it to NULL. In this case, the pointer itself will exist on the stack and will contain a special sequence of 0/1's that your system interprets as NULL. Once you do cPtr = new char('\0'), a char (i.e. a byte) will be allocated on the heap and set to "00000000", and the value of cPtr on the stack will be changed to point to the address of the new character on the heap.
PS: don't actually do char* cPtr = new char('\0'), use an std::vector<char> or an std::string. Also, you may want to look into smart pointers.

how to convert std::string pointer to std::string [closed]

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 have std::string pointer and i like to copy its value to ordinary std::string
i can't find quick and fast way to do it .
i have this :
int main ()
{
std::string * pStr = new std::string("hello")
std::string strNew = pStr->??? // how to convert ?
return 0;
}
Dereference:
std::string strNew = *pStr;
Two ways:
std::string strNew = pStr->c_str(); // Be careful of `\0` with in the string
or
std::string strNew = *pStr;
The second is better, because C-style string can not represent std::string correctly. It ends a string at first \0 and ignores trailing.

convert unsigned long to char* in c++ [closed]

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
How do I cast unsigned long to char*.
I am doing the following for loop
for (unsigned long i = 1; i< 7;i++)
{
callAMethod(5,i);
}
The method definition is as follows
callAMethod(int, const char*)
While I do this I get the following error:
invalid conversion from `unsigned long' to `const char*'
How do I do this?
Old C pointer arithmetic:
for (unsigned long i = 1; i< 7;i++)
{
callAMethod(5, (const char*)(&i));
}
But this way you'll just get one byte of the 8 bytes of the long. Do you want to get the string representation of the long? You may utilize STL and get the str().
"const char*" means "address of a character". You're going to pass it values 1 <= n < 7.
You can ask the compiler to pretend they are valid pointer-to-char values by casting
callAMethod(5, reinterpret_cast<const char*>(i));
But then I'd hazard a guess that your application will crash when it trys to read the memory at address "1" to look for a char
It seems like your "callAMethod" is expecting a C-style string, which is an arbitrary sequence of characters with the end-of-string denoted by a character of value 0:
const char hello[6] = { 'h', 'e', 'l', 'l', 'o', 0 };
being equivalent to
const char* hello = "hello";
(when you write a string in quotes like that in C/C++, the compiler automatically adds a 0 byte to the end of the stored string).
Typically, when you pass strings to a function, the finger print is "const char*"
So what you actually want to know is "how do I make a string from a long value" and the answer is "printf".
char str[20];
for (unsigned long i = 1; i < 7; ++i) {
#ifdef _MSC_VER
sprintf_f(str, "%u", i);
#else
snprintf(str, sizeof(str), "%u", i);
#endif
callAMethod(5, str);
}