-8

Is there any way to assign a quoted string to a pointer as following, without having it cast to const char*?

char** p;
*p=new char[100];
*p="quoted_string\n";

The problem with the above is that p is now a const string and I can't delete blocks from it:

delete p[n]; //gives an error--- block_is_valid...

I know about string literals such as L"string" but I couldn't find anything to solve my problem, though.

CLARIFICATION: I clearly stated that I'm looking for a method that is at the level of simplicity present in the provided example. I know that I can iterate through the string and copy it, but that's not what I'm looking for.

And for those confused about the placement of delete, I use it for something like this:

while(*p!=char_01) //this is not an example to be taken literally
{delete p; p++;}
user3140280
  • 313
  • 1
  • 3
  • 11

3 Answers3

6

Use an std::string:

std::string p;
p = "quoted_string\n";

you can then delete a character using:

p.erase(p.begin() + n);

The problem with your code is that in:

char* p;
p=new char[100];
p="quoted_string\n";

you are leaking the 100 byte memory by forgetting about the dynamically allocated pointer. Also delete should not be used to delete characters from a C-style string.

I also suggest you to read a book before going any further, because you seem to lack the very basics of C++.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
5

C++:

#include <string>
...
std::string str = "quoted_string\n";

possible C-style approach:

char str[] = "quoted_string\n";

Note that these are basics covered in any good book: The Definitive C++ Book Guide and List
Also note that L"string" is not what you think it is. Have a look at string literals.


Ugly C-style solution with dynamically allocated buffer (!!! you don't really want to do this !!!):

#include <cstring>
...
char* str = new char[100];                // destination buffer
const char* src = "quoted_string\n";      // pointer to constant string literal
strcpy(str, src);                         // strlen(src) must be <= 99 
...
delete[] str;
Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
4

For casting a quoted string to a pointer, you may do: char* p = (char*) "Hello World";

This silence the warning, HOWEVER, by doing this you lie to the compiler. And you will get bite when you later write to that region.

To see how seriously wrong this is, you'll need to understand that most toolchain defaults to put literal string on read-only section (.rodata) of your executable, that region is mapped read-only, and writing to that region cause a protection fault which default to terminate the application. So, to prevent human error the compiler is very nice to generate a warning for that.

If you absolutely know what you are doing, you may however tell the linker to put that literal string on a writable section, or try -fno-const-strings.

You wrote:

while(*p!=char_01) {delete p; p++;}

Now I see a lack of understanding of how pointer, string and delete work. If you want to manipulate string, it's easier to do it with string class, you'll get tons of tutorial with your favor search engines.

Non-maskable Interrupt
  • 3,841
  • 1
  • 19
  • 26
  • If that code doesn't deallocate the memory at p[n], then what does it do? If it does, what's wrong with it, and how does it demonstrate lack of understanding of how pointers work? Please explain. – user3140280 Feb 05 '14 at 17:31
  • delete p will invoke the delete function and attempt to deallocate the memory pointed by p, however, depends on implementation, delete p should most likely fail due to sanity check and discovered p is not allocated by its allocator/new. (note: most allocator add a prefix meta-block). Furthermore, by trying to inspect the non-existing meta-block the delete function may also trigger a page fault if you are lucky. – Non-maskable Interrupt Feb 05 '14 at 17:38