## C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only

5 code snippets Posted by Panasonic99 on 8 Aug 2005 at 10:55 AM
Here and the questions and my answers:

1.) Print the elements of array values using pointer/offset notation.

ANS: for ( i = 0; i < SIZE; i++ )
printf("%.1f", values);

2.) Print the elements of array values by subscripting the pointer to the array.

ANS: for ( i = 0; i < SIZE; i++ )
printf("%.1f", values[ i ] );

3.) Refer to element 5 of array values using array subscript notation, pointer/offset notation with the array name as the pointer, pointer subscript notation and pointer/offset notation.

ANS: values[ 5 ]
*( values + 5 )
values[ 5 ]
*( values + 5 )

4.) What address is referenced by vPtr + 3? What value is stored at that location?

ANS: The address is 1002500 + 3 * 4 = 1002512. The value is 3.3

5.) Assuming vPtr points to values[ 4 ], what address is referenced by vPtr -= 4. What value is stored at that location?

ANS: The address of values[ 4 ] is 1002500 + 4 * 4 = 1002516
The address of vPtr -= 4 is 1002520 - 4 * 4 = 1002504
The value of that location is l.1

how'd I do?

thanks

Re: 5 code snippets Posted by stober on 8 Aug 2005 at 11:32 AM
This message was edited by stober at 2005-8-8 11:39:4

: 5.) Assuming vPtr points to values[ 4 ], what address is referenced by vPtr -= 4. What value is stored at that location?
:
the answer depends on how is vPtr defined? Pointers are incremented and decremeneted by the size of the object to which they point. On a 32-bit compiler where sizeof(int) = 4, then vPtr -= 4 will decrement the pointer by 16 bytes. So the answer to the queation is that vPtr will point to &values[0].
```vPtr - (4 * sizeof(int)) = vPtr - (4 * 4) = (address of vPtr) - 16
assume vPtr = 10000 then vPtr -= 4 will result in address 10000 - 16.
```

Re: 5 code snippets Posted by Donotalo on 8 Aug 2005 at 11:54 AM
: : 5.) Assuming vPtr points to values[ 4 ], what address is referenced by vPtr -= 4. What value is stored at that location?
: :
: the answer depends on how is vPtr defined? Pointers are incremented and decremeneted by the size of the object to which they point. On a 32-bit compiler where sizeof(int) = 4, then vPtr -= 4 will decrement the pointer by 16 bytes. So the answer to the queation is that vPtr will point to &values[0].
:
```: vPtr - (4 * sizeof(int)) = vPtr - (4 * 4) = (address of vPtr) - 16
: assume vPtr = 10000 then vPtr -= 4 will result in address 10000 - 16.
: ```

if the type of *vPtr and *values are same and vPtr points to values[4], then after executing vPtr -= 4, vPtr will always point to &values[0], isnt it?

Re: 5 code snippets Posted by stober on 8 Aug 2005 at 12:18 PM
:
: if the type of *vPtr and *values are same and vPtr points to values[4], then after executing vPtr -= 4, vPtr will always point to &values[0], isnt it?
:
:

Yes -- vPtr will point to values[0], the address stored in vPtr will be the same as &values[0].
Re: 5 code snippets Posted by Panasonic99 on 8 Aug 2005 at 12:47 PM
do the first 4 look good?
Re: 5 code snippets Posted by stober on 8 Aug 2005 at 1:08 PM
: do the first 4 look good?
:

#1 is incorrect -- it always prints the values[0].
Re: 5 code snippets Posted by Panasonic99 on 8 Aug 2005 at 1:10 PM
1.) Print the elements of array values using pointer/offset notation.

ANS: for ( i = 0; i < SIZE; i++ )
printf("%.1f", values[ i] );

is it right now? how about the other ones?
Re: 5 code snippets Posted by stober on 8 Aug 2005 at 1:23 PM
This message was edited by stober at 2005-8-8 13:25:36

: is it right now? how about the other ones?
:

I THINK #2 is wrong too. assuming values of an array of floats
```float *vPtr = values;
for ( i = 0; i < SIZE; i++ )
printf("%.1f", *vPtr++ );

```

Re: 5 code snippets Posted by BitByBit_Thor on 8 Aug 2005 at 1:44 PM
: 1.) Print the elements of array values using pointer/offset notation.
:
: ANS: for ( i = 0; i < SIZE; i++ )
: printf("%.1f", *(values + i) );
:
: is it right now? how about the other ones?
:

Since you need to use Pointer/Offset, you can't use Array notation.

Greets...
Richard

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic