What's wrong with this..... Pointer Variables//Addresses

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




#include <iostream>

using namespace std;

void dnl();

int digit = 25;           // Variables
float number = 12.5;
char letter = 'A';
int set[4] = {2,4,6,8};

int *Iptr;       // Pointers
float *Fptr;
char *Cptr;
int *Aptr;

Iptr = &digit;     //I get errors here
Fptr = &number;
Cptr = &letter;
Aptr = set;

void dnl()
{
     cout << "The address of digit is " << *Iptr << endl;
     cout << "The size of digit is " << sizeof(digit) << " bytes\n";
     cout << "The value in digit is " << digit << endl;
     system ("pause");
     

     
}

int main()
{
    dnl();
    system ("pause");

}
You can't put code such as those assignments outside of a function. Move them
to main().
Ok thanks a lot, can you tell me why this happens?

When I tell it to show the address of letter it gives me "A" instead of an address.

void dnl()
{
int *Iptr; // Pointers
float *Fptr;
char *Cptr;
int *Aptr;

Iptr = &digit;
Fptr = &number;
Cptr = &letter;
Aptr = set;

cout << "The address of digit is " << Iptr << endl;
cout << "The size of digit is " << sizeof(digit) << " bytes\n";
cout << "The value in digit is " << digit << endl << endl;

cout << "The address of number is " << Fptr << endl;
cout << "The size of number is " << sizeof(number) << " bytes\n";
cout << "The value in number is " << number << endl << endl;

cout << "The address of letter is " << Cptr << endl;
cout << "The size of letter is " << sizeof(letter) << " bytes\n";
cout << "The value in letter is " << letter << endl << endl;

system ("pause");

}


Type char* is a special case. char pointers are treated as C-style strings... so when you cout a char pointer, it will print the string that the pointer points to, rather than the address.

To get around this, you can cast the pointer to a different pointer type:

1
2
cout << Cptr;  // prints it as a string
cout << static_cast<void*>(Cptr); // prints it as a pointer 
Ok thanks a lot!
Darn I am having more trouble now, I need to find information on "set" which is an array, how can I do that? I tried what I did earlier but that did not work.

Also if somebody could explain or refer me to learn how to find the address of specific value IN an array that would be very helpful! These pointers have got me very confused.

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
#include <iostream>

using namespace std;

void dnl();
void setinfo();

int digit = 25;           // Variables
float number = 12.5;
char letter = 'A';
int set[] = {2,4,6,8};



void dnl()
{
     int *Iptr;       // Pointers
     float *Fptr;
     char *Cptr;
     int *Aptr;

     Iptr = &digit;
     Fptr = &number;
     Cptr = &letter;
     Aptr = set;     
     
     cout << "The address of digit is " << Iptr << endl;
     cout << "The size of digit is " << sizeof(digit) << " bytes\n";
     cout << "The value in digit is " << digit << endl << endl;
     
     cout << "The address of number is " << Fptr << endl;
     cout << "The size of number is " << sizeof(number) << " bytes\n";
     cout << "The value in number is " << number << endl << endl;
     
     cout << "The address of letter is " << static_cast<void*>(Cptr) << endl;
     cout << "The size of letter is " << sizeof(letter) << " bytes\n";
     cout << "The value in letter is " << letter << endl << endl;
     
     system ("pause");
     
}

void setinfo()
{
     cout << "The address of set is " << Aptr << endl;
     
     system ("pause");
}

int main()
{

    dnl();
    setinfo();
    system ("pause");

}
Last edited on
Darn I am having more trouble now, I need to find information on "set" which is an array, how can I do that? I tried what I did earlier but that did not work.

The way you did it is right. It won't work because Aptr can't be "seen" from setinfo(). Aptr is a local variable declared inside main dnl function. Just ditch setinfo() and put that code inside main. Try declaring Aptr inside setinfo(). It should work.

Also if somebody could explain or refer me to learn how to find the address of specific value IN an array that would be very helpful! These pointers have got me very confused.

It's set==&set[0] i.e. the address of the array itself is the address of the first element of the array. To get the others you can simply write set+1 or &set[1] for the second element, set+2 or &set[2] for the third and so on...
Last edited on
Topic archived. No new replies allowed.