0

I am trying to extract a single element from a string literal included a list composed of string literals. I used VS Code.

First, I used pointer to make the array of string literals and succeeded in the extracting particular single character in C.

I ran following code to check this:

#include <stdio.h>

int main()
{ 
  char* list[]={"abc","def","ghi"};
  printf("%c\n",list[1][1]);
  return 0;
}

Then, I tried to use vector instead of array to make the list consists of string literals in C++.
I ran following code but failed:

#include <bits/stdc++.h>
using namespace std;

int main()
{
  vector<const char*> list;
  list = {"abc", "def", "ghi"};
  for (int i = 0; i < 3; i++){
    const char* temp = list[i];
    cout<<temp[3]<<endl;
  }
  return 0;
}

When I ran the code above, just blank was displayed:

I also tried changing const char* to char*, but it didn't worked well, too.

#include <bits/stdc++.h>
using namespace std;

int main()
{
  vector<char*> list;
  list = {"abc", "def", "ghi"};
  for (int i = 0; i < 3; i++){
    char* temp = list[i];
    cout<<temp[3]<<endl;
  }
  return 0;
}

I am not sure why my codes does not work well to extract the single character included the element of list. Could you please tell me the reason why my didn't work well?
I appreciate if you could help me.

Notations
Based on the comments, I revised my code. I changed char* to string as following:

#include <bits/stdc++.h>
using namespace std;

int main()
{
  vector<string> list;
  list = {"abc", "def", "ghi"};
  for (int i = 0; i < 3; i++){
    string temp = list[i];
    cout<<temp[3]<<endl;
  }
  return 0;
}

However, the output is also blank as well.

rturrado
  • 7,699
  • 6
  • 42
  • 62
XYJ
  • 11
  • 5
  • you cannot modify a string literal. A stirng literal is the thing you write in your code. Your vector contains pointers to c-strings it does not contain string literals. Use a `std::vector` and most of your problems will be gone – 463035818_is_not_an_ai Dec 30 '22 at 12:19
  • What do you expect that `cout< – mch Dec 30 '22 at 12:20
  • 3
    [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – 463035818_is_not_an_ai Dec 30 '22 at 12:20
  • You have different test cases. `list[1][1]` works, but `list[1][3]` does not. No difference for the vector. – BoP Dec 30 '22 at 12:21
  • 3
    `temp[3]` is the terminating null character. It is usually printed as nothing at all. Why did you change from `1` in the working version to `3` and expect the same behaviour? – molbdnilo Dec 30 '22 at 12:23
  • @463035818_is_not_a_number Thank you for your answer. I changed ```char*``` to ```string```, but it didn't work well. I attached my revised code in the question. Could you please tell me the reason why my revised code didn't work well? Thank you. – XYJ Dec 30 '22 at 14:03
  • please do not change the question after you received answer(s). If you want to ask a different question about different code you can open a new question – 463035818_is_not_an_ai Dec 30 '22 at 14:04
  • Why `temp[3]`? That's the real issue. In your previous code you had `list[...][1]`. Why change to `[3]` here? Try `temp[1]` instead. – john Dec 30 '22 at 14:10
  • @mch I wanted to print one of the single character in ```"abc"```. After one answer, I found iterator should not be 3, but should be either 0, 1, or 2. Thank you for your suggestion. – XYJ Dec 30 '22 at 14:11
  • @XYJ iterator -> index. I not trying to nit pick, but iterators are something else. – john Dec 30 '22 at 14:13
  • @463035818_is_not_a_number Thank you also for your suggestion to "using namespace std;". It was not until you told me that I know this was a bad practice. I'll try to be careful not to write so from now on. – XYJ Dec 30 '22 at 14:19
  • @BoP Thank you for your comment. I tested ```list[1][1]``` and successfully, the output is ```e```. – XYJ Dec 30 '22 at 14:25
  • @molbdnilo Thank you for your suggestion. I just put a random number there, but I didn't notice that ```3``` is out of range when I wrote the code. – XYJ Dec 30 '22 at 14:27
  • @463035818_is_not_a_number Thank you for your advice. I'm sorry that I changed my question after receiving commnets and answer. Actually, I thought my new problem is still related to this question so I added my new trial inspired by comments. In this case, where should I discuss this? I did also considering make a comment but I thought it is not use to attach codes in comment. – XYJ Dec 30 '22 at 14:31
  • @john Thank you for your suggestion. I haven't noticed that is the reason why I failed. I tried ```temp[1]``` or ```temp[2]``` and it worked well. I feel it is very difficult to find my easy mistakes. – XYJ Dec 30 '22 at 14:33
  • @XYJ Attention to detail is an important quality to be a programmer. It comes with experience. – john Dec 30 '22 at 14:53
  • @463035818_is_not_a_number I have realize that the reason why I should not use ```#Include ``` after reading the link you gave me. However, I'm not sure what should I use instead of ```using namespace std;``` in the private file (e.g. ```*.cpp```). I think in the [link you gave me](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), it mainly focus on using in header files. Actually, I have not completely understand the meaning of ```using namespace ***```, but I would be grateful if you could give me some advice. – XYJ Dec 30 '22 at 15:11

2 Answers2

3

temp[3] is null character. the literal "abc" means the array {'a', 'b', 'c', '\0'}.

Change

cout<<temp[3]<<endl;

To

cout<<temp[2]<<endl;
Smok1e
  • 51
  • 1
  • 3
  • Thank you for answer. I revised my code based on your answer and it worked well. – XYJ Dec 30 '22 at 14:35
0

you are using temp[3] but "abc" means an array of size 4 as follows:

{'a', 'b', 'c', '\0'}

Where '\0' represents the termination of the string. So temp[3] is same as '\0'.

Please use temp[2] instead to access the last character of the string.

  • Thank you for your answer. I had just put a random number ```3```, and I couldn't notice this mistake by myself. After revising, it worked well. – XYJ Dec 31 '22 at 01:29