problem: string equal to *str

im newbie in c, how do i write a statement (string equal to *str) to let string print "hello"?


#include <stdio.h>

void main()
{
char string[80];
char *str = "hello";

//string equal to *str

printf("%s",string);
}
Last edited on
i have no idea what you post.
char *str = "hello";

You shouldn't do that. str will point to somewhere in memory that is read-only which is dangerous. You should do this:

1
2
3
4
5
6
7
8
char str[] = "hello";

// or if you are going to use a pointer

char *str = new char[strlen("hello") + 1];
strcpy(str, "hello");
// and before your program ends...
delete[] str;
Since he's using C, it isn't a big deal. But if you want to split hairs, const char *str = "hello"; is better.
thx guys, strcpy let my thing done
Topic archived. No new replies allowed.