What does below pointer syntax mean [duplicate] - c++

This question already has answers here:
Placement of the asterisk in pointer declarations
(14 answers)
Closed 7 years ago.
Does below code segments mean the same thing or each one has a different meaning
char *data = "blah";
char* data = "blah";
char * data = "blah";

The three are same. The only differences are,
The position of the asterisk and the whitespace around them(which is done according to one's preference)
The string literals "blah" could be stored in the same memory location or different locations. From the C11 standard,
6.4.5 String literals
[...]
It is unspecified whether these arrays are distinct provided their elements have the
appropriate values. If the program attempts to modify such an array, the behavior is
undefined.

All of them are same
char *data = "blah";
char* data = "blah";
char * data = "blah";
char*data = "blah";

All of them are same. They all mean, data is a pointer to type char. The space in between don't make any difference. IMHO, you should have googled/binged that up.
EDIT:: Copied from The Paramagnetic Croissant comment, I think it will help others:: cdecl.org

Related

understand how char works in c++ [duplicate]

This question already has answers here:
What happened when we do not include '\0' at the end of string in C?
(5 answers)
What is the difference between char s[] and char *s?
(14 answers)
Why do string literals (char*) in C++ have to be constants?
(2 answers)
Closed last year.
I am a C++ newbie. Although many similar questions have been asked and answered, I still find these concepts confusing.
I know
char c='a' // declare a single char c and assign value 'a' to it
char * str = "Test"; // declare a char pointer and pointing content str,
// thus the content can't be modified via point str
char str1[] = "Test"; // declare a char array str1 and assign "Test" to it
// thus str1 owns the data and can modify it
my first question is char * str creates a pointer, how does char * str = "Test"; work? assign a string literal to a pointer? It doesn't make sense to me although it is perfectly legal, I think we can only assign an address to a pointer, however "Test" is a string literal not an address.
Second question is how come the following code prints out "Test" twice in a row?
char str2[] = {'T','e','s','t'}; // is this line legal?
// intializing a char array with initilizer list, seems to be okay to me
cout<<str2<<endl; // prints out "TestTest"
why cout<<str2<<endl; prints out "TestTest"?
char * str = "Test"; is not allowed in C++. A string literal can only be pointed to by a pointer to const. You would need const char * str = "Test";.
If your compiler accepts char * str = "Test"; it is likely outdated. This conversion has not been allowed since C++11 (which came out over 10 years ago).
how does char * str = "Test"; work?
String literals are implicitly convertible to a pointer to the start of the literal. In C++ arrays are implicitly convertible to pointer to their first element. For example int x[10] is implicitly convertible to int*, the conversion results in &(x[0]). This applies to string literals, their type is a const array of characters (const char[]).
how come the following code prints out "Test" twice in a row?
In C++ most features related to character strings assume the string is null terminated, which is implied in string literals. You would need {'T','e','s','t','\0'} to be equivalent to "Test".

Assigning String Literals to Char Pointers [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get rid of deprecated conversion from string constant to ‘char*’ warnings in GCC?
This assignment:
char *pc1 = "test string";
gives me this warning:
warning: deprecated conversion from string constant to 'char*'
while this one seems to be fine:
char *pc2 = (char*)("test string");
Is this one a really better way to proceed?
Notes: for other reasons I cannot use a const char*.
A string literal is a const char[] in C++, and may be stored in read-only memory so your program will crash if you try to modify it. Pointing a non-const pointer at it is a bad idea.
In your second example, you must make sure that you don't attempt to modify the the string pointed to by pc2.
If you do need to modify the string, there are several alternatives:
Make a dynamically-allocated copy of the literal (don't forget to free() it when done):
char *pc3 = strdup("test string"); /* or malloc() + strcpy() */
Use an array instead of a pointer:
char pc4[] = "test string";
That depends on whether you need to modify the string literal or not. If yes,
char pc1[] = "test string";

How is a const char* not a just a character? [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 5 years ago.
Improve this question
I'm really confused about this because personally, I come from a java background, and recently began c++. So, I learnt all the basic stuff, like, printing stuff out to the screen and whatnot, and now, I learnt POINTERS. So, the person on youtube (The Cherno's C++ Pointer tutorial This was not the video where he declared the const char*, just the pointer tutorial that I followed.) I've been following was using this following statement to declare what I know as a 'string'.
const char* str = "random text here";
But, how is a char* converted into a string, and its even using the double quotation marks like a string! Also, what does a constant have to do with any of this? If I remove the const from my code it gives me an error. But, I understand what a pointer is. It is a variable that holds the memory address of another variable, so if one was to access that variable directly, they would just have to do *ptrVarName and dereferenced it. But how can a string "like this one" be a memory address?
Wouldn't I have to do something like this?
char[] str = "string here";
and THEN do:
char* stringPointer = *str;
(WARNING: untested code!)
Thanks in advance.
(oh and sorry if this is a really NOOBY question or the question is poorly constructed, I've just started out with c++ and stackoverflow)
EDIT: Ok, so I understand what the char* str means. It means that when you reference *str, it means that you're accessing the first character in memory. Ok, I get it now. But, what does const mean?
const char* str = "random text here";
On the right hand side, the "random text here" defines a string literal which actually is an array of type const char[17] (including the null terminator character). When you assign that array to const char* str it decays to a pointer that points to the first character. You cannot modify the string literal through the pointer because string literals are stored in read-only memory, so the following would be illegal: str[0] = 'x';
char[] str = "string here";
This one is different. It defines the char array str which has the same size as the string literal on the right hand side (const char[12]). The string literal will be copied into the array str, so you will be able to modify str. In this case, it would be legal to write str[0] = 'x';.
If you declare const char* sth ="mystring"
It will place mystring inthe memory and sth pointing to that its work like array but with direct access to memory
These are plain C-strings. A C string is always null terminated. In other words- it has '\0' at the end. So you need only the place in memory where the string starts and you can find when it ends.
For pointer arithmetic these [] brackets are only syntax sugar. str[] is the same as *str and str[1] is the same as *(str+1) only incrementing the pointer by one char (8 bits) and getting the address of the second element.
C doesn't really have strings. A string is an array of characters, terminated by a nul (0). Now arrays and pointers in C are closely linked, and a char * or const char * usually points to a string, but only in the same way as other pointers usually point to arrays. An individual char * might only point to a single character, you have to know from context, just as an int * might point to one integer or an array of integers.
Because strings are handy, there's a special syntactical rule for strings literal. A string literal in quotes becomes a const char * (in fact its type is char * for backwards compatibility). So in this sense, C has strings. But all that is happening is that the string is being laid out in the data section of the program, then its address taken.

difference between char array and string in cplusplus [duplicate]

This question already has answers here:
What is the difference between a Character Array and a String?
(10 answers)
Closed 9 years ago.
I want to know the difference between character array and string in c++.
Can any one answer to this??
Please,
Thanks
Vishnukumar
string is a class/object, with methods and encapsulated data.
A char array is simply a contiguous block of memory meant to hold chars.
(1) char array is just a block of char type data:
e.g. char c[100]; // 100 continuous bytes are allotted to c
(2a) By string, if you mean char string then, it's little similar to array but it's allocated in the readonly segment of the memory and should be assigned to a const char*:
e.g. const char *p = "hello"; // "hello" resides in continuous character buffer
[note: char c[] = "hello"; belongs to category (1) and not to (2a)]
(2b) By string if yo umean std::string then, it's a standard library class from header and you may want to refer its documentation or search on web

questions regarding char* and char[] basics [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between char s[] and char *s in C?
Difference between char a[]=“string”; char *p=“string”;
Firstly, i would like to ask where can i learn all the basics of char* and char[].
Most of the time i find myself struggling with how to compare and how to declare.
Example 1 :
char *test = "hello world";
This will produce the following warning at compilation :
warning: deprecated conversion from string constant to ‘char*’
Example 2 :
vector<char*> test2;
test2.push_back("hello world");
This will produce an error of copying a string.
So the solution i come up with is :
(is this correct?)
vector<char*> test3;
char *str1 = "hello world"
test3.push_back(str1);
Thanks in advance! :)
============================================
Two good reads provided by people here :
What is the difference between char s[] and char *s?
Difference between char a[]="string"; char *p="string";
Your question "where can i learn all the basics of char* and char[]," is probably too general, but you can try reading the C++ spec.
Fix example 1 by changing it to
char const *test = "hello world";
Fix example 2 by changing it to
vector<std::string> test2;
test2.push_back("hello world");
Or if you really want a vector of non-owning pointers to c-strings:
vector<char const *> test2;
test2.push_back("hello world");
You can learn a lot about char*/char[] in your favorite book on C (not on C++, because C++ provides much better facilities for representing strings than C does).
The declaration/assignment
char *test = "hello world";
should be
const char *test = "hello world";
because string literals are pointers to constants. If you want a vector of strings in C++, you do it like this:
std::vector<std::string> test2;
test2.push_back("hello world");
This works, because string literals can be implicitly converted to std::string.
So the thing to remember is the difference between a pointer to a value and the value itself.
The value itself is the literal value that a variable represents as stored in memory.
A pointer is the address in memory that some value is stored at.
Pointers are useful because they allow you to access, change, and preserve changes in information across multiple functions / methods, which comes in handy when you realize you can only return one value from any function, but sometimes you want a lot more information than that.
The thing about arrays is that, while a lot of people don't realize this when first learning C++, they are pointers. A[0] isn't a variable, it's a pointer to a memory address. Arrays are handy because when declaring an array, you segment off a portion of memory reserved for use for that array. This is useful because it allows you to access the values stored in that block of memory very very quickly.
So really, there's not that much difference between declaring a pointer (char*) and an array (char[]) other than that the pointer will refer to a single location in memory while the array will refer to set of contiguous locations in memory.
To learn more about pointers and how to use them properly, visit http://www.cplusplus.com/doc/tutorial/pointers/ .