Can someone please explain what kind of whatever this (char*) is?
Can someone please explain what kind of whatever this (char*) is?
It's a pointer to an array of characters.
No, it's a pointer. It could be either a pointer to an array of char or a pointer to a single char. Without more content you can't tell which.It's a pointer to an array of characters.
zach, 15 hours ago you made a thread called "puzzled by char*" and then you said my response cleared up a lot for you.
8 hours ago, you made this thread with 1 sentence asking what a char* is again. It may be a good idea to condense these down to 1 thread so that you can just get all of your very related char* questions answered.
As I wrote in the other thread, char* is a POINTER (which is a variable that holds a memory address). This pointer holds the memory address of a char data type. As mentioned above, it can be a single char, or it can be a char array or string because those just have multiple contiguous chars in a row.
When you see (char*) exactly like that in code, this means that the programmer is casting something to a char*. Look up C casting for more info on a search engine.
If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!
More accurately, a char* is a pointer to a char, with a few caveats. This char may or may not be part of an array of char. The caveats would be that instead of actually pointing to memory that can be validly interpreted as a char, the pointer might point one past the end of an array of char, or it could be a null pointer, or it could be invalid. Nonetheless, in terms of type it remains a pointer to char. Loosely speaking, we commonly say that such a pointer points to an array of char or to a string when it points to the first char in an array or string, but strictly speaking a pointer to an array of char is a different type (i.e., char (*)[N] for some array of N chars).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Laserlight, is this what you are saying?
"In general a pointer points to an array when it points to the first char of that array.
Specifically, a char* is a pointer to a char That char might be a singular char or might be an array of char.
The pointer could be a null pointer, be invalid, or point one past the end of an array of char."
These 3 sentences are crystal clear to me... You could think about pointers as objects which holds a memory address. Since a process has its own "address space", an address can be valid (inside the process "address space"), invalid (outside the process "address space") or NULL (zero, usually - which is an invalid address as well).
An address "points" to something in memory. This "something" have size and that's what you specify in the pointer declaration:
Here, the variable p is a pointer (holds an address) which points somewhere in memory (when initializaed). But, when used to access memory, it will read/write chars. When you do some arithmetic with p variable (+ or -) it will take in consideration the size of data pointed, like:Code:char *p;
So q now points to the second element of the array b!Code:char a[] = "fred"; int b[] = { 1, 2, 0 }; char *p; int *q; p = &a[0]; // p now have the address of the first element of array a q = &b[0]; // q now have the address of the first element of array b. p++; // increment p (the address which p holds) in 1 unit (sizeof(char)). q++; // increment q (the address which q holds) in 4 units (sizeof(int)).
PS: Multiply and divide the pointer by an value isn't wise (and the compiler will issue an warning)...
Maybe it is better do draw...
In the fragment:
p and q ponits (holds the address) of the first elements of the arrays:Code:char a[] = "fred"; int b[] = { 1, 2, 3, 0 }; char *p = &a[0]; int *q = &b[0];
After incrementing p and q:
This will happen:Code:p++; q++
It is better to see the integers, in little endian format and what happens to q after it is incremented:
Last edited by flp1969; 08-21-2019 at 04:38 PM.
flp1969,
*All* I was asking was whether the quote below was a correct understanding of what silverlight had said. I **wasn't** asking what an ordinary pointer is. In my perception a char* isn't just an ordinary pointer to something. Nevertheless, thank you for responding.
"In general a pointer points to an array when it points to the first char of that array.
Specifically, a char* is a pointer to a char. That char might be a singular char or might be an array of char.
The pointer could be a null pointer, be invalid, or point one past the end of an array of char."
Re You're welcome.... Who's "silverlight"?
Silverlight is a contributor to this forum who really knows his stuff.
Re: Don't you mean laserlight? And her stuff? Yes of course.