is using char* the best method for a varying length string?
i dont think it is because ive tried to use a char* with sprintf & i get null pointer assignment.
whats an alternative? make a really big char array?
char*
Moderator: Board moderators
-
- New poster
- Posts: 25
- Joined: Wed Jun 05, 2002 4:55 am
- Location: London, ON, Canada
- Contact:
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
Did you allocate memory or just used the pointer without doing so? In the latter case it's no wonder you get a "null pointer exception" (I assume that's what you got, not an "assignment").
Solutions if you need to make a string longer:
- Reallocate memory.
- Use a large array from the start.
- Use string objects of C++.
Solutions if you need to make a string longer:
- Reallocate memory.
- Use a large array from the start.
- Use string objects of C++.
-
- New poster
- Posts: 25
- Joined: Wed Jun 05, 2002 4:55 am
- Location: London, ON, Canada
- Contact:
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
You can use the old C functions malloc, calloc, free and realloc, but I'd suggest the newer C++ variants:
char *str;
str = new char[1000];
... (use str)
delete[] str;
str = new char[10000];
... (use str)
delete[] str;
Try writing "bool" (lowercase letters).
Using a string:
string str = "abc";
cout << str << endl;
str += "def";
cout << str << endl;
char *str;
str = new char[1000];
... (use str)
delete[] str;
str = new char[10000];
... (use str)
delete[] str;
Try writing "bool" (lowercase letters).
Using a string:
string str = "abc";
cout << str << endl;
str += "def";
cout << str << endl;
-
- New poster
- Posts: 25
- Joined: Wed Jun 05, 2002 4:55 am
- Location: London, ON, Canada
- Contact:
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
-
- New poster
- Posts: 25
- Joined: Wed Jun 05, 2002 4:55 am
- Location: London, ON, Canada
- Contact:
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany