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
What is the effect of putting an asterisk (*) in front of a variable name such as in the following line of code:
char prompt = 'y' vs. char *prompt = 'y'?`
Edit: When I originally posted this question, I thought that I saw this asterisked variable in a Java sample program that came included in Netbeans. That was my mistake -- It was C++ code sample in Netbeans. I was searching for an answer in some Java documentation and could not find it which led me to ask the question here. In the end, it was a trick question that I didn't realize was tricky at the time.
The answer is that the asterisk has something to do with "pointers", which has to do with memory addressing better explained by anyone but me. And, they are not used in the Java language but most certainly are in C++.
char prompt="something" is wrong since char is only one byte long so you can only assign it something that is one byte long like
char prompt='y'
More elaborately "something" is represented in c as a array of character so compiler will also shout that you cannot assign const char[10](note length is 10 since a null character is appended at the end of the string by the compiler) to char.It is just like putting of buckets of apples to a single apple and result will be total car crash.
On the other hand char* prompt ="something" is pointer to a character.Here prompt is pointing to the string "something" you can also use indexing on it like prompt[0],prompt[1] which will result in s and o respectively.
I am also assuming that you mistakenly tagged your question to java.
That means it's a pointer. There is no pointer in Java.
So if you have char *str, in str you don't have something like "Hello World" but the memory address where the string "Hello World" is.
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 11 months ago.
Improve this question
I'm a beginner and I absolutely can't figure out how some string returns a pointer to its location in memory?
char* str = "okay"
why is it a pointer? After all, we don't even use new
how is char able to store an address
It doesn't, it's a pointer to a char.
how some string returns a pointer to its location
String literals are (generally) constant character arrays stored in a read-only section in memory that's mapped to a section in your executable. More details are OS-specific, but what's important is that string literals decay into a pointer to their 1st character, just like any other array.
After all, we don't even use new
new and malloc() return pointers, sure, but they're not the only source of pointers. As you see in your code, string literals are a source of pointers as well, and nothing stops you from writing stuff like char *p = (char *)0x800; if you want, which is something you see often in kernel or real-mode programming.
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 3 years ago.
Improve this question
Please sorry, I am JavaScript and TypeScript guy, not a c++ one.
But the JS engine V8 is written in c++ and here is a code piece from there:
// Convert the result to an UTF8 string and print it.
v8::String::Utf8Value utf8(isolate, result);
printf("%s\n", *utf8);
In the code above there are two lines.
First line contains utf8 function... where does it come from? I didn't see it before in the file and it wasn't imported (or was it)?
Second line contains utf8 variable (right?), though with * modifier which I am not aware of. Where did the variable come from? What is the role of the star modifier?
Sorry for this kind of questions, but at this point I cannot delve into the documentation of one of the most complex languages, which is c++...
utf8 is not a function, but a variable. The snippet (isolate, result) is the arguments passed to its constructor.
This could be rewritten as follows to be functionally identical, and in a way that is more familiar to a JavaScript programmer:
auto utf8 = v8::String::Utf8Value(isolate, result);
where auto infers the type of a variable.
As for the * in *utf8, the meaning of this will depend the implementation. * as a prefix operator can be given a user-defined meaning, though usually it has the semantics of "reach into and get the value from," as with raw pointers and things like std::unique_ptr and std::optional.
I'm not familiar with v8 personally. You should look for documentation on the * operator for the v8::String::Utf8Value type, to see exactly what it does.
You should also be very aware that C++ takes a long time to learn, and it's terribly easy to misunderstand or do things very wrong. If you want to commit to learning C++, I would suggest reading a good C++ book to get a foundational understanding.
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'm trying to understand this library that stanford uses for their CS course but i can't just use this feature in my CS course back in my home. How do i emulate this feature? i was wondering is this a getline(cin, integer)? or is this something to do with fstream? The reason being, i'm trying to go through the lecture series at UDEMY.
Here is the definition.
int getInteger(string prompt = "");
Reads a complete line from cin and scans it as an integer. If the scan
succeeds, the integer value is returned. If the argument is not a
legal integer or if extraneous characters (other than whitespace)
appear in the string, the user is given a chance to reenter the value.
If supplied, the optional prompt string is printed before reading the
value.
Usage:
int n = getInteger(prompt);
You can download simpio.h here: http://www.eecs.wsu.edu/~cs150/prog/libs.htm
If you can't or don't want to use the library, you could study its source code.
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 have a string on Linux standard terminal input. Its maximal length is 10^200 (as said in program specification). I have to count, how many "3" characters are inside it (occur in this string). I couldn't do that by for loop, because there is no so big variable type, which can be used as iterator. Is there any way to analyze so big strings?
Is there any way to analyze so big strings?
Not in this universe there is not. Such an entity cannot exist in this universe1 and that which does not exist, cannot be analyzed.
1 Current estimates of this universe's total number of particles particles are in the region of 1080.
As from your comment
The data source is standard terminal input.
Then you'll need a lot of monkeys to type this in.
Though you don't need to read in what's typed at once into a big string, but you can simply analyze char by char as typed. The std::istream::get(char_type& ch) method is suitable for doing so.
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 9 years ago.
Improve this question
I'm trying to make something in WinAPI but I get big loads of errors, corrected most of them but i cant find solution to that one:
for (LPINT i = 1; i <= ilosc; i++)
The compiler shows that error is is the "1".
I would post the entire code but its so unorganized that it would be hard to find this line in it the loop for declares its own variable so i think that I'm using the wrong type of variable (no clue what would be correct)
I changed the names of every variable I have to LP* and it solved almost all of my problems except this one.
Also if someone is well oriented in the topic of WinAPI could you teach me how to declare a variable in a textbox? I want to use textboxes as input sources for the variables so the program would count on numbers I type there? (the most of tutorials is written in very scientific language so I cant clarify myself good)
Also I know how bad at typing I am and that it lacks capitalization, punctuation, etc (I wasn't listening on language lessons in primary school and this mistake keeps showing off so don't rage at me cause I'm fixing it).
LPINT is a long pointer on int. Thats why you cannot assign a 1 to that variable.
LPINT is a typedef for typedef int *LPINT (http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx)
It's a pointer, not an int.