help easy problem

closed account (9G3v5Di1)
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.

EDITED: Codes below are updated. Apologies

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
int main()
{
   char yeh[10] = "Hello wor";
   char *pointer = yeh;

   while( *pointer )
   {
      std::cout << (int*)pointer << '\t' << *pointer << '\n';
      pointer++;
   }
}

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
Last edited on
closed account (9G3v5Di1)
were those numbers printed before the letters were the logical addresses?
Hmm, thought they were, but C++ shell doesn't like them.

So I was wrong. I'll need to think some more...

EDIT: OK, gone back and fixed.
Last edited on
closed account (9G3v5Di1)
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
There isn't a yeh[10] value. Your array has size 10 and counts from 0. The indices will be 0 to 9.
closed account (9G3v5Di1)
okay but how do we actually print the logical address
Yuk. Try these instead.

EDIT: I've just gone back and amended the previous codes for streamed (<<) output. More about the issue here: http://www.cplusplus.com/forum/beginner/181574/#msg890588 (@moshops and @cire's contributions).


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

int main()
{
   char yeh[10] = "Hello wor";
   char *pointer = yeh;

   while( *pointer )
   {
      printf( "%p   %c\n", pointer, *pointer );
      pointer++;
   }
}


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

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++ )
   {
      printf( "%p   %c\n", pointer, *pointer );
      pointer++;
   }
}
Last edited on
How do I do casting?

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)


Prefer the C++ casts.
Last edited on
closed account (9G3v5Di1)
wow so (void*)yeh came from C language while my prof is using it in C++ how come?

@lastchance, how do you write line 11 in cout form?
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.
Last edited on
closed account (9G3v5Di1)
@lastchance you missed the index for the variable 'yeh' in line 5 and the rest are okay. Thank you so much!!
my prof is using it in C++ how come?

Because C++ is almost a superset of C. Almost all C is legal in C++.

Almost.
goldwinbryanr wrote:
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.
Last edited on
closed account (9G3v5Di1)
what do you call that asterisk sign without (&)? as I know a pointer has an asterisk and equal to the address of the variable
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).
Topic archived. No new replies allowed.