How To Upload A File And Assign It To A Char Array
In both programs I am trying to assigning pointer to char to that char arry.
In second case program correctly. simply in commencement instance showing error.
| |
An array is not a pointer then you can't assign the arrow to the array. If you desire to copy the chars you tin use std::strcpy role (assuming the chars represents a naught terminated string, aka "C cord", which is the case in your program).
http://www.cplusplus.com/reference/cstring/strcpy/
Also check the fact that it is a constant.
Aceix.
then how does second program working ?
I didn't THINK the second 1 should be OK since it is assigning a value to a abiding in the body... then I set the program in my VS environs. When I mousover the z (with a ruby-red line nether it) in the start program, it says: "const char z[six] = "Blank" Error: expression must be a modifiable value".
I got rid of all const labels, and it says the same thing. Afterwards a lot of looking, I plant the answer SORT of hidden when I tried to run the offset programme. Read on and you should encounter this too in VS2010 in the log:
| |
And then that last line there means that conversion can get one way, but only non the other. Ta-da! But withal, it is strange to me that you state it is constant but can change the value in the 2d plan.
Last edited on
I volition upload compiled screen image file and output file.
can someone tell me how to upload the file ?
santosh anantwal wrote: |
---|
can someone tell me how to upload the file ? |
You tin upload images to imgur.com (or some other image hosting service) and link to it in your post, or why not copy paste the mistake message here?
santosh anantwal wrote: |
---|
then how does 2nd program working ? |
Note that
tis a pointer. It might look similar an array but it's a pointer. Both of these are exactly the aforementioned:
| |
I suspect you have made a mistake because yous are assigning ten to t which is kind of pointless because the t pointer will stop to exist afterward the function has ended. You probably meant to assigning t to x: x=t;
Note that in the second program the pointer ten is not constant. It is what the arrow points to that is const. To make x const you would declare it as: const char* const 10 = "Bare";
Last edited on
@ Peter87 (6160)
I know that t arrow will stop to exist after part has ended.
but if char array and pointer are same and if
t=ten is possible in function trunk, and then why not z=m ?
regarding with constant pointer and arrow to constant, I know both, but my focus is on assignment.
Here is 2nd program compilation image
"http://imgur.com/Ysk870G">
and here is output
"http://imgur.com/2GLRYum">
santosh anantwal wrote: |
---|
only if char assortment and pointer are same and if t=x is possible in role body, then why not z=m ? |
Because an array is non a pointer.
ok
but my question is,
if t=x is possible in function body, then why non z=grand ?
Hither is simplified version of above two programs combined.
| |
OK, I thought I gave y'all the answer but perhaps it was either incorrect or not clear plenty? Hither it is again.
The output from the log when the program fails to build explicitly states that:
ane) There are no conversions to array types, and
ii) At that place are conversions to references or pointers to arrays.
Upon a little more than examination, a search for error code C2440 is referenced here: http://msdn.microsoft.com/en-the states/library/sy5tsf8z.aspx
That page states in the first office that a literal string in C++ is a const char array. At that place is no conversion from a const char pointer to a const char array (which is the messsage y'all got). Yet, a conversion vice versa is doable; in other words, a const char assortment tin be converted to a const char pointer, and that is the simplest answer y'all're likely to become.
Once again, a literal string is a const char assortment. A const char array can be converted to a const char pointer. But a const char pointer is not a literal string (a const char assortment), nor can it exist converted into one.
:)
Concluding edited on
Information technology would be very clear if you give examples from above program
1) In that location are no conversions to assortment types, and
2) There are conversions to references or pointers to arrays.
three) There is no conversion from a const char arrow to a const char array
4) a const char array can exist converted to a const char pointer
5) a literal string is a const char array.
vi) A const char array can exist converted to a const char pointer
7) But a const char pointer is non a literal string (a const char array)
OK, but I desire you lot to know that I came upwards with some different conclusions, fashion easier ones. I besides want to say here that I don't know everything obviously, but that I am piecing together a solution from different sources. Enjoy my train of thought. This i'southward a doozy.
***If anyone wants the terminal answer, just go down to the last code window***
Here is a much less complicated plan I came upward with, similar to yours, and throwing the aforementioned error.
| |
I tried unlike things to see what else I could find out, so I tried to make string2 = another string instead. string2 was nonetheless underlined with the same bulletin, and the compiler throws an error proverb "cannot catechumen from const char [ane size] to const char [some other size]", equally I tried strings of unlike sizes. Then I matched the number of letters, like hither:
| |
When compile attempted, this comes out:
| |
I looked up that first error, which is a different kind, and constitute this page: http://msdn.microsoft.com/en-united states of america/library/wxy5f14h.aspx
Which gives this example of what tin can throw this error, namely something like:
i = a;
SOOO what I am getting from this is that one time a const cord array is defined, that is information technology. Information technology cannot be inverse catamenia. Whenever you lot see information technology once more, you lot might also type in the literal string it represents.
On the other manus, when a const char pointer is divers, it CAN exist changed. Check out this other forum topic: http://www.cplusplus.com/forum/beginner/10602/
At that place, kbw states that const char * is a pointer to a constant string. A const char * tin point to a unlike string, just the string information technology is currently pointing to cannot change.
I promise this has answered your question and gave some description.
Referring to your original code:
| |
I still couldn't figure it out. So when you hover-over t in the to a higher place argument, information technology says guess what? It IS a const char *. I don't know why. It seems like information technology should be read as a literal string, but my guess is that when you are passing the string equally an assortment, since y'all are non passing it by address, t is simply a pointer to the original cord yous had listed.
In other words, a pass by value is the same thing as pointing to what is being passed, essentially pregnant information technology is a pointer to the array, which Means that the line t=x is legal. So Now, here is your original code once again with fastened final comments. Enjoy.
| |
Last edited on
I repeat what I said earlier. t is a arrow. These ii function declarations are 100% equivalent.
| |
| |
I understand this tin exist disruptive because when you declare an array these ii ways of writing it are not equivalent:
| |
| |
Really, above you said an array is not a pointer, but likewise the same matter as this post. You didn't say WHY const char t[] in the function telephone call is a arrow, which is why we remained confused.
Thanks everybody.
particularly nickeeromo. your explanation of WHY things happen that way, was most important for me.
and Peter87'south last 4 lines summarized everything.
Source: http://www.cplusplus.com/forum/beginner/152858
Posted by: colemanpaland.blogspot.com
0 Response to "How To Upload A File And Assign It To A Char Array"
Post a Comment