Invalid conversion from 'int*' to 'int' question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nozdormu
    New Member
    • Oct 2007
    • 1

    Invalid conversion from 'int*' to 'int' question

    Hi guys, I have a quick question. The language I'm using is C.

    The question I'm tackling:
    Write a program that
    (1) defines a 1D array with ten int elements, and
    (2) sets the values of the elements such that they are equal to the addresses of the elements, and
    (3) prints out the content of the elements.
    The program should call a user-defined function value_address with the following function prototype: void value_address( int[] )

    My answer:

    [CODE=c]#include <stdio.h>
    int main(void)
    {
    int array[10], i;
    void value_address(i nt[]);

    value_address(a rray);

    for (i=0;i<10;i++)
    printf("Value of array[%1d] is %d\n", i, array[i]);

    return 0;
    }

    /* function definition for value_address */
    void value_address(i nt inarray[])
    {
    int i;
    for (i=0;i<10;i++)
    inarray[i] = &inarray[i];
    }[/code]


    I'm getting an error message Invalid conversion from 'int*' to 'int' on the line below
    inarray[i] = &inarray[i];
    What am I doing wrong?

    Thanks :)
    Last edited by Ganon11; Oct 28 '07, 02:13 PM. Reason: Please use the [CODE] tags provided.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Nozdormu
    Write a program that
    (1) defines a 1D array with ten int elements, and
    (2) sets the values of the elements such that they are equal to the addresses of the elements, and
    (3) prints out the content of the elements.
    The program should call a user-defined function value_address with the following function prototype: void value_address( int[] )

    [ ... ]

    I'm getting an error message Invalid conversion from 'int*' to 'int' on the line below
    inarray[i] = &inarray[i];
    What am I doing wrong?

    Thanks :)
    You're not doing anything wrong: question 2) sucks, i.e. an int can't contain an
    address because ints are not guaranteed to be able to contain the value of an
    int* (a pointer to an int).

    kind regards,

    Jos

    Comment

    Working...