Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I was able to do number 3, but im stuck on 4, plz help, thx alot. ps im programing in VC++ 6
3. (5 Marks) Define a dynamic array of longs with 5 elements. Set all elements to 3. Define a long * pointer to the first element and use the pointer to add 5 to the first element of the array. Now use pointer arithmetic to point to the last element of the array and set the value of that element to 0. Finally deallocate the array.
4. (5 Marks) Repeat the previous problem, but this time use a char * pointer to change the array of longs. Assume a big-endian platform.

What don't you get about it, specifically?
The problem is exactly the same as the one prior, except now you're editing the array byte-by-byte (rather than DWORD-by-DWORD)BlueRaja.admin@gmail.com

Hello,
I think the problem is to declare an array of long elements (8 bytes each) and use a char (1 byte) pointer to point to it and set values to its elements. The platform is big-endian. This means LSB is stored first and MSB at the end. So, for setting an element to 3 you need to set the first byte to 3 and the remaining 7 to 0. Here's the code.....
void main ()
{
//delare variables
long int array[5];
char *p;//point to the first element
p = (char*) array;//set the all elements to 3
for(int i=0; i<5; i++)
{
//set the LSB to 3
*p = 3;
//point to the next byte
p++;//set the other (second and onwards) bytes to 0
for(int j=2; j<=sizeof(long int); j++)
{
*p = 0;
p++;
}
}
//now point to the first byte of last element
p = (char*) array + 4 * sizeof(long int);//set it to 0
*p = 0;//write some code to display the array
///elements}
Best of luck.....Santanu Sen
National Institute of Technology
Durgapur
India

The 4th problem exams difference between big-endian and little-endian. The code is as fllow:
long [] laNum = new long[5];
for(int i=0; i<5; i++)
laNum[i] = 3;
char * pc = laNum;
char * pcTemp = pc;
pcTemp += 3;
(*pcTemp) += 5;
pcTemp = pc+19;
(*pcTemp) = 0;
delete[] laNum;

The difference between big-endian and little-endian is irrelevant in this problem, since all four bytes are set to 00000000b.
BlueRaja.admin@gmail.com

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |