2

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.

CrypticStorm
  • 560
  • 2
  • 11
  • http://stackoverflow.com/questions/5573310/difference-between-passing-array-and-array-pointer-into-function-in-c – tesseract Mar 03 '14 at 17:59
  • 1
    In your case, int readPoints(struct Points points[]); is the same thing as int readPoints(struct Points *points); – Antoine C. Mar 03 '14 at 17:59
  • So I can just call points[i] = whatever and then return the number of points read? Couldn't that run into problems with not enough space allocated? – CrypticStorm Mar 03 '14 at 18:03
  • Does `readPoints` read from the standard input? If so, can you prompt the user for the number of points to be entered? – wjmolina Mar 03 '14 at 18:13
  • Yes, but how does points know how long to be since the prompt is in this method? – CrypticStorm Mar 03 '14 at 18:14

1 Answers1

1

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);
}
Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
  • but how do I create the array without knowing its final size? Should I create it at size 0 and realloc whenever I add an item, or just create a huge buffer and trim it down after the call. – CrypticStorm Mar 03 '14 at 18:00
  • Being that the header can not be modified but I do have a MAX_POINTS variable defining the maximum number of points that will be read, should I make the passed array of size MAX_POINTS? Edit: The method is supposed to be all encompassing, prompting for the amount of points inside of it. – CrypticStorm Mar 03 '14 at 18:11
  • Thats totally depends on who is giving the function to you. If MAX_POINTS is provided, then you can expect atleast that much of memory. – Shashwat Kumar Mar 03 '14 at 18:39
  • MAX_POINTS is provided in the header file for this method. With that in mind, I would think that I should just malloc for MAX_POINTS and then realloc after the method to free up space. – CrypticStorm Mar 03 '14 at 18:41
  • malloc wont work. You just need to save data in given array only. Just try for Max_Points, if sufficient memory is available, its fine, else you will get segmentation fault. You can't do anything in that. – Shashwat Kumar Mar 03 '14 at 18:45
  • You don't do any memory allocation. In fact, you can't do any allocation as you have no way of telling the parent function where the memory is that you allocated. Your function's parameter is the pointer to the first element of an array that has already been allocated to have at least MAX_POINTS entries. Hopefully the assignment also says something about how the end of the list is marked, otherwise the calling function won't know how many points you read from the input. – M.M Mar 03 '14 at 23:20
  • Marking this as correct. I used MAX_POINTS for the parameter and it's working as intended with no memory problems. Thank you very much. – CrypticStorm Mar 04 '14 at 15:52