Trouble Using String Pointers

I'm working on a program that, among other things, simulates a command line interface. I've got my code set up such that the first word in the line that a user enters dictates which action will take place and the remaining words/text are simply parameters. My goal was to have each of these words/parameters read into a string pointer. The problem I've run into is that if I can't use string pointers to do standard string functions, i. e. normally you can do something like aString[1] will return the second character in a string. However, if aString is a pointer, the following won't work.

myString = *aString[1];

This apparent lack of functionality is causing me much frustration. I'm trying to get my program working elegantly and efficiently. I really like the idea of reading each parameter into its own (temporary) variable as opposed to one big long string, then pulling out what I need manually. Is there a way to return specific characters in a string if that string is only accessed through a string pointer or an array of string pointers? Or do I have to make a long list of string variables that get cleared after every action?


Here's a general idea of how I wanted this process to work.
1
2
3
4
5
6
7
8
9
10
11
string* param[10];

//while there is data, keep making new strings and assigning values to them...
int paramNum = 0;

while (cin.peek() != '\n')
{
    param[paramNum] = new string;
    cin >>*param[paramNum];
    paramNum++;
}



EDIT: To be more specific about what this portion of my program is trying to do, I want the user to be able to type in count epic and get a result that shows how many times the word "epic" occurs in the document. However, I also want them to be able to type in count "Stephen is awesome" and be able to search for the entire phrase "Stephen is awesome" (he really is). To do this, I need to be able to see whether or not there are quotation marks in the string the user entered. In order to do that using the method I want to (string pointers) I need to be able to check individual characters in a string.
Last edited on
You can use the dereferencing operator on a string pointer to use operators or functions of strings.

1
2
string* str = new string("Hello World!");
cout << (*str)[2]; // Prints 'l' 

However, I don't see why can't you use an array of string instead of an array of string pointers (The best for your case would actually be a vector of strings).
Ah! Parentheses! I didn't know you had to use those to make that work. It always gave me errors when I tried to do that without using the parentheses.

Yeah, I originally had planned on using an array of strings, but this is a matter of preference. For one thing, I rarely use pointers, so I want to take this opportunity to use them and get more comfortable with them (I've traditionally striven very hard to not use them). And second of all, it just seems cleaner and neater to just new and delete my strings instead of setting each one to an empty string or something. So it's really just a matter of preference for me. So far as I can tell, there really is no noticeable benefit in doing it the way I've decided to.

Also, thanks for the help. I've been anxious to get on with this program.
you can also do something like this which ive seen more commonly:
1
2
string* str = new string("Hello World!");
cout << *(str + 2) << endl;                          


also if you wanna take a line off of your example instead of incrementing paramNum on a seperate line just do:
 
param[paramNum++] = new string;


just an observation xD
Last edited on
^Actually that's *less* common and to most programmers, looks less correct.
really? they mean the same thing so it doesnt really make a difference i guess but i still didnt know that :)
really? they mean the same thing


Well what the OP put was broken. Your code is broken also, since it's doing the same thing. Zeillinger's solution is different (and works)

Zeillinger's:
cout << (*str)[2];

This dereferences the pointer first, then invokes the [] operator on the string object, giving you a single character.

ascii's:
cout << *(str + 2) << endl;

This takes the address of str, adds 2 strings to it (so we're on longer pointing to the "hello world" string, but instead of pointing to a nonexistant string elsewhere on the heap). It then attempts to dereference the pointer to this nonexistant string with the * operator, resulting in mangled output, possible an access violation, or worse.


As far as the foo[x] vs *(foo+x) is concerned... they're both equivilent, but I agree with firedraco that foo[x] is more common, more intuitive, and just overall easier on the eyes and brain.
bah i realize my example is screwed. i was going for something more like this
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using std::cout;
using std::endl;

int main(void)
{
	char str[] = "Hello World!";
	char* pstr = str;
	cout << *(str + 2) << endl;

	pstr = NULL;
	return 0;
}
Topic archived. No new replies allowed.