HELP! O___________________________________O

Started by
8 comments, last by jbadams 16 years, 3 months ago
zomg im having so many problems coz when i do this

#include<iostream>
using namespace std;
int main()
{
    char *pointer = 0;  //yeah its null, ill just call it 0 lol
    strcpy(pointer,"hello world"); 
    cout<<helloworld;
    return 0;
}

i get an error and idk how to fix it so help me plz thx u very much ^_^
Advertisement
Well, exactly what is your compiler telling you? I find that 99.987% of errors are easily traced using the garbage that your IDE or compiler spits up at you.
But you cannot strcpy into a NULL pointer. It's like trying to fill air with water. You need a bucket first. Use std::string, or look up dynamic memory allocation. You can new the char array, then delete it when you are done, but that's a little complex for someone "hello world"ing. Go with std::string. Simple, easy, painless.
#include <iostream>#include <string.h>int main(){std::string hello; //this is a stringhello = "hello world";std::cout<<helloworld<<std::endl;return 0;}


That should do it, might not compile, haven't tested it.
___________________________________________________Optimists see the glass as Half FullPessimists See the glass as Half EmptyEngineers See the glass as Twice as big as it needs to be
#include<iostream>using namespace std;int main(){    char *pointer = new char(12);  //yeah its null, ill just call it 0 lol    strcpy(pointer,"hello world\0");     cout<<pointer;    delete [] pointer;    return 0;}
o ok thx i didnt no taht
thx u very much ^^
Fitfool:
Your code won't compile, new uses brackets ( [] ) not parenthesis ( () ). It should be
char* ptr = new char[12];


You delete it correctly though.
___________________________________________________Optimists see the glass as Half FullPessimists See the glass as Half EmptyEngineers See the glass as Twice as big as it needs to be
Quote:Original post by Plasmarobo
Well, exactly what is your compiler telling you? I find that 99.987% of errors are easily traced using the garbage that your IDE or compiler spits up at you.
But you cannot strcpy into a NULL pointer. It's like trying to fill air with water. You need a bucket first. Use std::string, or look up dynamic memory allocation. You can new the char array, then delete it when you are done, but that's a little complex for someone "hello world"ing. Go with std::string. Simple, easy, painless.
*** Source Snippet Removed ***

That should do it, might not compile, haven't tested it.


Your code has a few problems. First of all, the correct header is <string>, not <string.h>. Second of all, you should be initializing your string with a value. Declaring it and then assigning it a value one line later is kind of asinine. Most importantly, you are referencing and undeclared variable named helloworld on your second to last line.

But, really, all of this is overkill for hello world:
#include <iostream>int main(){    std::cout<<"Hello World!"<<std::endl;    return 0;}


Original poster: In general, you want a specific topic name, so that someone can tell just by reading it whether or not they can help you. Also, internet lingo (zomg et al.) makes me want to gouge my eyes out, but I suppose that's more personal preference.
Just use std::string. If you couldn't spot the error in that code straight away, and don't have any need to sacrifice convenience and safety for some other benefit, then you're better off with a proper string rather than an array of characters.

#include <string>#include <iostream>int main(){   std::string foo = "Hello, ";   std::string bar = "world.";   std::cout << foo + bar << std::endl;  // "Hello, world."}


The above would be bloated by another dozen or more lines were you to use char* 'strings' and expect some similar level of safety (although, I guess, the above isn't perfect: I think foo + bar can technically throw an exception if there's not enough memory).

Also, your identifiers are messed up (you have a char* called pointer, and nothing called helloworld).

In future, you may also want to post up the actual error that gets reported, so that we can help if the problem isn't so clear. :-)
[TheUnbeliever]
Quote:Original post by Plasmarobo
Fitfool:
Your code won't compile, new uses brackets ( [] ) not parenthesis ( () ). It should be
char* ptr = new char[12];


You delete it correctly though.

It certainly will compile as it is a perfectly legal and well defined line of code. What it does, however, I will leave as an exercise to you to figure out. Makes using wrong parantheses, or even believing that you can't use wrong ones without compiler error, quite dangerous in this case.
Ok, here's the deal - I'm pretty sure you're actually a duplicate of a banned member trying to mess with people, and I'll be forced to remove you unless you convince me otherwise, which will at a minimum involve the following:
  • Title your threads properly. Everyone is looking for help, and the very long "smiley" is unneccesary and annoying.

  • Put in some effort to type properly. Sentences begin with a capital letter and end with a period or other punctuation, and things like "coz", "plz" and "thx" belong (if anywhere) in sms messages rather than forum posts you've typed with a full keyboard, type proper words instead.

- Jason Astle-Adams

Screw it, on closer examination of your other thread you're definitely just messing with people, goodbye Ryan.

- Jason Astle-Adams

This topic is closed to new replies.

Advertisement