Write a program that will display the value and logical address of an uninitialized character array with size ten (10) and a pointer pointing to the array. (Hint: you may need to perform some casting.)
I know exactly what to do if the array is an integer. How do I do casting?
1 2 3 4 5 6 7 8 9
int main(){
char yeh[10] = "Hello wor";
int *pointer = (int)yeh[10];
_pause();
return 0;
}
and btw how is it considered "Hello wor" as size 10? It causes me error whenever I add or subtract a letter
Not really sure what you are asking. How do you display the value of something that is uninitialised?
To store a null-terminated C-string you need to leave space for the terminating null character ('\0'). yeh[10] doesn't exist anyway: indices go from 0 to 9 if the size is 10.
0x7962591cac60 H
0x7962591cac61 e
0x7962591cac62 l
0x7962591cac63 l
0x7962591cac64 o
0x7962591cac65
0x7962591cac66 w
0x7962591cac67 o
0x7962591cac68 r
You could also use a "pure" char array - NOT a null-terminated C-string - to get one extra letter:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
int main()
{
char yeh[10] = { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l' }; // Character array, but not null-terminated C-string
char *pointer = yeh;
for ( int i = 0; i < 10; i++ )
{
std::cout << (int*)pointer << '\t' << *pointer << '\n';
pointer++;
}
}
0x7dc221e402f0 H
0x7dc221e402f1 e
0x7dc221e402f2 l
0x7dc221e402f3 l
0x7dc221e402f4 o
0x7dc221e402f5
0x7dc221e402f6 w
0x7dc221e402f7 o
0x7dc221e402f8 r
0x7dc221e402f9 l
hmm. I guess theres no need to initialize a value for it. I think it just requires the yeh[10] value and address. but yeah it will leave the value blank
The (void*)yeh is a C-style cast. C++ has more specific static_cast, dynamic_cast, reinterpret_cast, and const_cast. For example: static_cast<void*>(yeh)
The C++ cast syntax are more specific; they do less and that is Good.
They are easier to spot too. Your editor probably has Search function (aka Find). It is easy to search for "_cast" from the code. (You might get some false positives, but no false negatives.)
Consider the alternative. Your code might have some C-style casts. You cannot remember what types you cast to, what values/variables/expressions you cast. We do want to find those (typename)something. That is hard.
One more thing: float(42) is a cast too. We create a temporary float object that we initialize with value 42. typename(something)
For the cout form, please go back to my previous codes, which I have edited to correct. std::cout << (int*)pointer << '\t' << *pointer << '\n';
I have used (int *) rather than (void *). Output both in the same line and you will find that they give the same results. I (personally) find (int *) more natural than (void *) for a number.
I note @keskiverto's comments about static_cast<>; thus: std::cout << static_cast<void *>(pointer) << '\t' << *pointer << '\n';
However, my (personal) preference is for c-style casts, as I prefer shorter code, and I (personally) find it clearer. Each to his own. On the whole, I like casting as little as possible.
you missed the index for the variable 'yeh' in line 5
You are incorrect. I did not miss the index - it will automatically point to the first element of the array. This is the same as if you pass arrays as function arguments. This, too, is C-type behaviour: I quite like it.
If I wanted to point to a particular element then I would have to use & as an "address of" operator; e.g char *pointer = &yeh[0];
To me that seems unnecessary. The only times that I do it are for multidimensional arrays: not in cases like the present one.
I don't follow your question - please clarify. An asterisk, *, is used in the declaration of a pointer, and to dereference it (i.e. give the value of what it points to) later. An ampersand, &, means "address of" in this context (though, like many symbols, it can be used for other things).