Computing.Net > Forums > Programming > c++ pointer problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

c++ pointer problem

Reply to Message Icon

Name: haotj
Date: January 24, 2005 at 18:09:35 Pacific
OS: Win32
CPU/Ram: athlon2000/512mb
Comment:

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.




Sponsored Link
Ads by Google

Response Number 1
Name: BlueRaja
Date: January 25, 2005 at 14:45:47 Pacific
Reply:

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


0

Response Number 2
Name: sen (by santanusen_82)
Date: January 26, 2005 at 22:18:24 Pacific
Reply:

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


0

Response Number 3
Name: guantao
Date: January 27, 2005 at 20:49:51 Pacific
Reply:

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;


0

Response Number 4
Name: BlueRaja
Date: January 28, 2005 at 18:55:10 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: c++ pointer problem

C++ Pointer problem www.computing.net/answers/programming/c-pointer-problem/11700.html

C++ pointer www.computing.net/answers/programming/c-pointer/11648.html

pointer problem with structures www.computing.net/answers/programming/pointer-problem-with-structures/12572.html