hello !

i have a program id like to code , and it uses from 3 upto 15 members of a structure , id like to ask ,
1,

is there a way to not define all members of a structure at the start but add members as needed ?

2,

im fairly sure im missing something out , im sure theres a better way to add 15 members to a structure than this , but i cant remember ?

struct jatekos ja1,ja2,ja3,ja4,ja5,ja6,ja7,ja8,ja9,ja10,ja11,ja12,ja13,ja14,ja15;

3,

the following code isnt working because of the

strcpy(tmp,ja.nev)

what can i do to tell the program to work with "ja1" struct member ? and after that "ja2" .. and the number after the "ja" would always be the value of "i"
like stings , for example

tmp[i]; i++;

... i want to increment that value !!
i know it doesnt work because its a different object/function altogether but is there a way to give a variable to C in this format ? where part of the variables name is a variable itself ?

struct jatekos
{
	char nev[20];
	int dob1;
	int dob2;
	int dob3;
};
struct jatekos ja1,ja2,ja3,ja4,ja5,ja6,ja7,ja8,ja9,ja10,ja11,ja12,ja13,ja14,ja15;

int x;
int i;
char tmp[20];




void main (void){
	i=1;

		printf("adja meg az %d. jatekos nevet ! ha vegzett , ures sor !",i);
	while(getline(tmp,20)>0){
		strcpy(tmp,ja[i].nev)
i++


	}
getchar();

1. No.

2. That does not add members to the structure, it declared 15 objects of the structure. A better way to do that is by using an array of structs e.g. jatekos array[15]; // declare 15 objects of type jatekos 3. Ok the code you posted here makes more sense. Here again, like above, you need to declare an array of those structures.

struct jatekos ja[15];

while(getline(tmp,20)>0){
   strcpy(ja[i].nev,temp)
   i++
}

//or like this, eliminating the need for temp array
while(getline(ja[i].nev20)>0){
   i++
}

1 is kind of possible .... But it is messy
Check out this piece of code

#include<stdio.h>
#include<stdlib.h>

struct student
{
        void *subjects[15];

};

int main() {

        struct student s1;
        int numberOfSubjects;
        int i =0;

        printf("How main subject does the student have \n");
        scanf("%d",&numberOfSubjects);

        for(i=0;i< numberOfSubjects;i++) {
                s1.subjects[i] = (int *)malloc(sizeof(int) *1);
        }

        printf("Now enter the marks\n");

        for(i=0;i<numberOfSubjects;i++){
                printf("Subject %d \n",i);
                scanf("%d",(int *)s1.subjects[i]);
        }

        printf("****************************");

        printf("The entered marks are \n");

        for(i=0;i<numberOfSubjects;i++){
                printf("Subject number:%d -----%d\n",i,*(int *)s1.subjects[i]);
        }

        printf("\n");

        return 0;
}

actually, I suspect that what he's really looking for with the first question is not to be able to add members to the struct, but to be able to dynamically allocate new structs which would be associated with some other data structure, such as a linked list or a tree.

thanks for the answers ..

what i was looking for was not allocating the whole ja[15] array structure at the start but only 3 members of it , and expanding as the user enters more ... but i figured out its not worth the effort , for now ....

what i do have is another problem , i have to print the current time into a file , i already have some output going into the file , and a separate timer measuring how much did it take to run the program , but what i need is the current hour/minutes printed into the file to go along with it , i cant seem to find the format that would work ...

i gathered these values from here
i realize tm_min should be an integer , and since there is no fputint() function in C that i know of , i should convert it .

file_ptr = fopen("eredmeny.txt", "w");
	fputs(ja[jat].nev,file_ptr);
[B]		itoa(tm_min,time1);
	fputs(time1,file_ptr);[/B]
	fclose(file_ptr);

my compiler says tm_min is undefined

however when i copy the structure definition to my code , it complains its already defined in time.h ?

>>what i was looking for was not allocating the whole ja[15] array structure at the start but only 3 members of it , and expanding as the user enters more

Yes that can be fairly easily done by declaring ja as a pointer and not an array. Each time you use an item of the array you have to increment nItemsUsed.

struct  jatekos* ja = malloc(sizeof(struct jatekos) * 3); // allocate 3 members
int jaSize = 3;
int nItemsUsed = 0;

// now in a loop or something start filling the array
// when the array is full then reallocate it to a larger size
if( nItemsUsed == jaSize)
{
   // bump size by 5 items
   jaSize += 5;
   // now reallocate the array
   ja = realloc(ja, sizeof(struct jatekos) * jaSize);
}

>>however when i copy the structure definition to my code , it complains its already defined in time.h ?

You are not supposed to copy standard C structures into your code -- juse use the #include <time.h> statement .

i see .
i had an error with too few arguments for the itoa()

itoa(tm_min,time1,10);
	fputs(time1,file_ptr);

now it simply prints a 0 after the output i originally had

i suspect its because i havent assigned some standard time (local or gm) it should work by , and its delivering Null ?

To answer the last question first, the solution is simply to add #include <time.h> at the top of the code file. This automatically copies the structure definition into your code, without setting off alarm bells with the compiler.

Oh, and itoa() is deprecated, and is not part of the standard library; a better solution is to use

sprintf(time1, "%d", tm_min);

As for the original question, well, as I said earlier, chances are a linked list would be what you want. You would re-define the struct as

struct jatekos
{
	char nev[20];
	int dob1;
	int dob2;
	int dob3;
        struct jatekos* next;  
        /* You might use 'kov', perhaps? I don't know Hungarian, I just checked Google Translate for an equivalent and it gave 'következő' */
};

jatekos* head;

You would need to define functions for adding, removing, searching, etc., but those aren't too difficult to write.

The other alternative is to use an extensible array, which Ancient Dragon already explained the basics of.

itoa() is deprecated, and is not part of the standard library

The second part is correct, the first part is not. itoa was never part of the standard library, and thus cannot be deprecated. Deprecated means that a feature of the standard is slated for future removal; it's still standard and available, but not recommended for use.

Valid point; I misspoke myself. Thank you for the correction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.