私の以前の投稿を参照してください: https://stackoverflow.com/questions/12548828/trying-to-save-command-line-arguments-in-a-dynamic-array-in-c
したがって、char* 配列とその中の要素の総数を返したいと思います。配列を反復処理して NULL が見つかったときに停止できるかどうかわからないため、要素の総数を直接取得する方法を見つけることができませんでした。それは可能ですか? 配列の範囲外に達した場合、NULL 値が返されるということですか? それ以外の場合、次のように構造体を作成しようとしました: char** vertex_name と int curr_size の 2 つの変数を持つ struct name_vertex に応じてコードを変更すると、 「expected =, ,, ;, asm or __attribute__ before unique_name」というエラー メッセージが表示されます。それが何を意味するのか理解できません..そして、.hファイルをチェックしましたが、欠落していません。または、または =。
name_vertex* unique_name(char *argv[], int argc)
{
int i, j, curr_max, index, counter, flag;
char *temp;
name_vertex* structure_p;
/*
*Allocating of size 4 char*. Trying to make it a string pointer array
*Not sure if this is the correct way
*/
structure_p= malloc(4*sizeof(name_vertex));
/*
*Setting the first two elements of the allocation to
*the second and third element of the command line argv
*as you may know, the first one is the file name
*/
structure_p.vertex_name[0] = argv[1];
structure_p.vertex_name[1] = argv[2];
index=2; //Index variable for the dynamic array
curr_max = 3; //current total number of elements, 1 < total allocated as array element count starts at 0
structure_p.curr_size = 4; //Allocation amount int literal, count starts at 1
/*
*The outer loops starts at four as it is for the argv[] and the first one is irrelevent
*The outer loop essentially jumps 3 elements at a time as
*I am ultimately going to use this for a graph data structure that
*I am trying to create. So this array will store all the unique names of vertices
*so it makes setting the names when creating vertices easy
* Also will tell me how many unique vertices to create.
*The inner loop only runs once and it checks if there is a name that alreaady exist.
*It only takes into cosideration the first 2 out of the 3 that are considered in the outer loop
*The the third one is going to be an integer value for the edge weight of two vertices
*so don't need it right now
*/
for(i=4; i<argc; i+=3)
{
for(j=0; j<1; j++)
{
flag = 0;
counter = 0;
//Compare first argv[i] with all the elements
//of vertex_name array
while(counter<index)
{
if(strcmp(argv[i], stucture_p.vertex_name[counter]) == 0)
{
flag = 1;
break;
}
counter++;
}
//If no match found, allocates some memory
//adds the element to vertex_name
//Increments index, curr_size, curr_max
if(flag == 0)
{
temp = realloc(structure_p, (structure_p.curr_size + 2) * sizeof(name_vertex)); //CHECK THE SYNTAX, WANNA ADD 2 MORE ELEMENTS TO ARRAY
structure_p = temp;
structure_p.vertex_name[index] = argv[i];
index++;
structure.curr_size +=2;
curr_max +=2;
}
flag = 0; //reset flag
counter = 0; //reset counter
//Do the same comparison as above, but
//this time its argv[i+1] compared
while(counter < index)
{
if(strcmp(argv[i+1], structure_p.vertex_name[j]) ==0)
{
flag = 1;
break;
}
counter++;
}
//If no match found, same process as before
//Increment index, curr_size, curr_max variables
if(flag == 0)
{
temp = realloc(structure.vertex_name, (structure.curr_size + 2) * sizeof(name_vertex)); //CHECK THE SYNTAX, WANNA ADD 2 MORE ELEMENTS TO ARRAY
structure_p.vertex_name = temp;
structure_p.vertex_name[index] = argv[i+1];
index++;
structure_p.curr_size += 2;
curr_max +=2;
}
}
}
//Returning the new array
return structure_p;
}