Problem with pointers

Hello,

In the process of writing code, i am at a troubled situation.
The problem has to do with pointers.


Here is my code:: ( i know you might thing this is not necessary, since i am just trying to set a name and print it out, but i am using these exercises to learn how they work, rather than solving the problem quickly)

so i am reading in a name and printing it out, doing it through classes to see how it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
using namespace std;

class Time
{
        private:
                char name[50];

        public:
              void  setname(char *_settingname); // i am declaring a pointer
              void printo();


};

void Time::setname(char *_settingname)
{
    name = *_settingname; // i am giving the value pointed by the pointer to name
}
void Time::printo()
{
cout <<"your name was assigned to \n" <<name<<endl;
}


class Day
{
        private:
                Time        testing;

        public:
                void set_naming(char *_name);
                void printout();



};

void Day::set_naming(char *_name)
{

        testing.setname(&_name); // here i am giving the address of name to the setname function, which expects a pointer type



}
void Day::printout()
{

    testing.printo();


}

int main ()
{
    char h [50];

    cout<< "enter a name to be set:"<<endl;
    cin >>h;
    Day test;
    test.set_naming(&h); // giving the address of h to set_naming function
    test.printout();




    return 0;

}



i have a hard time diagnosing the error. If you can point me in the correct direction i would appreciate it, thank you very much in advance.
 
 name = *_settingname; // i am giving the value pointed by the pointer to name 

You can't assign strings like that, you have to use strcpy:
1
2
3
4
void Time::setname(char *_settingname)
{
    strcpy(name, _settingname); 
}


testing.setname(&_name); // here i am giving the address of name to the setname function, which expects a pointer type test.set_naming(&h); // giving the address of h to set_naming function
You don't need ampersand sign there because you must pass a pointer to these functions, not an address of the variables.

Thank you for answering.

the code works great now, but i am still confused about not assigning the pointer _settingname a pointee. you have indicated that i do not need to include the & sign during,

testing.setname(&_name) but is this not similar to stating *_settingname=&_name.
but not having it would come like this *_settingname=name;
wouldn't the program crash, because i am trying to assign a value to what the pointer is pointing to but the pointer is not pointing to anything.

i wrote this and tried it and the program does end up crashing.
(i understand that there is something i am misunderstanding here, since when i did what you said the code works great, i am a bit confused of why not including the &)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    int *p;
    int i;
    cin>>i;
    *p=i;
    cout<<*p<<endl;

    return 0;
}






Also, why do i need to use strcpy, can i not follow the pointer, and get whats in the box that the pointer is pointing to, and assign it to name.

Thank you
Last edited on
Your example crashes because p is not allocated == points to a random memory location.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
    int *p=new int; // allocate memory
    int i;
    cin>>i;
    *p=i;
    cout<<*p<<endl;
    
    delete p; // free allocated memory

    return 0;
}
}


Also, why do i need to use strcpy, can i not follow the pointer, and get whats in the box that the pointer is pointing to, and assign it to name.


There are many reasons for that.

Example 1: This works
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

void printout(char *str)
{
    char *p_str=str;
    cout <<p_str<<endl;
}


int main()
{

  printout("lol");
  return 0;
}


Example 2: This will crash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

void printout(char *str)
{
    char *p_str=str;
    p_str[0]='a'; // trying to modify a constant string-- program will crash here
    cout <<p_str<<endl;
}


int main()
{
  printout("lol");
  return 0;
}


Example 3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char *p_str; // pointer to string

int main()
{
  char *str=new char[25]; // allocate memory for string
  p_str=str; // assign pointer
  strcpy(str,"abccdefgh");

  cout <<p_str<<endl; //  OK -- will print the string

  delete[] str; // deallocate memory

  cout <<p_str<<endl; // p_str still points to deallocated memory. Program will not priont the string or crash

  return 0;
}


There are even more (and better) explanations why you can't simply assign pointers like that

EDIT: Read
http://www.cplusplus.com/doc/tutorial/pointers/
and
http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
Thank you so much, i understand now, i been misinterpreting a lot of things in C++.

Thank you so much again.
Topic archived. No new replies allowed.