A question between Array and Pointer
Program 1:
#include <iostream.h>
int main()
{
char *nz;
nz="hello";
cout << nz << "\n";
return 0;
}
Program 2:
#include <iostream.h>
int main()
{
char nz[6];
nz="hello";
cout << nz << "\n";
return 0;
}
Isnt that,they are the same?howcome the first one run without any error,but the second one got an error?
Re: A question between Array and Pointer
Quote:
Originally posted by Unregistered
Program 1:
#include <iostream.h>
int main()
{
char *nz;
nz="hello";
cout << nz << "\n";
return 0;
}
Program 2:
#include <iostream.h>
int main()
{
char nz[6];
nz="hello";
cout << nz << "\n";
return 0;
}
Isnt that,they are the same?howcome the first one run without any error,but the second one got an error?
With the second you are trying to assign a r-value to an l-value. To do what you want to do you have to use the function strcpy( );
strcpy( nz, "Hello" );