-4
  1. Is there any way to extract the first (or second, third, whichever) symbol of a string, using only a char* pointer to it?

    char* str = "abcde";
    char* symbol;
    
    // Some actions
    
    std::cout << *symbol; // a in output
    
  2. Is there any way to change a certain symbol of a string, using only a char* pointer to it?

    char* str = "abcde";
    
    // Some actions;
    
    std::cout << str; // 1bcde in output
    

    UPD: Note that, according to the task given, I should do it by using char*, not char[] (although if it were not a task, I would definitely use char[]).

vitdev
  • 11
  • 3
  • 1
  • 3
    I think you need a good [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), because those questions are pretty basic and it's a bit better to learn from books than from the internet. – Rakete1111 Aug 06 '17 at 13:42
  • Do char str[] = "abcde" and then you can access it as you would an array. – Eyal K. Aug 06 '17 at 13:43
  • @EyalK. A pointer would be as good, the problem is the `const`ness in the initiaization. – user0042 Aug 06 '17 at 13:45
  • he declared char *, not const char *. I know that he initialized it with a literal, but my memory of this (which, granted, goes back a few years) would not put it in the const/read only memory space. Would this even compile properly? – Basya Aug 06 '17 at 13:49
  • @BasyaPerlman - Your memory is wrong. As a matter of fact, you cannot have a non-const pointer to a literal in C++. – StoryTeller - Unslander Monica Aug 06 '17 at 13:50
  • Pointers and arrays are very much interchangeable in most cases. And you can use pointer arithmetic with an array to get an element, or use array indexing with a pointer. – Some programmer dude Aug 06 '17 at 13:53
  • @StoryTeller -- is this C++11? Because I've just Googled around and it apparently was once permitted. Not to say that it was ever a smart thing to do :-) – Basya Aug 06 '17 at 13:59
  • 2
    @BasyaPerlman All string literals are considered *constant* arrays of characters. It has been like that since at least C++ was standardized. – Some programmer dude Aug 06 '17 at 14:01
  • OK, well, I go back before that :-) I'll update my memory :-) Seriously though, of course a string literal is a constant array of characters. The question is what the compiler does with it when you do something which doesn't really make sense (like set a char * to a string literal) – Basya Aug 06 '17 at 14:01
  • I don't know if the compiler is required to produce a "diagnostic" (a warning) but most good compilers do. – Some programmer dude Aug 06 '17 at 14:05
  • @vitdev: in your example, you are using a string literal ("abcde"), which is constant. But your questions seem to be asking about char * in general. Which is it? Perhaps you are just using the string literal for your example, but it is not really part of your task? – Basya Aug 06 '17 at 14:07
  • I can confirm that g++ produces a warning of _warning of: _"ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]"_, Visual C++ 2013 produces no warnings. – Ron Aug 06 '17 at 14:07
  • @Ron - If GCC produces that warning, it means it's compiling with extensions. That is not standard C++. – StoryTeller - Unslander Monica Aug 06 '17 at 14:08
  • @StoryTeller I see. Tried it both on [Coliru](http://coliru.stacked-crooked.com/a/15d2b6a1726650d4) and local Ubuntu machine. Same results. – Ron Aug 06 '17 at 14:10
  • Visual C++ has historically been too permissive ... – Basya Aug 06 '17 at 14:11
  • @Ron - You don't follow. I'm not questioning GCC permits it, it *had* good reason to (not to break legacy code). What I'm saying, is that it should not permit it under strict compliance (more so now). – StoryTeller - Unslander Monica Aug 06 '17 at 14:12
  • Consider compiling with [-Wpedantic], so your compiler can inform you that: "ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic] char* s = "abcde";" _____ I use "-Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -Wcast-align -Wcast-qual -Wconversion -Wpointer-arith -Wunused -Woverloaded-virtual " in my default build, all useful warnings. – 2785528 Aug 06 '17 at 16:17

3 Answers3

1

The problem you have is that this line

char* str = "abcde";

is wrong, and the compiler even should have complained about that. It should be

   const char[6] str = "abcde";
// ^^^^^

because you can't change anything in the literal.
Or simply do:

   const char[/* length is implicit */] str = "abcde";

anyway you'll end up with a const char[6] array variable.

  1. Is there any way to extract the first (or second, third, whichever) symbol of a string, using only a char* pointer to it?

To access a single character you can use just indexing ...

std::cout << str[0] << std::endl; // prints a
  1. Is there any way to change a certain symbol of a string, using only a char* pointer to it?

... but you can't change that value by any means.

user0042
  • 7,917
  • 3
  • 24
  • 39
  • const char[6] str = "abcde"; is very brittle; you have to count the characters and it breaks easily in maintenance. Why not let the compiler count it for you? – Basya Aug 06 '17 at 14:12
  • Yes! That looks much better to me. – Basya Aug 06 '17 at 14:50
0

OK, you are asking questions, and giving example code. As others have mentioned, there is a bit of a mismatch.

In your example code, you write:

char* str = "abcde";

The string literal can't be changed (and it is being assigned to a pointer which is not const; this is an error, as others have mentioned).

Yet you explain that you were given a task which requires changing it!

So, either: 1. Your example to us does not really reflect the task or 2. You are misunderstanding the task or 3. The task is incorrect.

Perhaps the task expects you to work with char *, and you decided on your own to initialize it as you did.

If so....

You need to define a char * which can be modified. For example:

char str1[MAX_CHARS]; // you'll have to define MAX_CHARS as something reasonable
strcpy(str1, "abcde"); // you'll have to make sure that str1 is large enough
char * str = str1;

that is just one example. You can allocate str on the heap if you want, using "new".

Once you have a modifiable string, you can do the things you asked: 1.

char* symbol = &str[0];

std::cout << *symbol; // a in output

or, more directly:

std::cout << str[0]; // a in output -- add an end of line if you need it
  1. Modify the string:

    char* str = "abcde";

    str[0] = '1';

    std::cout << str; // 1bcde in output

This is, again, once you have a modifiable string. I am guessing that you really want to know how to properly access a specific point in the string and either use or modify it, and the constness was simply a mistake in your example.

Basya
  • 1,477
  • 1
  • 12
  • 22
0

I've just found the answer to my question by myself.

1. I can definitely extract symbols from char* by [] operator.

char* str = "abcde";
std::cout << str[0]; // a
std::cout << str[1]; // b
// and so on

2. I can change the first symbol of the string using char* pointer by following actions:

char* str = "abcde";

char* sym = "1";
str++;
char* rest = str;

str = (char*) malloc((strlen(sym) + strlen(rest)) * sizeof(char));

strcpy(str, sym);
strcat(str, rest);

In my real task I just needed to change the first symbol of a string, so mission completed.

vitdev
  • 11
  • 3
  • if you compiled with [-Wpedantic], your compiler would inform you that: "ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic] char* s = "abcde";" – 2785528 Aug 06 '17 at 16:20