Hey! I'm learning C++ and pointers are pretty new to me, I can work with them a little but still don't see the point to them.. ok, well, I do.. For example, passing variables byReference! But that's the only good I see comming from pointers (yup, I'm a n00b)..

Ok, my question...

First of all, you can explain to me why pointers are so significant (if you want, otherwise I'll find out in time anyway I'm sure)? And secondly, I can't get something to work and I've been trying for hours.. Please make the following work!! I want to modify the value of a string using pointers, and a void method. I don't want to use any additional headers either.. Thanks! (this knowledge will benefit me forever)

#include <iostream>
using namespace std;

void ModStr(char * theStr);

int main(){
char strMyString[40];
cin >> strMyString;
ModStr(&strMyString);
cout << " value: " << strMyString << "\n";
return 0;
}

void ModStr(char * theStr){
*theStr = "This is the new text";
}

Why won't this work? Do I not have a clue what I'm doing? :rolleyes:

No compiler at moment to confirm, but I'd try this:

ModStr(strMyString);

instead of this:

ModStr(&strMyString);

as the name of the string is the address of the first char of the char array making up the string, unlike the names of other types of variables.

This won't work because you can't assign arrays

*theStr = "This is the new text";

You would need to use strcpy(), or a variant thereof, from cstring header file. You need to be sure the string you are copying into has enough memory for the string you are passing to it. In this case, it wouldn't be a problem.

strcpy(theStr, "This is the new text");

ITTT WOOOORRRKKSS !! :p

Aww, thankie.. Stupid me, I thought that strcpy required an additional header... And yea ModStr(strMyString); makes sense, actually I knew that and I don't know why I was using the amperstance thingie. :-|

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.