sorrie im new at this.. but whats wrong with my code?
#include <iostream.h>
#include <string.h>
main()
{
char *test;
test = NULL;
*test = 'x';
cout << *test << endl;
}
Printable View
sorrie im new at this.. but whats wrong with my code?
#include <iostream.h>
#include <string.h>
main()
{
char *test;
test = NULL;
*test = 'x';
cout << *test << endl;
}
Try;Quote:
#include <iostream.h>
#include <string.h>
main()
{
char *test;
test = NULL; //here you are assigning the pointer to NULL
*test = 'x'; //Now you are trying to fill a NULL pointer...not good
cout << *test << endl;
}
Quote:
#include <iostream.h>
#include <string.h>
int main(void)
{
char *test = new char;
//test = NULL;
*test = 'x';
cout << *test << endl;
delete test;
}
how bout this one though :( :
#include <iostream.h>
#include <string.h>
main()
{
char *str;
str = new char[10];
strcpy(str, "hello");
delete [] str;
cout << str << endl;
char *str2 = new char[10];
strcpy(str2, "dolly");
cout << str << " " << str2 << endl;
}
Ummm... what are you trying to do here?
Take out the 'delete' line (what's it there for?) and try it again.
Almosthere
> delete [] str;
Any reference to str after this point is broken
Whether you use int main() or int main(void), you'll need to return, usually return 0;
As far as I know, you can't use main() or main (void), You can use void main()
> You can use void main()
No you can't (not unless your compiler is also broken)
I just ran this code on MSVC 6.0 and it ran fine. I know it's not recommended, but it works.
Code:#include <iostream>
using std::cout;
void main()
{
cout << "Hello world";
return;
}
Just because some piece of code compiles and runs doesn't mean that its legal c/c++. Read a copy of the c and c++ standards. main() always returns an int.
The value of this int is seen usually as an error code by the operating system. Returning a value of 0 means that your app finished its work all well and good.Any non-zero value can indicate to the operating system that an error occurred during the running of your app.