Help with pointers

I got stuck in this assignment. I can't get the for loop to work. The loop needs to step through each character in My_Name and display: the index, the character, its address, and its decimal value. Any help will be appreciated thank you

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
#include <iostream>
#include <string.h>

using namespace std;

int main(){

	int x=25;		//int variable
	int *ptr=nullptr;	//Pointer variable, can point to an int

	ptr=&x;			//Store the address of x in ptr
	cout<< "The value in x is" << x << endl;
	cout<< "The pointer of x is :" << ptr << endl;
	cout<< "The hex address of x is: "<< ptr << endl;
	cout<< "The dec address of x is: "<<(long long) ptr << endl;
	cout<< "The size of ptr is " << sizeof(ptr) << " bytes\n";
	cout<< "The size of x is " << sizeof(x) << " bytes\n";
	cout<< endl;
	
	char My_Name[]= "Omar Martinez";
	cout <<"Size of My_Name: " << sizeof(My_Name) << endl;
	cout <<"Size of string length My_Name: "<< strlen(My_Name) << endl;
	
	//char *p=nullptr;
	//p=My_Name;
	
	
	for(int i=0;i<strlen(My_Name);i++){
		char *p=nullptr;
		p =My_Name;
		cout << i << " is " << My_Name[i]  << " at address: " << &p[i] << " with a values of:" << sizeof(p) << endl;
	}
	return 0;
}
Last edited on
sizeof(p) is a constant. it is the size in bytes of the pointer, useless info for you right now.
try (int)(p[i]) instead (the integer value of the character).


you had it right, 24 and 25 is a good place for p. Inside the loop is inefficient, even if it works. you don't really need p anyway, my_name is good enough for these tasks.

C-string library in c++ is <cstring>. Its better to use this than string.h
Last edited on
Nice the (int)(p[i]) help it gave me the decimal value but I still can't get the address of each character to work. when I run the program it prints out the character on My_Name and not the address
To display an address of type char* using cout, you need to cast the pointer to an unsigned int (unsigned long int for 32bit, unsigned long long int for 64bit). By definition, cout will display the null-terminated string starting at the specified address rather than the address itself for a char * type. Also, you're using c-style cast rather than the C++ casts. Note also that strlen() is a potentially 'expensive' operation, so it's best to only evaluate once if possible.

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

using namespace std;

int main() {

	const int x = 25;	//int variable
	const int* ptr = &x;	//Store the address of x in ptr

	cout << "The value in x is: " << x << endl;
	cout << "The pointer of x is: " << ptr << endl;
	cout << "The hex address of x is: " << ptr << endl;
	cout << "The dec address of x is: " << reinterpret_cast<unsigned long long>(ptr) << endl;
	cout << "The size of ptr is " << sizeof(ptr) << " bytes\n";
	cout << "The size of x is " << sizeof(x) << " bytes\n";
	cout << endl;

	char My_Name[] = "Omar Martinez";
	cout << "Size of My_Name: " << sizeof(My_Name) << endl;
	cout << "Size of string length My_Name: " << strlen(My_Name) << endl;

	for (size_t i = 0, e = strlen(My_Name); i < e; ++i)
		cout << i << " is " << My_Name[i] << " at address: " << reinterpret_cast<unsigned long long>(&My_Name[i]) << " with a values of: " << static_cast<int>(My_Name[i]) << endl;

	return 0;
}



The value in x is: 25
The pointer of x is: 0018FF24
The hex address of x is: 0018FF24
The dec address of x is: 1638180
The size of ptr is 4 bytes
The size of x is 4 bytes

Size of My_Name: 14
Size of string length My_Name: 13
0 is O at address: 1638188 with a values of: 79
1 is m at address: 1638189 with a values of: 109
2 is a at address: 1638190 with a values of: 97
3 is r at address: 1638191 with a values of: 114
4 is   at address: 1638192 with a values of: 32
5 is M at address: 1638193 with a values of: 77
6 is a at address: 1638194 with a values of: 97
7 is r at address: 1638195 with a values of: 114
8 is t at address: 1638196 with a values of: 116
9 is i at address: 1638197 with a values of: 105
10 is n at address: 1638198 with a values of: 110
11 is e at address: 1638199 with a values of: 101
12 is z at address: 1638200 with a values of: 122


[32 bit compile]
Last edited on
Topic archived. No new replies allowed.