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.
Related
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 1 year ago.
Improve this question
#include<iostream>
using namespace std;
char* str[]={"Man","Woman","Car","Plane",0};
int main(){
char** cp=str;
while(*cp!=0)
cout<<*cp++<<endl;
return 0;
}
It prints the String.
But when I print **cp++ I get only first letters like M,W,C,P.
For starters the array should be declared with the qualifier const because in C++ string literals have types of constant character arrays.
const char* str[]={"Man","Woman","Car","Plane",0};
In fact the declaration above is equivalent to
const char* str[]={ &"Man"[0], &"Woman"[0], &"Car"[0], &"Plane"[0], 0 };
because the string literals having array types used as initializers in this declaration are implicitly converted to pointers to their first elements.
In this declaration
char** cp=str;
that also should be written like
const char** cp=str;
the pointer cp points to the first element of the array that has the type char * and points to the first character of the string literal "Man".
Dereferencing the pointer cp one time like *cp you will get the first element of the array that has the pointer type char * and points to the character 'M' of the string literal "Man". Dereferencing the pointer the second time like **cp you will get an object of the type char that contain this character 'M'.
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/
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
I'm new comer to C++, so I'm very confused about the code below:
const char* headers[]= {"apple","pear","tree"};
So is this line supposed to create "an array of pointers to char"? (Maybe i'm wrong). If so, why such array of pointers being assigned to an array of strings?
Also I'm confused why bother to have such pointer array? Can't we simply do sth like: int array [5] = { 1, 2, 3, 4, 5 };
All such pointer/reference stuff in C++ are so confusing and difficult for people like me from Java world.
Many thanks!
Hope this will help, here is a basic explanation of what the line means with some crude illustrations.
Please note that all addresses are made up, and some things are simplified in hopes of making the concept easier to see. Forgive the crude art, I am far from an artist!
const char* headers[]= {"apple","pear","tree"};
This line is telling the compiler that you would like to allocate an array. The elements in the array will be of type char*. The const keyword tells the compiler that the object or variable is not modifiable. char* is the type of elements in the array. headers is the variable name, and the brackets [] indicate that headers is an array. The rest of the line is an initializer for the array. header will have 3 elements of type char* which basically means “pointer to a string of characters”.
The compiler will also allocate memory and initialize the array by filling it with each of the 3 strings in the braces {"apple","pear","tree"}
The first element in the header array header[0] will contain a pointer to the memory where the string “apple” is located.
The second element in the header array header1 will contain a pointer to the memory where the string “pear” is located.
const char* headers[]= {"apple","pear","tree"};
"If so, why such array of pointers being assigned to an array of strings?"
Because in C there is no such thing as a string. There is instead a sequence of characters with a trailing 0 at the end.
C++ has std::string.
const char* headers[]= {"apple","pear","tree"};
means headers is an array of char pointers. Simple way to dissect it to look at non array version
const char * fruit = "pear";
This creates a literal "pear\0" (note the 0 added on the end) and then creates a variable that points to the 'p' character. Ie it contains the address of where that 'p' is stored. Now back to
const char* headers[]= {"apple","pear","tree"};
This creates 3 literals 'apple\0', 'pear\0', 'tree\0'
It create a 3 entry array of char * pointers
It sets header[0] to point to the 'a' of apple
It sets header[1] to point to the 'p' of pear
It sets header[2] to point to the 't' of tree
Answering the question as asked:
const char* headers[]= {"apple","pear","tree"};
This line defines a variable named headers, with type "array of 3 pointers to const char" (where 3 is deduced from the number of initializers). The pointers are set to point to 3 string literals, which they remain immutable during program execution.
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.
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.