5 code snippets - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

5 code snippets

Panasonic99Panasonic99 Posts: 140Member
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

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-8-8 11:39:4[/red][/b][hr]
    : 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].
    [code]
    vPtr - (4 * sizeof(int)) = vPtr - (4 * 4) = (address of vPtr) - 16
    assume vPtr = 10000 then vPtr -= 4 will result in address 10000 - 16.
    [/code]












  • DonotaloDonotalo Posts: 715Member
    : : 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. [red]So the answer to the queation is that vPtr will point to &values[0].[/red]
    : [code]
    : vPtr - (4 * sizeof(int)) = vPtr - (4 * 4) = (address of vPtr) - 16
    : assume vPtr = 10000 then vPtr -= 4 will result in address 10000 - 16.
    : [/code]

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

  • stoberstober Posts: 9,765Member ✭✭✭
    :
    : [red]if the type of [blue]*vPtr[/blue] and [blue]*values[/blue] are same and [blue]vPtr[/blue] points to [blue]values[4][/blue], then after executing [blue]vPtr -= 4[/blue], [blue]vPtr[/blue] will always point to [blue]&values[0][/blue], isnt it?[/red]
    :
    :

    Yes -- vPtr will point to values[0], the address stored in vPtr will be the same as &values[0].
  • Panasonic99Panasonic99 Posts: 140Member
    do the first 4 look good?
  • stoberstober Posts: 9,765Member ✭✭✭
    : do the first 4 look good?
    :

    #1 is incorrect -- it always prints the values[0].
  • Panasonic99Panasonic99 Posts: 140Member
    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?
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-8-8 13:25:36[/red][/b][hr]
    : is it right now? how about the other ones?
    :

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

    [/code]




  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : 1.) Print the elements of array values using pointer/offset notation.
    :
    : ANS: for ( i = 0; i < SIZE; i++ )
    : printf("%.1f", [red]*(values + i)[/red] );
    :
    : is it right now? how about the other ones?
    :

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

    Greets...
    Richard

Sign In or Register to comment.