banner



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.

                      1
2
three
four
five
6
7
8
ix
ten
11
12
13
14
fifteen
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
xl
                                              #include<iostream>                        using                        namespace                        std;                        const                        char                        z[]="Blank";                        void                        callthis(const                        char* g);                        int                        principal(){  	callthis("Exam");                        return                        0; }                        void                        callthis(const                        char* chiliad){ 	z=m;                        // mistake C2440: '=' : cannot catechumen from 'const char *' to 'const char [vi]'                        }                        /* #include<iostream> using namespace std;  void callthis(const char t[]);  const char* x = "Bare";			// Blank pointer to char  int main(){	 	 	callthis("Test"); 	 	return 0; }  void callthis(const char t[]){	 	 	t=x;		// OK }  */                                          

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:

                      1
2
iii
                      1>c:\users\nick\documents\visual studio 2010\projects\datastructuresmalik\ch. i\quickpractice\quickpractice\dognames.cpp(fifteen): error C2440:                        '='                        : cannot catechumen from                        'char *'                        to                        'char [6]'                        i>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\Microsoft.Cpp.Win32.Targets(153,5): error MSB6006:                        "CL.exe"                        exited with code 2. 1>          There are no conversions to array types, although there are conversions to references or pointers to arrays                    

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

t

is a pointer. It might look similar an array but it's a pointer. Both of these are exactly the aforementioned:

                      ane
2
                                              void                        callthis(const                        char* t);                        void                        callthis(const                        char                        t[])                    

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.

                      1
2
3
4
5
half-dozen
vii
eight
ix
10
xi
12
thirteen
14
fifteen
16
17
xviii
19
20
21
22
23
24
                                              #include<iostream>                        using                        namespace                        std;                        void                        callthis(const                        char                        b[]);                        const                        char* temp="Second_Test";                        int                        main(){      	callthis("First_Test");                        //const char a[]="first";                        //a=temp;                        //cout << "a is: " << a << endl;                        return                        0; }                        void                        callthis(const                        char                        b[]){  	b=temp;     	cout <<                        "b is: "                        << b << endl;     }                                          

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.

                      ane
two
3
iv
five
6
7
8
9
10
11
12
thirteen
14
15
16
17
eighteen
                                              #include <iostream>                        #include <string>                        int                        chief(){                        const                        char* string1 =                        "string1";                        const                        char                        string2[] =                        "string2";  string1 = string2;                        // This one works fine.                        string2 = string1;                        // If y'all hover over string2 (should exist underlined past red), it volition tell y'all                        // that the expression must be a modifiable value, merely it gives the aforementioned                        // error when you try compilation: C2440                        std::cin.get();                        return                        0;  }                    

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:

                      one
2
3
4
v
6
vii
8
9
10
11
12
13
14
15
xvi
17
18
xix
20
21
                                              #include <iostream>                        #include <string>                        int                        main(){                        const                        char* string1 =                        "string1";                        const                        char                        string2[] =                        "string2";  string1 = string2;  string2 =                        "Vlad567";                        // string2 hover-over displays exactly:                        // "const char string2[8] = "string2"                        // Error: cord must be a modifiable lvalue (lvalue not a typo)                        string2 = string1;                        // aforementioned hover-over message                        std::cin.get();                        return                        0;  }                    

When compile attempted, this comes out:

                      ane
2
                      error C2106:                        '='                        : left operand must be l-value  (at line string2 =                        "Vlad567";) fault C2440:                        '='                        : cannot catechumen from                        'const char *'                        to                        'const char [8]'                        (at line string2=string1;)                    

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:

                      one
ii
3
four
v
half-dozen
vii
8
9
10
11
12
thirteen
14
fifteen
sixteen
17
eighteen
19
20
21
22
23
24
25
26
27
28
29
thirty
31
32
33
34
35
36
37
38
39
                                              #include<iostream>                        using                        namespace                        std;                        const                        char                        z[]="Blank";                        // z is now prepare equally a constant char array - it CANNOT be changed.                        void                        callthis(const                        char* chiliad);                        int                        main(){  	callthis("Test");                        render                        0; }                        void                        callthis(const                        char* m){                        // one thousand points to the constant char array "Examination"                        z=g;                        // fault C2440: '=' : cannot convert from 'const char *' to 'const char [vi]'                        }                        /* #include<iostream> using namespace std;  void callthis(const char t[]);  const char* x = "Bare";			// ten POINTS to constant string "Blank"  int main(){	 	 	callthis("Test"); 	 	render 0; }  void callthis(const char t[]){	// const char t[] = "Test".....OK, I lose.  Meet comment at end. 	 	t=x;		// OK }  */                                          

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.

                      ane
2
iii
4
five
vi
7
eight
9
10
11
12
13
14
fifteen
xvi
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
                                              #include<iostream>                        using                        namespace                        std;                        const                        char                        z[]="Blank";                        // z is set to a constant character array.  Information technology cannot change period.                        void                        callthis(const                        char* m);                        int                        main(){  	callthis("Test");                        return                        0; }                        void                        callthis(const                        char* m){                        // m is a pointer to a the const character array "Test"                        z=1000;                        // error C2440: '=' : cannot convert from 'const char *' to 'const char [6]'                        // Here, const char [vi] = const char * is not legal.                        }                        /* #include<iostream> using namespace std;  void callthis(const char t[]);  const char* x = "Blank";			// x is a pointer, pointing to a constant char array with values "Blank"  int chief(){	 	 	callthis("Test");     // literal string passed 	 	render 0; }  void callthis(const char t[]){	// This is the key.  Passed by value which essentially means this is a Pointer to the original data being passed - "Test" - and Not that actual string.   	 	t=x;		// OK  // when y'all hover-over t in this argument, says "const char * t" //  And that's the game!  Here, const char * = const char * is perfectly legal. }  */                                          

Last edited on

I repeat what I said earlier. t is a arrow. These ii function declarations are 100% equivalent.

                                          
                                              void                        callthis(const                        char                        t[]);                        // t is a pointer                                                                  
                                          
                                              void                        callthis(const                        char* t);                        // t is a pointer                                                                  

I understand this tin exist disruptive because when you declare an array these ii ways of writing it are not equivalent:

                                          
                                              const                        char                        x[] =                        "Bare";                        // x is an array                                                                  
                                          
                                              const                        char* x =                        "Blank";                        // x is a pointer                                                                  

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel