Hi all, i need to understand how to solve the following exercise, I have a structure:
Code:
#define FRAMESIZE 17		// carframe size

typedef struct {
	char *name;         // owner name
	char *surname;   // owner surname
	char carframe[FRAMESIZE];     // carframe number(alphanumeric)
	struct {
		int d;		// day
		int m;		// month
		int y;		// year
	} matr;						// matriculation date
} Vehicle;
I have to implement a function Vehicle *buildarchive(char *text, int *n) that creates a dynamically allocated array of struct Vehicle whose values are contained into the string text and that returns in *p the number of struct, each string is divided by rows in order to contain datas of each vehicle, every row has the following format: N;S;F;D;M;Y where N is the owner's name, S the surname etc.. and terminates with the character '\n', so if the text is:
Code:
Mario;Dell'Olio;ABCD1234567899GHT;12;1;2004
Laura;Di Carlo;ABBB0987654321HGT;3;10;1999
Maria Luisa;Crociani Sforza Pallavicini;NNNN1234567890BFB;25;11;2006	
Giorgio;Verdoni;AAAA09876FFT54321;10;10;2009
the function creates the following array, and return in *p the value 4:
Code:
[0] = {"Mario", "Dell'Olio", ABCD1234567890GHT, {12, 1, 2004}}
[1] = {"Laura", "Di Carlo", ABBB0987654321HGT, {3,10,1999}}
etc till [3] = ...
now what i did till now is basically just printing on screen each row:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FRAMESIZE 17		// carframe size

typedef struct {
	char *name;
	char *surname;
	char carframe[FRAMESIZE];
	struct {
		int d;		// day
		int m;		// month
		int y;		// year
	} matr;						// matriculation date
} Vehicle;

Vehicle *buildarchive(char *text, int *n);
int main()
{
	char string[] = {"Mario;Dell'Olio;ABCD1234567899GHT;12;1;2004\n"
						 "Laura;Di Carlo;ABBB0987654321HGT;3;10;1999\n"
						 "Maria Luisa;Crociani Sforza Pallavicini;NNNN1234567890BFB;25;11;2006\n"			
						 "Giorgio;Verdoni;AAAA09876FFT54321;10;10;2009\n"
						 };

	int *p;
	
	buildarchive(string, p);

	return 0;	
}
Vehicle *buildarchive(char *text, int *n)
{
	int count = 0, i;
	char *text_temp;
	char *text_tmp;
	text_temp = text;
	text_tmp = text;
	
	/* counts rows */
	while(*text_tmp != '\0') {
		if(*text_tmp == '\n') {
			count++;
		}
		*text_tmp++;
	}
	
	Vehicle *array = (Vehicle*)malloc(count * sizeof(Vehicle));
	
	while(*text != '\0') {	  		// iterates till the string reaches the '\0' character
		while((*text_temp != ';')&&(*text_temp != '\n')) {		
			printf("%c", *text_temp);
			*text_temp++;			
		}			
		text = text_temp;		
		if(*text == '\n') {
			printf("\n");
		}
		*text++;
		text_temp = text;
	}
	printf("\nsentences = %d (counter)\n", count);
}
the counter counts the number of rows so that i could create an array of Vehicle but im stuck trying to understand how to put each information into this array, any suggestion? thanks ^^