Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,220 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,166 people online right now. Registration is fast and FREE... Join Now!




Need Some Help with pointers

 
Reply to this topicStart new topic

Need Some Help with pointers

James Bond C++ Spy
20 Nov, 2007 - 01:15 PM
Post #1

D.I.C Head
**

Joined: 3 Oct, 2007
Posts: 81


My Contributions
When I first run this code it displays the the cout first and it is displaying the pointer instead of name="" before asking to enter the data. I need it to be blank. The age is displayed properly with a 0. This is a snippet of the initialize function I have:

CODE

void initialize(char [], int *);
        int age1;
    int age2;

    initialize(name1, &age1);
    initialize(name2, &age2);
    
    // function initialize definition
void initialize(char name[], int *age) {
    name = " ";
    *age = 0;
    } // end function initialize

// function print definition
void print(const char name[], const int age) {
    cout << "The value stored in variable name is: " << name << endl
            << "The value stored in variable age is: " << age << endl << endl;
    
} // end function print


Thanks for the help

This post has been edited by James Bond C++ Spy: 20 Nov, 2007 - 01:31 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Need Some Help With Pointers
20 Nov, 2007 - 08:11 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
lets look at a simple little example function:
CODE

void setVal(int *ptr) {
int value = 100;
ptr = &value;
}

int main() {
int val = 0;
setVal(&val);
cout << "Value: " << val;
return 0;
}


this would print out "Value: 0" rather than 100, why?

The reason is that the argument (in this case &val) is stored on the stack, and the variable ptr is assigned to that location on the stack, when I change that value I am in no way affecting the value stored in the address &val. This is exactly what is happening with your string. You pass a pointer to some string, you then change that pointer, but NOT what the pointer points to. What is funny to me is that you did things right with the int in the same function!

So how to get your function to work?

Well first off you need to create a string to pass a reference to (or pass a pointer to a pointer). Then you need to change what the pointer points to, not the pointer:
CODE
#include <iostream>
#include <cstring>
using namespace std;

void initialize(char [], int *);

int main() {
    char name[100]; //setup a string that will need to be intialized
    int age;
    initialize(name, &age);
    cout << "Name: " << name << "\tAge: " << age << endl;
    return 0;
}

void initialize(char name[], int *age) {
    strcpy(name, "Hello"); //this will copy the string into the location
    *age = 100;
}

The other way to do this would be to use a pointer to a pointer:
CODE
#include <iostream>
#include <cstring>
using namespace std;

void initialize(char **name, int *age);

int main() {
    char *name;
    int age;
    initialize(&name, &age);
    cout << "Name: " << name << "\tAge: " << age << endl;
    delete[] name;
    return 0;
}



void initialize(char **name, int *age) {
    *name = new char[100]; //create the buffer
    strcpy(*name, "Hello");    //copy in the new value
    *age = 100;
}

User is offlineProfile CardPM
+Quote Post

Bench
RE: Need Some Help With Pointers
21 Nov, 2007 - 02:52 AM
Post #3

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Since you're using C++, the best way to solve your problem is the C++ library type called string which allows you to treat a string as a single entity, rather than worrying about arrays of characters.

Do a little reading up on the <string> library, and make your life easier.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 05:38AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month