Development > Coding

C: appending arrays

(1/3) > >>

Hoehrer:
This is most probably a noob question, but how do i append two arrays in C?
For strings there is the strcat function, but

Example code

--- Code: ---int a1[25];
int a1[25];
--- End code ---


Let's now say that the arrays are filled from the beginning (0++) and the first array has at least one free space at the end. Is there an existing function/way to do this or do i need to write my own?

Werner

EDIT: Note to self: This is a pretty good overview on C FAQs http://www.eskimo.com/~scs/c-faq.com/ ... need to dig through the array-section soon.

oxyXen:
Hi

You mean probably  :wink:

--- Code: ---
int a1[25]
int a2[25]

--- End code ---

and want a function doing something like : a3[50]=a1+a2

When you create an array, then its space in the memory is static. You will have to do something like that (pseudocode):

--- Code: ---arrayAppend(char* a1, char* a2)
{
   l1<-length of a1
   l2<-length of a2
   
   char* a3 = (char*) malloc (sizeof(char)*(l1+l2))
   and now use 2 loops to copy the whole stuff.
   return a3
}
--- End code ---


strcat needs as a parameter enough allocated space.

--- Quote ---Parameters.
dest
  Pointer to a null-terminated string with enough space allocated to contain both src and dest.
src
  Null-terminated string to append.
--- End quote ---

Everywhere are pros and contras ;)
What exactly do you want to do with this?

ox
 :)

edit: arrays are no black magic, but they can get me sometimes insane :D

Hoehrer:
Ugh, that is what i was afraid of .... let me show you what i need/want to do:

This is a nice little code-snippet from src/client/cl_research.c

--- Code: ---/*
======================
R_GetFirstRequired

Returns the first required technologies that are needed by "id".
That means you need to research the result to be able to research (and maybe use) "id".
======================
*/

void R_GetFirstRequired( char *id, char *required[MAX_TECHLINKS])
{
int i, j;
technology_t *t;
char temp_requiredlist[MAX_TECHLINKS][MAX_VAR];

for ( i=0; i < numTechnologies; i++ ) {
t = &technologies[i];
R_GetRequired ( t->id, temp_requiredlist );
// if (length of temp_requiredlist == 0) TODO
// _append_ it to *required TODO
// else
for ( j=0; j < MAX_TECHLINKS; j++ ) {
R_GetFirstRequired( temp_requiredlist[j], temp_requiredlist );
}
}
Com_Printf( _("R_GetFirstRequired: technology \"%s\" not found.\n"), id );
}
--- End code ---


Ok, this function is meant to loop (recursively) through an array (not the one in question) and collects multiple arrays of strings by calling
--- Code: ---R_GetRequired ( t->id, temp_requiredlist );
--- End code ---

this arrays of strings should finally be appended to (char *required)

every list of strings looks like this:

--- Code: ---char varname[MAX_TECHLINKS][MAX_VAR];
--- End code ---


I hope my explaination was clear enough?

Thanks for any help.
Werner

oxyXen:
Ok, this is now a bit heavier for me :wink:

At first i'll try to understand the system:

--- Code: ---initial
    |----tachyon
            |----tachyonrifle
                     |----tachyonrifle_ammo
                     |----tachyonsniper
                              |----tachyonsniper_ammo
--- End code ---

So, this is now a piece of the techtree.

Ok, now i wann research tachyonsniper_ammo.

R_GetFirstRequired should now return tachyon (i guess, initial is researched at the beginning of the game).

Hm, i think i don't get it completely. How about that:

--- Code: ---void R_GetRequired(technology_t *t, char *required)
{
   if (t->required->isResearched)
   {
       return;
   } else
   {
       <pseudo>append t->required to required</pseudo>
       R_GetRequired(t->required, required);
   }

   return;
}

technology_t *R_GetFirstRequired(technology_t *t)
{
   char temp[MAX...]
   R_GetRequired(t, temp);
   <pseudo>reverse temp</pseudo>
   return temp[0];
}
--- End code ---


I don't know the structs and the pointer-wirrwarr :wink: that well, so it might look strange or be even not that, what you want :(

Why don't you take integers for the ids?

You could also try to get a list of pointers to the technologies needed.

Will there be technologies which will require two or more predecessors, ie:

--- Code: ---tech oxyXen
{
type tech
name "Tachyon control"
requires oxygen
        requires    xentronium
description "oxyxen_txt"
time 100
}
--- End code ---


And last but not least, there seems to be a bug in the research.ufo file:

--- Code: ---tech plasma
{
type weapon
requires tachyonsniper
provides tachyonsniper_ammo
time 0
}
--- End code ---


I hope i could help you somehow and did not get you dizzy ;)

ox

edit: there are some errors in my sourcecode, but i hope the idea can be realized anyways.

Hoehrer:

--- Quote from: "oxyXen" ---At first i'll try to understand the system:

--- Code: ---initial
    |----tachyon
            |----tachyonrifle
                     |----tachyonrifle_ammo
                     |----tachyonsniper
                              |----tachyonsniper_ammo
--- End code ---

So, this is now a piece of the techtree.

Ok, now i wann research tachyonsniper_ammo.

R_GetFirstRequired should now return tachyon (i guess, initial is researched at the beginning of the game).
--- End quote ---

Correct, the "initial" and "nothing" requirements are terminators and indicate that this is a starting technology (initial) or one that has been aquired from the aliens (nothing). ... in this case we can handle see them as equals.

I'll look into your pseudo code and see if i can make it out ... just need some time for that :)


--- Quote ---
I don't know the structs and the pointer-wirrwarr :wink: that well, so it might look strange or be even not that, what you want :(

Why don't you take integers for the ids?

You could also try to get a list of pointers to the technologies needed.
--- End quote ---

What would that change? I would still need to handle the appending of arrays... don't I? But i might not understand what you mean exactly. I'll look into this as well.


--- Quote ---Will there be technologies which will require two or more predecessors, ie:

--- End quote ---

yes, the code for that looks like this:


--- Code: ---tech oxyXen
{
type tech
name "Oxygen-Xentronium"
requires "oxygen xentronium"
description "oxyxen_txt"
time 1000
}
--- End code ---

R_GetFirstRequired should return "oxygen" and "xentronium"


BTW: I fixed some bug in research.ufo recently including the plasma-bug


--- Quote ---I hope i could help you somehow and did not get you dizzy ;)
--- End quote ---


Thanks for your help,
Werner

Navigation

[0] Message Index

[#] Next page

Go to full version