I
Hallo,
ich habe eine Frage zu einer Funktion die ich schreiben moechte.
Ich habe ja Speicher mit
ptr = malloc ( n * sizeof(location) );
reserviert die Groesse habe ich ja mit sizeof location angegeben
typedef struct {
int x;
int y;
}location;
wie schreibe ich nun die Funktion, damit ich die Werte auch uebergeben kann, denn der Compiler meckert immer rum das ptr[].x oder ptr[].y keine gueltigen Werte sind ?
Meine Idee war :
int vector ( int * ptr[], int n )
int vector ( int ptr[].x, int ptr[].y , int n )
Aber beides funktioniert nicht... Muss ich die struct definition vielleicht nochmal in die Funtion einfuegen ???
EDIT :
Nach 1 Stunde rumprobieren und kapieren das location jetzt ein Type ist , hier das fertige Programm:
/***************************************************************/
/* Programmer: Micha Hobert */
/* */
/* Program 14: Lowermost and Leftmost */
/* */
/* Approximate completion time: 5 hours */
/***************************************************************/
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x;
int y;
}location;
void vector ( location ptr[] , int n ) {
int j;
int miny;
int minx;
minx = ptr[0].x;
miny = ptr[0].y;
for (j=0; j<n-1; j++){
if (ptr[j].x<minx){
minx=ptr[j].x;
if (ptr[j].y<miny)
miny=ptr[j].y;
}
}
printf( "The Lower Most, Left Most Point is: x = %d and y = %d\n\n", minx,miny );
}
/* I tried to create a function for Leftmost & Lowermost, but it wouldn't
accept my array in combination with my struct */
int main ( ) {
int n;
location *ptr;
int i=0;
FILE *fin;
fin = fopen ( "points.txt", "r");
fscanf ( fin, "%d", &n );
ptr = malloc ( n * sizeof(location) );
while(i <= n-1 ){
/* fgetc(fin);*/
fscanf(fin, "%d%d", &ptr[i].x,&ptr[i].y);
i++;
}
vector ( ptr , n );
return 0;
}