Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ string class

Name: Anonymous 2012-09-03 13:53

If you have a C++ string and a char pointer

string str = "mudkipz";
char * ptr;


how would you iterate through each char in the string using only the pointer and only pointer notation? In other words, you can't assign the address with ptr = &str[0];

I don't see how it's possible, since a string isn't an array. If it were, I'd just do this

string str = "mudkipz";
    char * ptr = str;

    for(int i = 0; i < str.length(); ++i)
    {
        cout << *(ptr + i);
    }


Any ideas?

Name: Anonymous 2012-09-03 13:55

Also, I can't use C-strings or any function that returns a C-string/char array like string.c_str()

Name: Anonymous 2012-09-03 14:34

This post is everything what's wrong with c++. It gives ``ideas'' like that to idiots like OP.

Name: Anonymous 2012-09-03 15:57

>>3
Ideas? It's part of a project for my course. And my professor doesn't seem like an idiot. Instead of contributing absolutely nothing, why don't you mention exactly what it is I'm doing that's so wrong? Stepping through a string with a char pointer? Please. Unless, of course, you don't really have any programming knowledge and just like to troll.

Name: Anonymous 2012-09-03 16:14

>>4
One of the many things that we tend to talk about here are the design of programming languages and their consequences. If you are not interested in this, and only want help with your homework, I suggest that you kindly take your questions to your professor or teaching assistant. Thank You

Name: Anonymous 2012-09-03 16:15

If your prof is forcing you to use unnecessary pointers to iterate a string, he's in fact an idiot.

Name: Anonymous 2012-09-03 16:20

>>5
"One of many" things. Okay. I get that. But the topic of strings and char pointers is surely another one of those many things. One thing for certain is personal attacks accomplish and contribute nothing. Actually, they detract. It'd be better not responding at all.

>>6
Maybe. But it's just to illustrate the relationship between the string class and pointers, I think. I just don't see how it's possible to initially point the char pointer to the beginning of the string without using the address operator. I.E. &str[0]

Name: Anonymous 2012-09-03 16:26

>>4
You shouldn't mix oop and pointer magic. One of the main principles of oop is encapsulation which, in terms of your example, means that you should manipulate objects of the stirng class only though their public methods, the inner representation of the string should never leak outside. Trying to "tinker" the state of objects through pointers completely violates this principle.

You can obtain a pointer to an inner array of chars using the [code]data()[/spoiler] method. c++ is shit

Name: Anonymous 2012-09-03 16:27

>>8
Okay, nevermind. I guess my professor is an idiot.

Name: Anonymous 2012-09-03 16:31

cout << *(ptr + i)
Why not str[i]? Getting a pointer and using *(ptr + i) isn't helpful anyway. It's just a more complicated way to write ptr[i]. Unless your professor wants you to iterate through the string with ptr++.

Name: Anonymous 2012-09-03 16:33

>>10
Yeah. He wants us to only use the pointer.

Name: Anonymous 2012-09-03 16:38

>>9
Maybe he's not as big of an idiot as everyone here thinks.  Look at this:
http://www.cplusplus.com/reference/string/string/operator[]/

When you use the square bracket operator, you get get a reference of a char at whatever location you point it at.  How does it do that?  Using data(). Coincidence?  Probably not.

Name: Anonymous 2012-09-03 17:53

>>12
C++: It even breaks URLs.

Name: Anonymous 2012-09-03 19:40

>>13
shitty board implementation...

Name: Anonymous 2012-09-03 20:43

>>8
Fuck "OOP principles", pragmatism is where it's at.

Name: Anonymous 2012-09-03 22:16

>>15
Fixing the mess you suggest isn't pragmatic at all.

Name: Anonymous 2012-09-04 5:41

>>16
it's pragmaticism anyway

Don't change these.
Name: Email:
Entire Thread Thread List