Question

An assignment that I'm working on requires us to write a read function that reads in an array of Points from stdin. The problem I'm having is the syntax of the method. (The header is provided and can not change.)

int readPoints(struct Points points[]);

This method is supposed to be all encompassing, returning the number of points read and populating the parameter array with their values. The only problem is that the array is not a pointer to the array, so wouldn't populating that array just do so in the local scope? I also don't know how much space to allocate for the function since the number of points is determined in the method.

Was it helpful?

Solution

points in not the copy of original array passed as argument. It's the pointer to the array passed. Just populate it and everything will do fine.
The problem in this type is that you don't know the actual size of array passed as argument. The array size should have been passed as arguments. There is no way to know the size allocated to the array.

The following code verifies it:

void func(int a[])
{
    printf("%d",sizeof(a));    //prints 4, so a is a type of pointer
}

int main()
{
    int a[5];
    printf("%d",sizeof(a));   //prints 20
    func(a);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top