所以,我想返回一个 char* 数组和其中的元素总数。我无法找到直接获取元素总数的方法,因为我不知道是否可以遍历数组并在发现 NULL 时停止,这可能吗?我的意思是,如果它超出了数组的范围,它会给出一个 NULL 值吗?否则,我尝试按如下方式创建结构: struct name_vertex 它有两个变量 char** vertex_name 和 int curr_size 并根据更改代码,我收到错误消息 “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;
}