1

请参阅我以前的帖子: https ://stackoverflow.com/questions/12548828/trying-to-save-command-line-arguments-in-a-dynamic-array-in-c

所以,我想返回一个 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;
     }
4

2 回答 2

0

name_vertex可能未定义或定义与您尝试使用它的方式不同。

选项1:

typedef struct
{
  // yada, yada, yada
} name_vertex;

name_vertex unique_name(/*etc*/)
{
  // ...
  name_vertex structure;
  // ...
  return structure;
}

选项 2:

struct name_vertex
{
  // yada, yada, yada
};

struct name_vertex unique_name(/*etc*/)
{
  // ...
  struct name_vertex structure;
  // ...
  return structure;
}

选项 3:

typedef struct name_vertex
{
  // yada, yada, yada
} name_vertex;

name_vertex unique_name1(/*etc*/)
{
  // ...
  name_vertex structure;
  // ...
  return structure;
}

struct name_vertex unique_name2(/*etc*/)
{
  // ...
  struct name_vertex structure;
  // ...
  return structure;
}

至于用指针遍历数组,当指针超出数组边界时,它不会变成NULL. 您要么必须知道数组大小或指向其第一个或最后一个元素的指针(以适当者为准),要么数组本身必须在其第一个/最后一个元素中包含一个特殊值,这样您就知道何时完成了对数组的处理。没有魔法。您必须自己管理指针和数组索引。

于 2012-09-23T04:29:36.807 回答
-1

C 不支持从函数返回复杂数据类型。相反,您需要传入一个指向已分配结构的指针并让函数填充它,或者让函数返回一个新分配的指针。

选项1:

void unique_name(char *argv[], int argc, name_vertex* structure_p) {
  // fill structure_p using structure_p->field = value.
}
// In the caller:
name_vertex* structure_p = malloc(sizeof(name_vertex));
unique_name(argv, argc, structure_p);
// use structure_p
free(structure_p);

选项 2:

name_vertex* unique_name(char *argv[], int argc) {
  // ...
  name_vertex* structure_p = malloc(sizeof(name_vertex));
  // fill structure_p using structure_p->field = value.
  return structure_p;
}

// In the caller:
name_vertex* structure_p = unique_name(argv, argc);
// use structure_p
free(structure_p);
于 2012-09-23T04:10:24.483 回答